如何解决如何在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”,但我想显示它的标题。我应该怎么办? 谢谢
usersModel
<?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>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。