【发布时间】:2021-10-26 02:52:26
【问题描述】:
我的在线轮班申请有问题:
用户可以查看他们的轮班请求。问题是点击视图(...)总是重定向到两个寄存器中的移位ID 93。
控制器:
public function inicio() {
if (Auth::guest()) {
return redirect()->route("login");
}
$config = Configuraciones::findOrFail(0);
// Solicitudes = REQUEST
$solicitudes = Solicitudes::where('user_id', Auth::User()->id)->orderByDesc('created_at')->take(5)->get();
// GET THE ID OF THE REQUEST FROM THE LOGED USER
$solicitud = Solicitudes::where('user_id', Auth::User()->id)->first();
// GET THE SHIFT 'id' RELATED TO THE 'id' OF THE request
$eventos = Eventos::where('solicitud_id', $solicitud->id)->first();
return view('solicitudes.inicio', compact('solicitudes', 'config', 'eventos'))->with($data);
}
刀片:
@foreach($solicitudes as $solicitud)
@if(Auth::user()->id == $solicitud->user_id)
<tr>
<td>{{ $solicitud->fecha }}</td>
<td>{{ $solicitud->hora }}</td>
<td class="text-center">
@if($solicitud->estado == '0')
<span class="badge badge-primary">Pendiente</span>
@elseif($solicitud->estado == '1')
<span class="badge badge-success">Aceptada</span>
@elseif($solicitud->estado == '2')
<span class="badge badge-danger">Rechazada</span>
@endif
</td>
<td class="text-center">
<div class="dropdown custom-dropdown">
<a class="dropdown-toggle" href="#" role="button" id="dropdownMenuLink1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-more-horizontal">
<circle cx="12" cy="12" r="1"></circle>
<circle cx="19" cy="12" r="1"></circle>
<circle cx="5" cy="12" r="1"></circle>
</svg>
</a>
<div class="dropdown-menu" aria-labelledby="dropdownMenuLink1">
<a class="dropdown-item" href="/turno/{{ $eventos->id }}">Ver</a>
</div>
</div>
</td>
</tr>
@endif
@endforeach
尝试使用:$eventos = Eventos::where('solicitud_id', $solicitud->id)->get();,但报错:
此集合实例上不存在属性 [id]。
错误:
我能做些什么来获得正确的班次 ID 并且同一件事不会总是重复?
【问题讨论】:
-
$eventos是一个集合。您需要遍历集合以访问其属性。错误可能来自这一行:<a class="dropdown-item" href="/turno/{{$eventos->id}}">Ver</a> -
是的,在那一行是'id'的错误。我该如何通过它?
-
循环播放。
foreach。如果只有一条记录属于请求 solcitud->id,您可以将get()更改为first(),您可以继续使用$eventos->id,但请确保处理空情况;optional($eventos)->id. -
这没有修复错误? (指你刚刚删除的评论)
-
是的,它修复了它,但它仍然只显示'id' 93
标签: php laravel laravel-5 laravel-blade laravel-8