【问题标题】:Why is my form not posting to the pivot table?为什么我的表单没有发布到数据透视表?
【发布时间】:2015-10-27 12:46:18
【问题描述】:

我正在使用 Laravel 5。我一直在关注 this tutorial 关于使用表单创建带有“标签”的文章。我已经用类别替换了标签。我的文章“属于许多”类别,而我的类别“属于许多”文章。我有一个创建新文章的表单,但它只在articles 表中创建一个条目,而不是在article_category 数据透视表中。

我收到此错误:

FatalErrorException in ArticlesController.php line 77:

调用数组上的成员函数 categories()

我认为问题出在store 函数的第四行。我找不到有同样问题的人。该行应该将文章id 附加到ids 类别,但它不起作用。感谢您的帮助。

我的文章控制器:

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();
});

【问题讨论】:

    标签: php mysql laravel-5


    【解决方案1】:

    这似乎是一个错误:

    $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_category,CONSTRAINT article_category_category_id_foreign FOREIGN KEY(@987654328 @) REFERENCES categories (id)) (SQL: insert into article_category (article_id, category_id, created_at, updated_at) 值 (1, 48, 2015-10-27 20: 11:10, 2015-10-27 20:11:10), (2, 48, 2015-10-27 20:11:10, 2015-10-27 20:11:10))
    • 似乎您尝试附加到文章的类别尚未创建,这可能吗? id 为 48 的类别是否存在?
    • id 为 1,2,3 的类别只有三个。 ?
    • 传奇!谢谢。现在工作。 'App\Category' 之后的那些规定是必要的吗?为什么?我将不胜感激文档的解释/链接。再次感谢!
    • Heres 一个链接。如果您遵守 laravel 约定,则不需要它们。该链接中的第一段对其进行了解释,但基本上,如果您对文章和类别说多对多,则数据透视表应命名为article_category(按字母顺序排列的单数表名),该表上的外键为article_idcategory_id。如果一切都以这种方式设置,则您不需要它们,但如果名称不同,则需要指定它们。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 2011-04-15
    • 1970-01-01
    • 2019-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多