Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.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 调用未定义的BelongsTo::attach()方法_Php_Mysql_Laravel - Fatal编程技术网

Php 调用未定义的BelongsTo::attach()方法

Php 调用未定义的BelongsTo::attach()方法,php,mysql,laravel,Php,Mysql,Laravel,为什么这种关系不起作用 我正在尝试使用Laravel5.2、mysql、迁移和种子程序来关联帖子和类别 但我有一个错误: 调用未定义的方法Illumb\Database\Eloquent\Relations\BelongsTo::attach() posttableeeder.php public function run() { factory(App\Post::class, 300)->create()->each(function (App\Post $post) {

为什么这种关系不起作用

我正在尝试使用Laravel5.2、mysql、迁移和种子程序来关联帖子和类别

但我有一个错误:

调用未定义的方法Illumb\Database\Eloquent\Relations\BelongsTo::attach()

posttableeeder.php

public function run()
{
    factory(App\Post::class, 300)->create()->each(function (App\Post $post) {
        $post->category()->attach([
            rand(1, 5),
            rand(6, 14),
            rand(15, 20),

        ]);
    });
}
public function category()
{
  return $this->belongsTo(Category::class);
}
public function posts()
{
  return $this->belongsTo(Post::class);
}
模型:
Post.php

public function run()
{
    factory(App\Post::class, 300)->create()->each(function (App\Post $post) {
        $post->category()->attach([
            rand(1, 5),
            rand(6, 14),
            rand(15, 20),

        ]);
    });
}
public function category()
{
  return $this->belongsTo(Category::class);
}
public function posts()
{
  return $this->belongsTo(Post::class);
}
模型:
Category.php

public function run()
{
    factory(App\Post::class, 300)->create()->each(function (App\Post $post) {
        $post->category()->attach([
            rand(1, 5),
            rand(6, 14),
            rand(15, 20),

        ]);
    });
}
public function category()
{
  return $this->belongsTo(Category::class);
}
public function posts()
{
  return $this->belongsTo(Post::class);
}

在模型中定义
归属关系

公共功能类别()
{
返回$this->belongtomany(Category::class);
}
不要忘记为
Post
Category
关联添加一个中间透视表

由于您不使用RTFM,下面是一个完整的工作示例

posttableeeder.php

public function run()
{
    factory(App\Post::class, 300)->create()->each(function (App\Post $post) {
        $post->category()->attach([
            rand(1, 5),
            rand(6, 14),
            rand(15, 20),

        ]);
    });
}
public function category()
{
  return $this->belongsTo(Category::class);
}
public function posts()
{
  return $this->belongsTo(Post::class);
}
公共函数运行()
{
工厂(App\Post::class,300)->create()->each(函数(App\Post$Post){
$post->categories()->附加([
兰特(1,5),
兰特(6,14),
兰特(15,20),
]);
});
}
Post.php
model

public function categories()
{
返回$this->belongstomy('App\Category');
}
Category.php
model

公共职能岗位()
{
返回$this->belongstomy('App\Category');
}
category\u post
表迁移

Schema::create('category_post',函数(Blueprint$表){
$table->unsignedbiginger('post_id');
$table->unsignedbiginger('category_id');
});

希望这有帮助:)

在模型中定义
归属关系

公共功能类别()
{
返回$this->belongtomany(Category::class);
}
不要忘记为
Post
Category
关联添加一个中间透视表

由于您不使用RTFM,下面是一个完整的工作示例

posttableeeder.php

public function run()
{
    factory(App\Post::class, 300)->create()->each(function (App\Post $post) {
        $post->category()->attach([
            rand(1, 5),
            rand(6, 14),
            rand(15, 20),

        ]);
    });
}
public function category()
{
  return $this->belongsTo(Category::class);
}
public function posts()
{
  return $this->belongsTo(Post::class);
}
公共函数运行()
{
工厂(App\Post::class,300)->create()->each(函数(App\Post$Post){
$post->categories()->附加([
兰特(1,5),
兰特(6,14),
兰特(15,20),
]);
});
}
Post.php
model

public function categories()
{
返回$this->belongstomy('App\Category');
}
Category.php
model

公共职能岗位()
{
返回$this->belongstomy('App\Category');
}
category\u post
表迁移

Schema::create('category_post',函数(Blueprint$表){
$table->unsignedbiginger('post_id');
$table->unsignedbiginger('category_id');
});

希望这有帮助:)

非常感谢您的回复,我在Post模型``公共函数category(){return$this->belongomany(category::class);//Post pertence a una categoria}``中添加了belongomany,但结果是``SQLSTATE[42S22]:未找到列:1054未知列'field list'中的'category\u id'(SQL:将值(4,1)、(6,1)、(19,1)插入到
类别帖子中(
category\u id
post\u id
)```嗯。难道不应该有一个
有…
的一面和一个
属于…
的一面吗?我也在想@Caddy DZ在阅读后的建议:如果没有表格,它也可能是一个有很多属于的一面,比如@ficuscr建议我还不明白外键在哪里OP没有发布表格@Porlosceros-ψ,任何假设可能是错误的。我建议您阅读文档或观看Laracast(非赞助商),这样您就不会浪费任何时间试图发现自己的东西。非常感谢您的回答,我在发布模型``public function category(){return$this->belongstomy(category::class)中添加了belongstomy;//post pertence a una categoria}```但结果是`SQLSTATE[42S22]:未找到列:字段列表中的1054未知列'category_id'(SQL:插入
category_post
category_id
post_id
)值(4,1)、(6,1)、(19,1))```嗯。难道不应该有一个
有…
的一面和一个
属于…
的一面吗?我也在想@Caddy DZ在阅读后的建议:如果没有表格,它也可能是一个有很多属于的一面,比如@ficuscr建议我还不明白外键在哪里OP没有发布表格@Porlosceros-ψ,任何假设可能是错误的。我建议阅读文档或观看Laracast(非赞助)你不会浪费任何时间去发现你自己的东西,我想如果你想在一篇文章中附加许多类别,你需要将关系定义为belongTomany而不是belongTomany。我不清楚什么属于什么,你能不能在问题中添加相关的表字段?我想如果你想在一篇文章中附加许多类别,你可以需要将关系定义为belongToMany而不是belongSto我不清楚什么属于什么,请在问题中添加相关的表字段好吗?