Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为什么我的表单没有发布到透视表?_Php_Mysql_Laravel 5 - Fatal编程技术网

Php 为什么我的表单没有发布到透视表?

Php 为什么我的表单没有发布到透视表?,php,mysql,laravel-5,Php,Mysql,Laravel 5,我使用的是Laravel 5。我一直在关注如何使用表单创建带有“标记”的文章。我用类别替换了标签。我的文章“属于许多”类别,我的类别“属于许多”类别。我有一个表单可以创建一篇新文章,但它只在articles表中创建一个条目,而不在article\u类别中创建一个条目 我得到这个错误: FatalErrorException in ArticlesController.php line 77: 对数组上的成员函数categories()的调用 我认为问题在于store函数中的第四行。我找不到有同

我使用的是Laravel 5。我一直在关注如何使用表单创建带有“标记”的文章。我用类别替换了标签。我的文章“属于许多”类别,我的类别“属于许多”类别。我有一个表单可以创建一篇新文章,但它只在
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
,constraint
article\u category\u id\u foreign
外键(
category\u id
)引用
categories
id
)(SQL:insert-into
article\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
。如果所有设置都是这样,则不需要它们,但如果名称不同,则需要指定它们。