【问题标题】:set relations in evailable full table in laravel在 laravel 的可用全表中设置关系
【发布时间】:2017-03-29 23:38:58
【问题描述】:

我创建如下迁移:

  public function up()
{
    //
    Schema::create('categories', function (Blueprint $table) {
        $table->increments('id');
        $table->unsignedInteger('parent')->nullable()->unsigned();;

        $table->string('category_name');
        $table->text('category_lable');
        $table->timestamps();
});
    Schema::table('categories', function (Blueprint $table) {

        $table->foreign('parent')
            ->references('id')->on('categories')
            ->onDelete('cascade')
            ->onUpdate('cascade');
    }) ;
}

迁移是通过这种方式播种的:

   Category::create([
        'id' => 1,
        'parent' => null,
        'category_name' => "root"
    ]);

    Category::create([
            'id' => 2,
            'parent' => 1,
            'category_name' => "something"
         ]);

    Category::create([
        'id' => 3,
        'parent' => 2,
            'category_name' => "something"
            ]);
    Category::create([
        'id' => 4,
        'parent' => 2,
            'category_name' => "something"
            ]);

    and etc ...

这也是我自己引用的模型:

class Category extends Model
{
public function getchildren() {
    return $this->has_many('App\Category' , 'parent');
}
public function getparent()   {
    return $this->belongs_to('App\Category' , 'parent');
}

}

在修补程序中,当我尝试获取某个节点父节点或子节点时,出现错误。

错误:

BadMethodCallException 并带有消息“调用未定义的方法 Illuminate\Database\Query\Builder::getparent()”

BadMethodCallException 并带有消息“调用未定义的方法 Illuminate\Database\Query\Builder::getchildren()”

我哪里错了?

【问题讨论】:

    标签: laravel laravel-5 orm model relationship


    【解决方案1】:

    首先,关系方法应该是camelCase

    public function getchildren() {
      return $this->hasMany('App\Category' , 'parent');
    }
    public function getparent()   {
      return $this->belongsTo('App\Category' , 'parent');
    }
    

    其次,发生错误是因为您可能以错误的方式请求这些关系。您是在 Builder 实例上调用这些方法,而不是在 Model 实例上。

    它应该看起来像这样:

    $category = Category::first();
    
    var_dump($category->getchildren, $category->getparent);
    

    或者:

    foreach(Categories::all() as $category) {
      var_dump($category->getchildren, $category->getparent);
    }
    

    无论如何,如果你能分享你在 Tinker 中输入的代码,我们就能准确地找出问题所在。

    编辑

    你应该将你的方法重命名为children()parent(),所以没有get前缀。

    另外,要直接检索这种关系,请将它们称为属性,而不是方法:

    $model = Category::findOrFail(26);
    
    // this will return Builder instance
    var_dump($model->children(), $model->parent());
    
    // this will return the return a Collection of children
    var_dump($model->children);
    
    // this will return the parent
    var_dump($model->parent);
    

    差是 ()。

    【讨论】:

    • tnanks @jan-willem 请注意。这是我的:$cat = Category::findOrFail(26) 然后:$cat->children()
    • 它返回 null :(( ($model->children)
    猜你喜欢
    • 2018-12-08
    • 2017-07-20
    • 1970-01-01
    • 2012-09-27
    • 2017-06-25
    • 1970-01-01
    • 1970-01-01
    • 2021-02-17
    • 1970-01-01
    相关资源
    最近更新 更多