试图在laravel中获取非对象的属性'expiry_date'
Posted
技术标签:
【中文标题】试图在laravel中获取非对象的属性\'expiry_date\'【英文标题】:Trying to get property 'expiry_date' of non-object in laravel试图在laravel中获取非对象的属性'expiry_date' 【发布时间】:2020-06-16 20:20:12 【问题描述】:请帮我解决这个问题
我的控制器代码是
public function index()
$callrecordings = DB::table('callrecordings')
->join('users', 'users.id', '=', 'callrecordings.user_id')
->leftJoin('disableapp', 'callrecordings.user_id', '=', 'disableapp.user_id')
->select('callrecordings.*', 'users.expiry_date')
->where('callrecordings.user_id', '=', Auth::id())
->where(function($q)
$q->where('disableapp.status', '=', 1)
->orWhereNull('disableapp.status');
)
->paginate(5);
dd($callrecordings);
if($callrecordings[0]->expiry_date <= Carbon::now()->toDateTimeString())
$result = 'That email belongs to an existing referrer.';
return view('frontend.views.package-expire', compact('result'));
else
return view('frontend.views.callrecordings', compact('callrecordings'));
我的刀片文件是
<table id="example" class="table table-bordered dt-responsive nowrap dataTable no-footer dtr-inline" style="width:100%">
<thead>
<tr>
<th>Name</th>
<th>Number</th>
<th>Duration</th>
<th>Size</th>
<th>Direction</th>
<th>Audio</th>
<th>Download</th>
<th>Timestamp</th>
</tr>
</thead>
<tbody>
@foreach ($callrecordings as $callrecording)
<tr>
<td> $callrecording->name </td>
<td> $callrecording->phone</td>
<td> $callrecording->duration</td>
<td> $callrecording->size</td>
<td>
@if ($callrecording->direction === 'Incoming')
<span class="label label-success">Incoming</span>
@elseif ($callrecording->direction === 'Outgoing')
<span class="label label-info">Outgoing</span>
@elseif ($callrecording->direction === 'Rejected')
<span class="label label-warning">Rejected</span>
@else
<span class="label label-danger">Missed</span>
@endif
</td>
<td>
<audio id='sound1'>
<source src=" URL::to('/') /uploads/audio/ $callrecording->audio " type="audio/ogg">
<source src=" URL::to('/') /uploads/audio/ $callrecording->audio " type="audio/mpeg">
</audio>
<div class="play btn btn-success btn-xs" id="btn1">play</div>
</td>
<td>
<a href=" URL::to('/') /uploads/audio/ $callrecording->audio " download="proposed_file_name" class="btn btn-primary btn-xs"><i class="icon-download-alt"> </i> Download </a>
<!---button class="btn btn-primary btn-xs" data-download=" URL::to('/') /uploads/audio/ $callrecording->audio "><i class="fa fa-fw fa-download"></i> Download</button---->
</td>
<td> $callrecording->created_at </td>
</tr>
@endforeach
</tbody>
</table>
此条件有效,但如果我在表中执行表状态 0 或没有增值,则会显示此错误消息正在尝试获取非对象的属性 'expiry_date'。
我附上了我的 dd($callrecordings);
请告诉我应该如何克服这个问题
提前致谢。
【问题讨论】:
【参考方案1】:您在代码中遗漏了一些内容,如果数据不存在,则检查 0 元素的数据,因此我们需要检查是否存在以进行记录
if(isset($callrecordings[0]->expiry_date) && $callrecordings[0]->expiry_date <= Carbon::now()->toDateTimeString())
【讨论】:
以上是关于试图在laravel中获取非对象的属性'expiry_date'的主要内容,如果未能解决你的问题,请参考以下文章
试图在laravel中获取非对象的属性'expiry_date'