在laravel中,我们这样指定外键。
public function creator() {
     return $this->belongsTo('App\Models\User', 'creator_id', 'id');
}当creator_id为null时,有没有办法返回虚拟用户('system')?
原因:在模板中显示它。
例如:
如果creator_id = 10,=>创建者= {id: 1,名称:'John'}
If creator_id = null系统创建者= {id: 0,=>:'system'}
发布于 2017-03-02 21:21:27
在您的模板中尝试以下内容:
{{ (model->creator) ? model->creator->name : 'system' }}模型将替换为您的变量名。
发布于 2017-03-02 21:56:49
创建一个id =0、name = 'System‘的虚拟用户。
在迁移过程中将creator_id设置为默认值0
https://stackoverflow.com/questions/42556500
复制相似问题