id 的问题不显示正确并重复
problem with id does not show the correct and repeats
我的在线轮班申请有问题:
用户可以查看他们的班次请求。问题是单击视图(...
)总是重定向到两个寄存器中的移位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);
}
Blade:
@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();
但出现错误:
Property [id] does not exist on this collection instance.
错误:
我怎样才能获得正确的班次 ID 并且不会总是重复同样的事情?
正如评论中强调的那样,首先修复你的关系方法。
//Solicitudes model
public function eventos(){
return $this->hasMany(Eventos::class, 'solicitud_id');
}
现在,当您通过执行此查询 Solicitudes
时可以包括所有事件(注意 with('eventos')
:
的使用
$solicitudes = Solicitudes::with('eventos')->where('user_id', Auth::User()->id)->orderByDesc('created_at')->take(5)->get();
在你的 blade 中:
@foreach($solicitudes as $solicitud)
//@if(Auth::user()->id == $solicitud->user_id) => this is redundant since you are already getting solicitudes belonging to the authenticated user so it would be okay to remove
{{ $solicitud->fecha }}
@foreach($solictud->eventos as $evento)
<a class="dropdown-item" href="/turno/{{ $evento->id }}">Ver</a>
@endforeach
@endforeach
这里的想法是每个孤独都会加载其相关事件。
当你有适当的关系时,你可以使用 with
轻松获取相关模型。更多信息在这里:https://laravel.com/docs/8.x/eloquent-relationships
请注意,我没有检查其他方法,但我相信这应该可以修复您突出显示的主要错误或为您指明正确的方向。
我的在线轮班申请有问题:
用户可以查看他们的班次请求。问题是单击视图(...
)总是重定向到两个寄存器中的移位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);
}
Blade:
@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();
但出现错误:
Property [id] does not exist on this collection instance.
错误:
我怎样才能获得正确的班次 ID 并且不会总是重复同样的事情?
正如评论中强调的那样,首先修复你的关系方法。
//Solicitudes model
public function eventos(){
return $this->hasMany(Eventos::class, 'solicitud_id');
}
现在,当您通过执行此查询 Solicitudes
时可以包括所有事件(注意 with('eventos')
:
$solicitudes = Solicitudes::with('eventos')->where('user_id', Auth::User()->id)->orderByDesc('created_at')->take(5)->get();
在你的 blade 中:
@foreach($solicitudes as $solicitud)
//@if(Auth::user()->id == $solicitud->user_id) => this is redundant since you are already getting solicitudes belonging to the authenticated user so it would be okay to remove
{{ $solicitud->fecha }}
@foreach($solictud->eventos as $evento)
<a class="dropdown-item" href="/turno/{{ $evento->id }}">Ver</a>
@endforeach
@endforeach
这里的想法是每个孤独都会加载其相关事件。
当你有适当的关系时,你可以使用 with
轻松获取相关模型。更多信息在这里:https://laravel.com/docs/8.x/eloquent-relationships
请注意,我没有检查其他方法,但我相信这应该可以修复您突出显示的主要错误或为您指明正确的方向。