我正在使用具有多对多关系的 laravel 5。 下面是我的代码。
文章.php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
class Article extends Model {
protected $fillable = ['title', 'body', 'published_at', 'user_id'];
protected $dates = ['published_at'];
public function setPublishedArAttribute($date)
{
Carbon::createFromFormat('Y-m-d', $date);
$this->attributes['published_at'] = Carbon::parse($date);
}
public function scopePublished($query)
{
$query->where('published_at', '<=', Carbon::now());
}
public function scopeUnpublished($query)
{
$query->where('published_at', '>', Carbon::now());
}
public function user()
{
return $this->belongsTo('App\User');
}
public function tags()
{
return $this->belongsToMany('App\Tag');
}
}
标签.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tag extends Model {
protected $fillable = [
'name'
];
public function articles()
{
return $this->belongsToMany('App\Article');
}
}
文章 Controller .php
namespace App\Http\Controllers;
use App\Article;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Request;
use Carbon\Carbon;
use Auth;
class ArticlesController extends Controller {
public function __construct()
{
$this->middleware('auth', ['except', 'index']);
}
public function index()
{
$articles = Article::latest('published_at')->published()->get();
return view('articles.index', compact('articles'));
}
public function show(Article $article)
{
return view('articles.show', compact('article'));
}
public function create()
{
if(Auth::guest()){
return redirect('articles');
}
$tags = \App\Tag::lists('name', 'id');
return view('articles.create', compact('tags'));
}
public function store(Requests\ArticleRequest $request)
{
$article = Auth::user()->articles()->create($request->all());
$article->tags->attach($request->input('tags'));
\Session::flash('flash_message', 'Your article has been created!');
Article::create($request->all());
return redirect('articles');
}
public function edit(Article $article)
{
return view('articles.edit', compact('article'));
}
public function update(Article $article, Requests\ArticleRequest $request)
{
$article->update($request->all());
return redirect('articles');
}
}
当我尝试使用 ArticleController 中的存储功能创建文章并将其存储在数据库中时,出现以下错误。
ArticlesController.php 行中的 FatalErrorException:调用未定义的方法 Illuminate\Database\Eloquent\Collection::attach()
我在文章和标签之间设置了多对多的关系,也适本地添加了迁移。
但是当我尝试使用标签添加文章时出现错误。 让我知道我的代码有什么问题。设置关系中是否缺少任何内容。
这也是迁移表文件
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTagsTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('tags', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->timestamps();
});
Schema::create('article_tag', function(Blueprint $table){
$table->increments('id');
$table->integer('article_id')->unsigned()->index();
$table->foreign('article_id')->references('id')->on('articles')->onDelete('cascade');
$table->integer('tag_id')->unsigned()->index();
$table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('tags');
Schema::drop('article_tag');
}
}
让我知道我应该怎么做才能使附加与多对多关系一起工作
最佳答案
使用关系属性 ($article->tags) 将返回与文章相关的所有标签的 Collection。此 Collection 没有 attach() 方法。
您需要使用关系方法($article->tags())。关系方法返回关系实例,它有 attach() 方法。
$article->tags()->attach($request->input('tags'));
关于php - Laravel 5 调用未定义的方法 Illuminate\Database\Eloquent\Collection::attach(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38582582/
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby1.9+ 关于ruby-主要:Objectwhenrun
我想学习一些关于Continuation的知识,使用callcc方法从一些文章中键入几个示例,但我遇到了错误:NoMethodError:undefinedmethod`callcc'formain:Objectfrom(pry):2:in`'没有文章提到包含延续库。那么如何解决这个问题呢?谢谢编辑:ruby1.9.2p290(2011-07-09修订版32553)[x86_64-linux] 最佳答案 您需要要求“继续”。require'continuation' 关于ruby-继续,
我正在开发我的第一个Rubygem,并捆绑了cucumber、rspec和shoulda-matches进行测试。当我运行rspec时,出现以下错误:/app/my_gem/spec/spec_helper.rb:6:in`':undefinedmethod`configure'forShoulda::Matchers:Module(NoMethodError)这是我的gem规范:#my_gem.gemspec...Gem::Specification.newdo|spec|......spec.add_development_dependency"activemodel"spec.a
有谁知道在Heroku的Bamboo堆栈上启动并运行使用DataMapper的Sinatra应用程序所需的魔法咒语?Bamboo堆栈不包含任何预安装的系统gem,无论我尝试使用何种gem组合,我都会不断收到此错误:undefinedmethod`auto_upgrade!'forDataMapper:Module(NoMethodError)这是我的.gems文件中的内容:sinatrapgdatamapperdo_postgresdm-postgres-adapter这些是我将应用程序推送到Heroku时安装的依赖项:----->Herokureceivingpush----->Si
我有一个集合选择:此方法的单选按钮是什么?谢谢 最佳答案 Rails3中没有这样的助手。在Rails4中,它是collection_radio_buttons. 关于ruby-on-rails-rails上的ruby:radiobuttonsforcollectionselect,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/18525986/
我目前对后台队列不太满意。我正在尝试让Resque工作。我已经安装了redis和Resquegem。Redis正在运行。一个worker正在运行(rakeresque:workQUEUE=simple)。使用Web界面,我可以看到工作人员正在运行并等待工作。当我运行“rakeget_updates”时,作业已排队但失败了。我已经用defself.perform和defperform试过了。发条.raketask:get_updates=>:environmentdoResque.enqueue(GetUpdates)end类文件(app/workers/get_updates.rb)c
我正在为一个类赋值,它在rspec测试中使用了column_types方法。it"Userdatabasestructureinplace"doexpect(User.column_names).toinclude"password_digest","username"expect(User.column_types["username"].type).toeq:stringexpect(User.column_types["password_digest"].type).toeq:stringexpect(User.column_types["created_at"].type).t
我正在尝试循环哈希数组。当我到达获取枚举器开始循环的位置时,出现以下错误:undefinedmethod`[]'fornil:NilClass我的代码如下所示:defextraireAttributs(attributsParam)classeTrouvee=falsescanTrouve=falseownerOSTrouve=falseownerAppTrouve=falseresultat=Hash.new(0)attributs=Array(attributsParam)attributs.eachdo|attribut|#CRASHESHERE!!!typeAttribut=a