我的在線輪班申請有一個問題:
用戶可以查看他們的輪班申請。問題是,點擊查看(...)總是重定向到兩個暫存器中的輪班ID 93。
控制器:
public function inicio() /span>{
if (Auth::guest() ) {
return redirect()->route("login"/span>)。
}
$config = Configuraciones::findOrFail(0)。
// Solicitudes = REQUEST
$solicitudes = Solicitudes::where('user_id', Auth:: User()->id)->orderByDesc('created_at')->take(5)->get()。
//從已登錄的用戶那里獲取請求的ID。
$solicitud = Solicitudes::where('user_id', Auth::User()-> id)->first();
//獲取與請求的'id'相關的移位'id'。
$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>/span>
<td>{{ $solicitud-> hora }}</td>
<td class="text-center">
@if($solicitud->estado == '0')
<span class="badge badge-primary"/span>> Pendiente</span>
@elseif($solicitud->estado =='1')
<span class="badge badge-success"/span>> Aceptada</span>
@elseif($solicitud->estado =='2')
<span class="badge badge-danger"/span>> Rechazada</span>。
@endif
</td> @endif
<td class="text-center">
<div class="dropdown custom-dropdown"/span>>
< a class="dropdown-toggle"/span> href="#" role="button" id="dropdownMenu""dropdownMenuLink1" data-toggle="dropdown" aria- haspopup="true"/span> aria-expanded="false"/span>>
<svg xmlns="http://www.w3. org/2000/svg" width="24" height="24" viewBox "currentColor" stroke-width="2" stroke-lineecap="round" stroke-linejoin="round" class="feather羽毛-more-horizontal">
< circle cx="12" cy="12"/span> r="1"/span>>。 </circle>>
< circle cx="19" cy="12"/span> r="1"/span>> </circle>>
< circle cx="5" cy="12"/span> r="1"/span>> </circle>>
</svg>/span>
</a>/span>
< div class="dropdown-menu" aria-labelledby="dropdownMenuLink1">
< a class="dropdown-item" href="/turno/{{$eventos-> id }}">Ver</a>/span>
</div>/span>
</div>/span>
</td>/span>
</tr>
@endif
@endforeach
嘗試使用。$eventos = Eventos::where('solicitud_id', $solicitud->id)->get();但它給出的錯誤是:
屬性[id]不存在于這個集合實體中。
錯誤:
我應該怎樣做才能獲得正確的移位ID,并且同樣的事情不會總是重復?
我應該怎樣做才能獲得正確的移位ID,并且同樣的事情不會總是重復?
uj5u.com熱心網友回復:
正如評論中所強調的,首先修復你的關系方法。
/Solicitudes model。
public function eventos(){
return $this->hasMany(Eventos::class, 'solicitud_id')。
}
現在,當你查詢Solicitudes時,你可以通過這樣做包括所有的eventos(注意使用with('eventos'):
$solicitudes = Solicitudes::with('eventos')->where('user_id', Auth: :User()->id)->orderByDesc('created_at')->take(5)->get()。
而在你的刀片中:
@foreach($solicitudes as $solicitud)
//@if(Auth::user()->id == $solicitud->user_id) => 這是多余的,因為你已經得到了屬于認證用戶的征求意見,所以洗掉也無妨。
{{ $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
注意,我沒有檢查其他方法,但我相信這應該可以解決你強調的主要錯誤,或者為你指出正確的方向。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/320613.html
標籤:



