简体   繁体   English

如何修复 Laravel 急切加载不适用于自定义外键

[英]How to fix Laravel eager loading not working with custom foreign key

Listing and Sub_Category have a one to many relationship. Listing 和 Sub_Category 是一对多的关系。 Listing belongs to one sub_category and sub_category has many listings.列表属于一个 sub_category,而 sub_category 有多个列表。

I have a column on my listings table 'sub_catgory_id' and I have also specified the foreign key in my relationship我的列表表“sub_catgory_id”上有一列,并且我还在我的关系中指定了外键

Listing.php Model
public function sub_category()
{
    return $this->belongsTo('App\Sub_Category', 'sub_category_id', 'id');
}

Trying to get all listings associated with a sub category尝试获取与子类别关联的所有列表

return $sub_category = \App\Sub_Category::with('listing')->get()

I get this error => Column not found: 1054 Unknown column 'listings.sub__category_id' in 'where clause'.我收到此错误 => 未找到列:“where 子句”中的 1054 列“listings.sub__category_id”未知。

I know eloquent is checking for sub__category_id (double underscore) but I am already far into this project and would like to leave it as sub_category_id (single underscore).我知道 eloquent 正在检查 sub__category_id(双下划线),但我已经深入这个项目,并希望将其保留为 sub_category_id(单下划线)。 Any ideas about how this can be achieved?关于如何实现这一目标的任何想法?

I finally figured it out a fix.我终于想出了一个解决办法。 You can camelCase your model.你可以驼峰命名你的模型。 So in my case I renamed my model from Sub_Category to subCategory so eloquent checks sub_category_id.所以就我而言,我将我的模型从 Sub_Category 重命名为 subCategory,以便雄辩地检查 sub_category_id。

I think you should set like :我认为你应该设置为:

1 .Listening.php: 1 .Listening.php:

public function sub_category()
{
    return $this->belongsTo('App\Sub_Category', 'id', 'sub_category_id');
}

2.Sub_Category.php : 2.Sub_Category.php :

public function listing()
{
    return $this->belongsTo('App\Listening', 'sub_category_id','id');
}

By laravel document in set Relationships we set first foreign_key then local_key like this (Example get from laravel website):通过laravel文件中设定的关系,我们先设置foreign_key然后local_key像这样(实例laravel网站获得):

return $this->hasOne('App\Phone', 'foreign_key', 'local_key');

You can try below code:你可以试试下面的代码:

1.Sub_Category.php(Model)

public function listimg() {
    return $this->belongsTo('App\Listening');
}

2.Contoller
$sub_category = \App\Sub_Category::with('listing')->get();

For the custom foreign key column you have pass two additional arguments for the function hasMany('Model', 'fk', 'pk') with the foreign key column name and the reference key column name.对于自定义外键列,您为函数hasMany('Model', 'fk', 'pk')传递了两个附加参数,其中包含外键列名称和引用键列名称。

class Category extends Model
{
    protected $table = 'categories';
    public function contents(){
        return $this->hasMany('App\Content', 'sub_cat_id', 'id');
    }
}
class Content extends Model
{
    //
    protected $table = 'contents';
    public function category(){
        return $this->belongsTo('App\Category');
    }
}

 Schema::create('categories', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('user_id');
            $table->integer('parent_id');
            $table->string('title');
            $table->timestamps();
        });

Schema::create('contents', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('app_id');
            $table->integer('cat_id');
            $table->bigInteger('sub_cat_id')->unsigned();
            $table->integer('content_type');
            $table->text('content');
            $table->text('title');
            $table->timestamps();
            $table->foreign('sub_cat_id')->references('id')->
            on('categories')->onDelete('cascade');

        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM