阅读(4749) (7)

Laravel 8 处理关联关系

2021-07-06 09:24:57 更新

因为要加载的 Model 关联关系也会被序列化,导致序列化的任务字符串可能会变得非常大。为了防止关系被序列化,您可以在设置属性值时调用模型上的 withoutRelations 方法。这个方法会返回一个没有加载关系的模型实例:

/**
 * 创建一个新的 job 实例
 *
 * @param  \App\Models\Podcast  $podcast
 * @return void
 */
public function __construct(Podcast $podcast)
{
    $this->podcast = $podcast->withoutRelations();
}