为什么我的表单不发布到数据透视表? [英] Why is my form not posting to the pivot table?
问题描述
我正在使用Laravel5.我一直在关注本教程关于使用表格创建带有标签"的文章.我已将标签替换为类别.我的文章属于许多"类别,而我的文章属于许多"文章.我有一个创建新文章的表单,但是它只在articles
表中创建一个条目,而不在article_category
数据透视表中创建一个条目.
I am using Laravel 5. I have been following this tutorial about using a form to create an article with 'tags'. I have replaced tags with categories. My articles 'belong to many' categories and my categories 'belong to many' articles. I have a form to create a new article but it only creates an entry in the articles
table and not in the article_category
pivot table.
我收到此错误:
FatalErrorException in ArticlesController.php line 77:
在数组上调用成员函数category()
Call to a member function categories() on array
我认为问题出在store
函数的第四行.我找不到任何人遇到同样的问题.该行应该将文章id
附加到类别ids
,但是它不起作用.感谢您的帮助.
I think the problem is with fourth line in the store
function. I can't find anyone with the same problem. That line is supposed to attach the article id
to the category ids
but it's not working. Grateful for any help.
我的ArticlesController:
My ArticlesController:
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());
编辑 在您的文章模型中更正此关系
EDIT Correct this relation in you article model
public function categories()
{
return $this->belongsToMany('App\Category', 'article_category', 'article_id', 'category_id')->withTimestamps();
}
这篇关于为什么我的表单不发布到数据透视表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!