![](/img/trans.png)
[英]Laravel - How to use a foreign key to a table with an identifying foreign key?
[英]how to use a field of other table in view using foreign key in laravel
我有兩個表“團隊”和“用戶”。 在用戶中,我在“團隊”表中的“ID”字段中有一個外鍵(“current_team_ID)。現在我想在我們的視圖中查看用戶團隊的名稱。
這是我的控制器代碼:
public function index() {
$user = User::orderby('id', 'desc')->paginate(20);
$current_team_id = Teams::pluck('title', 'id');
return view('admin.users.index')->with(compact('user', 'current_team_id'));
}
和索引代碼:
@foreach ($user as $item )
<tr class="tableRows">
<td>{{ $item->id }}</td>
<td>{{ $item->name }}</td>
<td>{{ $item->email }}</td>
<td>{{ $item->stateID }}</td>
<td>{{ $item->current_team_id }}</td>
</tr>
@endforeach
它只顯示團隊的“ID”,但我想顯示它的標題。 我應該怎么辦? 謝謝
用戶模型
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Fortify\TwoFactorAuthenticatable;
use Laravel\Jetstream\HasProfilePhoto;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable {
use HasApiTokens;
use HasFactory;
use HasProfilePhoto;
use Notifiable;
use TwoFactorAuthenticatable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'username',
'mobile',
'personalCode' ,
'email',
'password',
'stateID',
'lastLoginDateTime',
'token'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
'two_factor_recovery_codes',
'two_factor_secret',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
/**
* The accessors to append to the model's array form.
*
* @var array
*/
protected $appends = [
'profile_photo_url',
];
}
團隊模型
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Teams extends Model {
use HasFactory;
protected $table = 'teams';
public $primaryKey = 'id';
public $timestamps = false ;
}
如果用戶團隊定義了關系,您可以在用戶模型中執行此操作
public function team()
{
return $this->belongsTo(Team::class);
}
public function get_team_name()
{
if($this->team()->exists()) { // check if the user have a related team
return $this->team->title;
}
}
然后在刀片循環中你可以
<td>{{ $item->get_team_name() }}</td>
首先讓我們創建關系
將此添加到User
類
public function team(){
return $this->belongsTo(Team::class);
}
將此添加到 Teams 類
public function users(){
return $this->hasMany(User::class);
}
如果你想訪問一個用戶
$users = Teams::find(1)->with('users')->get();
return view('admin.users.index')->with(compact('users');
如果你想展示一個有用戶的團隊
@foreach($users as $user)
<td>$user->team->title </td>
@endforeach
=>add on your users table.
$table->foreign('current_team_id')->references('id')->on('teams')->onDelete('cascade');
=>add relationship on Users Model
public function team()
{
return $this->hasOne(Teams::class, 'id', 'current_team_id');
}
=>you can access directly use team relationship in the user index file.
<td>{{$item->team->title}}</td>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.