Laravel 5.1 - 具有空值的模型关系
Laravel 5.1 - Relationships on model with null values
我在 Laravel 5.1.
上具有空值的模型上的关系有问题
我有 2 个表任务和用户。在任务中,我有列 id_requester 和 id_responsible.
我想显示所有任务,我总是有一个请求者,但有时我还没有一个负责人。
因此在这种情况下 id_responsible 为空。
我的模特是:
protected $table = 'tasks';
protected $fillable = array(
'id_requester',
'id_responsible',
);
public function requester()
{
return $this->hasOne('app\User', 'id', 'id_requester');
}
public function responsible()
{
return $this->hasOne('app\User', 'id', 'id_responsible');
}
我的控制器中的查询是:
$tasks = Tasks::get();
我试着这样显示:
<table>
...
@foreach($tasks as $task)
<td>{{ $task->requester->name }}</td>
<td>{{ $task->responsible->name }}</td>
@endforeach
...
</table>
问题是当我尝试访问页面时出现错误 'Trying to get property of non-object'。
我已经测试过了,这只是当我有一个 id_responsible = Null 时。
如何解决这个问题以显示所有寄存器?
谢谢!!!
而不是:
@foreach($tasks as $task)
<td>{{ $task->requester->name }}</td>
<td>{{ $task->responsible->name }}</td>
@endforeach
你应该使用:
@foreach($tasks as $task)
<td>{{ $task->requester ? $task->requester->name : 'unknown' }}</td>
<td>{{ $task->responsible ? $task->responsible->name : 'unknown' }}</td>
@endforeach
这是因为$task->requester
和$task->responsible
都可能为空(没有相关记录)所以你不能在[=上使用名称属性(->name
) 15=]
首先,让我们谈谈显示条件数据。
来自文档:https://laravel.com/docs/5.2/blade#displaying-data
Sometimes you may wish to echo a variable, but you aren't sure if the variable has been set. We can express this in verbose PHP code like so:
{{ isset($name) ? $name : 'Default' }}
However, instead of writing a ternary statement, Blade provides you with the following convenient short-cut:
{{ $name or 'Default' }}
In this example, if the $name variable exists, its value will be displayed. However, if it does not exist, the word Default will be displayed.
换句话说,你应该可以做到这一点:
<td>{{ $task->requester->name or 'No requester' }}</td>
<td>{{ $task->responsible->name or 'Nobody responsible.' }}</td>
只有当数据存在时才会显示相应的数据。如果不是,它将显示“没有人负责”或“没有请求者”。
但是,如果您的问题是如何获取只有专人负责的任务,您可以将查询更改为使用 has
方法:
$tasks = Tasks::has('responsible')->get();
这将只检索已经分配了“负责人”的任务,因此您不必担心空值。
我在 Laravel 5.1.
上具有空值的模型上的关系有问题我有 2 个表任务和用户。在任务中,我有列 id_requester 和 id_responsible.
我想显示所有任务,我总是有一个请求者,但有时我还没有一个负责人。
因此在这种情况下 id_responsible 为空。
我的模特是:
protected $table = 'tasks';
protected $fillable = array(
'id_requester',
'id_responsible',
);
public function requester()
{
return $this->hasOne('app\User', 'id', 'id_requester');
}
public function responsible()
{
return $this->hasOne('app\User', 'id', 'id_responsible');
}
我的控制器中的查询是:
$tasks = Tasks::get();
我试着这样显示:
<table>
...
@foreach($tasks as $task)
<td>{{ $task->requester->name }}</td>
<td>{{ $task->responsible->name }}</td>
@endforeach
...
</table>
问题是当我尝试访问页面时出现错误 'Trying to get property of non-object'。
我已经测试过了,这只是当我有一个 id_responsible = Null 时。
如何解决这个问题以显示所有寄存器?
谢谢!!!
而不是:
@foreach($tasks as $task)
<td>{{ $task->requester->name }}</td>
<td>{{ $task->responsible->name }}</td>
@endforeach
你应该使用:
@foreach($tasks as $task)
<td>{{ $task->requester ? $task->requester->name : 'unknown' }}</td>
<td>{{ $task->responsible ? $task->responsible->name : 'unknown' }}</td>
@endforeach
这是因为$task->requester
和$task->responsible
都可能为空(没有相关记录)所以你不能在[=上使用名称属性(->name
) 15=]
首先,让我们谈谈显示条件数据。
来自文档:https://laravel.com/docs/5.2/blade#displaying-data
Sometimes you may wish to echo a variable, but you aren't sure if the variable has been set. We can express this in verbose PHP code like so:
{{ isset($name) ? $name : 'Default' }}
However, instead of writing a ternary statement, Blade provides you with the following convenient short-cut:
{{ $name or 'Default' }}
In this example, if the $name variable exists, its value will be displayed. However, if it does not exist, the word Default will be displayed.
换句话说,你应该可以做到这一点:
<td>{{ $task->requester->name or 'No requester' }}</td>
<td>{{ $task->responsible->name or 'Nobody responsible.' }}</td>
只有当数据存在时才会显示相应的数据。如果不是,它将显示“没有人负责”或“没有请求者”。
但是,如果您的问题是如何获取只有专人负责的任务,您可以将查询更改为使用 has
方法:
$tasks = Tasks::has('responsible')->get();
这将只检索已经分配了“负责人”的任务,因此您不必担心空值。