我正在檢索分配給用戶的所有故障單。單個票據具有用戶表的外鍵。這是下面的代碼:如何根據Laravel中的刀片和控制器中的ID獲取列值?
App\Models\User.php
protected $appends = ['full_name', 'avatar'];
public function getFullNameAttribute()
{
return $this->first_name . ' ' . $this->last_name;
}
App\Http\Controllers\TicketsController.php
/**
* Display a listing of the resource.
*/
public function index()
{
$tickets = Ticket::paginate(25);
$users = User::all();
//return view('user.tickets.index', compact('tickets'));
return View::make('user.tickets.index', compact('tickets','users'));
}
Resources/Views/User/Tickets/index.blade.php
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Status</th>
<th>Assigned To</th>
</tr>
</thead>
<tbody>
@foreach($tickets as $item)
<tr>
<td>{{ $item->status }}</td>
<td>{{ $item->user_id }}</td>
</tr>
@endforeach
</tbody>
</table>
我要到外地{{$用品 - > USER_ID}}轉換爲{{$用品 - > FIRST_NAME其中id = $用品 - > USER_ID}}領域應該轉換成名字根據$ item-> user_id
user_id是門票的外鍵,相當於它是Users表的。
請幫忙。
謝謝你讓我知道。我只知道在視圖中編寫數據庫查詢並不是一個好習慣。你是一個天才。你的回答是正確的 –