Php 为什么我的表单没有发布到透视表?
我使用的是Laravel 5。我一直在关注如何使用表单创建带有“标记”的文章。我用类别替换了标签。我的文章“属于许多”类别,我的类别“属于许多”类别。我有一个表单可以创建一篇新文章,但它只在Php 为什么我的表单没有发布到透视表?,php,mysql,laravel-5,Php,Mysql,Laravel 5,我使用的是Laravel 5。我一直在关注如何使用表单创建带有“标记”的文章。我用类别替换了标签。我的文章“属于许多”类别,我的类别“属于许多”类别。我有一个表单可以创建一篇新文章,但它只在articles表中创建一个条目,而不在article\u类别中创建一个条目 我得到这个错误: FatalErrorException in ArticlesController.php line 77: 对数组上的成员函数categories()的调用 我认为问题在于store函数中的第四行。我找不到有同
articles
表中创建一个条目,而不在article\u类别中创建一个条目
我得到这个错误:
FatalErrorException in ArticlesController.php line 77:
对数组上的成员函数categories()的调用
我认为问题在于store
函数中的第四行。我找不到有同样问题的人。该行应该将文章id
附加到类别id
,但它不起作用。谢谢你的帮助
我的文章管理员:
public function store(CreateArticleRequest $request)
{
$article = $request->all();
Article::create($article);
$categoriesId = $request->input('categories');
$article->categories()->attach($categoriesId);
return redirect()->route('articles_path');
}
名称空间:
namespace App\Http\Controllers;
use App\Article;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Requests\CreateArticleRequest;
use App\Http\Controllers\Controller;
use App\Category;
use App\Day;
路线:
Route::resource('articles', 'ArticlesController', [
'names' => [
'index' => 'articles_path',
'show' => 'article_path',
'edit' => 'articleEdit_path',
'update' => 'articleUpdate_path',
'create' => 'articleCreate_path',
'store' => 'articleStore_path'
]
]);
文章范本:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
public function categories()
{
return $this->belongsToMany('App\Category', 'article_category', 'category_id', 'article_id')->withTimestamps();
}
protected $fillable = array('title', 'description', 'image', 'lat', 'lng');
}
类别模型
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
public function articles()
{
return $this->belongsToMany('App\Article', 'article_category', 'article_id', 'category_id')->withTimestamps();
}
protected $fillable = array('name');
}
数据透视表:
Schema::create('article_category', function (Blueprint $table) {
$table->integer('article_id')->unsigned()->index();
$table->foreign('article_id')->references('id')->on('articles')->onDelete('cascade');
$table->integer('category_id')->unsigned()->index();
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
$table->timestamps();
});
这似乎是一个错误:
$article = $request->all();
Article::create($article);
试试这个
$article = Article::create($request->all());
编辑
在您的文章模型中更正此关系
public function categories()
{
return $this->belongsToMany('App\Category', 'article_category', 'article_id', 'category_id')->withTimestamps();
}
嗨,谢谢。如果我更改,我会得到以下错误:SQLSTATE[23000]:完整性约束冲突:1452无法添加或更新子行:外键约束失败(storelocatordb
article\u category
,constraintarticle\u category\u id\u foreign
外键(category\u id
)引用categories
(id
)(SQL:insert-intoarticle\u category
(article\u id
,category\u id
,created\u at
,updated\u at
)值(1,482015-10-27 20:11:102015-10-27 20:11:10),(2,482015-10-27 20:11:10)似乎您试图附加到文章中的类别尚未创建,可能吗?id为48的类别是否存在?id为1、2、3的类别只有三个。?图例!谢谢。现在可以工作了。“应用\类别”之后的那些规定是否必要?为什么?我非常感谢解释/链接到文档。再次感谢!链接。它们是如果您坚持laravel约定,则不需要。该链接的第一段解释了这一点,但基本上如果您有文章和类别的多对多,则透视表应命名为article\u category
(按字母顺序排列的单数表名)以及该表上的外键article\u id
和category\u id
。如果所有设置都是这样,则不需要它们,但如果名称不同,则需要指定它们。