简体   繁体   English

Laravel 6 在 belongsTo 关系上使用 with() 急切加载只是*有时*返回 null

[英]Laravel 6 eager loading using with() on a belongsTo relationship is only *sometimes* returning null

I am working on a project where we have a model for a service provider, the type of care provided, and the status:我正在做一个项目,我们有一个 model 用于服务提供商,提供的护理类型和状态:

Provider:提供者:

class Provider extends Model
{
    protected $table = 'providers';

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

    public function caretype() {
        return $this->belongsTo('App\CareType', 'id');
    }
}

CareType:护理类型:

class CareType extends Model
{
    protected $table = 'type_of_care';

    public function providers() {
        return $this->hasMany('App\Providers', 'type_of_care_id');
    }

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

Status:地位:

class Status extends Model
{
    protected $table = 'status';

    public function providers() {
        return $this->hasMany('App\Providers');
    }
}

On the my SearchController (the controller that processes search requests for providers), the show() function using eager loading retrieves the caretype perfectly.在我的SearchController (处理提供者搜索请求的 controller)上,使用预加载的show() function 可以完美地检索caretype But on the search() function that lists the collection of search results, the caretype is always listed as null.但是在列出搜索结果集合的search() function 中, caretype始终列为 null。

I don't understand why it would be working in one function but not the other, especially when the code to eager load is exactly the same in both functions:我不明白为什么它会在一个 function 而不是另一个中工作,特别是当两个函数中的预加载代码完全相同时:

public function search(Request $request)
    {

        $validated = $request->validate([
            //I removed the validation code for this post
        ]);

        $providers = Provider::with(['status', 'caretype'])->get();

        return view('search.results', ['providers' => $providers]);

    }

    public function show($id)
    {

        $single_provider = Provider::with(['status', 'caretype'])->where('id', $id)->first();
        return view('search.details', ['provider' => $single_provider]);

    }

Any help in this would be appreciated.对此的任何帮助将不胜感激。 I know that the model and relationship foreign keys are properly defined because the show() function is able to get the caretype just fine.我知道 model 和关系外键已正确定义,因为show() caretype能够很好地获得护理类型。

nope.不。 your relationship and foreign keys are not correct.您的关系和外键不正确。 as from the doc文档

Eloquent determines the default foreign key name by examining the name of the relationship method and suffixing the method name with a _ followed by the name of the primary key column. Eloquent 通过检查关系方法的名称并在方法名称后加一个_后跟主键列的名称来确定默认外键名称。 However, if the foreign key on the Child model is not like that, you may pass a custom key name as the second argument to the belongsTo method.但是,如果 Child model 上的外键不是这样,您可以将自定义键名称作为第二个参数传递给 belongsTo 方法。

you are passing the id column as the foreign key in Provider model's caretype ralation but your foreign key is type_of_care_id .您将id列作为Provider模型的caretype中的外键传递,但您的外键是type_of_care_id so you are getting some results when the id matches but if not, you are getting null.所以当 id 匹配时你会得到一些结果,但如果不匹配,你会得到 null。 change your relationship code to将您的关系代码更改为

public function caretype()
{
    return $this->belongsTo('App\CareType', 'type_of_care_id');
}

now again from the doc现在再次来自文档

If your parent model does not use id as its primary key, or you wish to join the child model to a different column, you may pass a third argument to the belongsTo method specifying your parent table's custom key.如果您的父 model 不使用id作为其主键,或者您希望将子 model 加入不同的列,您可以将第三个参数传递给指定父表的自定义键的belongsTo方法。

in your case id is the primary key.在您的情况下, id是主键。 so you don't have to pass the third parameter.所以你不必传递第三个参数。 just update the primary key reference and everything will work perfectly.只需更新主键引用,一切都会完美运行。

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

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