如何在 Laravel 中使用其他 table 的列
How to use column from other table in Laravel
在我的导航栏上,我想放置用户的姓名和头像。每次用户登录他们的帐户时,他们都可以在导航栏上看到自己的名字和头像。在此之前,姓名和头像列在我的用户table中。现在,我将它们移至其他 table,适用于每种类型的用户,例如买家、承包商和管理员。两个 table 通过使用 user_id 列关联。
用户table
id
role
email
password
买家table
id
user_id
name
avatar
email
承包商table
id
user_id
name
avatar
email
navbar.blade
<a href="#" class="dropdown-toggle" data-toggle="dropdown"><img src="http://localhost:8000/storage/images/{{auth()->user()->avatar}}"class="img-circle" alt="Avatar <span>{{auth()->user()->name}}</span><i class="icon-submenu lnr lnr-chevron-down"></i></a>
我不应该使用 {{auth()->user()->avatar}}
和 {{auth()->user()->name}}
因为 user table[= 中没有 name
和 avatar
列43=] 了。那么,我需要使用什么查询?
这个问题可以通过使用多态关系来解决。更多相关信息:https://laravel.com/docs/8.x/eloquent-relationships#one-to-many-polymorphic-relations
如果您的“类型”table 中的信息全面相同,您最好只在 users
中添加一个名为 type
的列 table。此列可以是 enum
.
如果你想保持你当前的方法,多态关系来拯救!
您应该从 buyers
、contractors
和 admins
table 中删除 user_id
。相反,您的 users
table.
中将有两个多态列
您的 table 应如下所示:
用户table
id
role
email
password
typable_id - integer
typable_type - string
买家table
id
name
avatar
email
承包商table
id
name
avatar
email
管理员table
id
name
avatar
email
您的模型应如下所示:
用户模型
class User extends Model
{
public function typable()
{
return $this->morphTo();
}
}
买家模式
class Buyer extends Model
{
public function user()
{
return $this->morphMany(User::class, 'typable');
}
}
承包商模型
class Contractor extends Model
{
public function user()
{
return $this->morphMany(User::class, 'typable');
}
}
管理模式
class Admin extends Model
{
public function user()
{
return $this->morphMany(User::class, 'typable');
}
}
您的视图应该如下所示:
<a href="#">
<img src="http://localhost:8000/storage/images/{{auth()->user()->typable->avatar}}">
<span>{{auth()->user()->typable->name}}</span>
</a>
您可以添加任意数量的类型,只需添加另一个 table 和型号即可。您的 User
模型现在可以输入了。
更新:
需要创建新用户时,先创建typable
,再创建关系函数的用户:
public function create(Request $request)
{
$buyer = Buyer::create([
'name' => $request->name,
'avatar' => $request->avatar,
'email' => $request->email,
]);
$buyer->user()->create([
'role' => 'buyer',
'email' => $request->email,
'password' => bcrypt('system'),
'remember_token' => Str::random(60),
]);
}
在我的导航栏上,我想放置用户的姓名和头像。每次用户登录他们的帐户时,他们都可以在导航栏上看到自己的名字和头像。在此之前,姓名和头像列在我的用户table中。现在,我将它们移至其他 table,适用于每种类型的用户,例如买家、承包商和管理员。两个 table 通过使用 user_id 列关联。
用户table
id
role
email
password
买家table
id
user_id
name
avatar
email
承包商table
id
user_id
name
avatar
email
navbar.blade
<a href="#" class="dropdown-toggle" data-toggle="dropdown"><img src="http://localhost:8000/storage/images/{{auth()->user()->avatar}}"class="img-circle" alt="Avatar <span>{{auth()->user()->name}}</span><i class="icon-submenu lnr lnr-chevron-down"></i></a>
我不应该使用 {{auth()->user()->avatar}}
和 {{auth()->user()->name}}
因为 user table[= 中没有 name
和 avatar
列43=] 了。那么,我需要使用什么查询?
这个问题可以通过使用多态关系来解决。更多相关信息:https://laravel.com/docs/8.x/eloquent-relationships#one-to-many-polymorphic-relations
如果您的“类型”table 中的信息全面相同,您最好只在 users
中添加一个名为 type
的列 table。此列可以是 enum
.
如果你想保持你当前的方法,多态关系来拯救!
您应该从 buyers
、contractors
和 admins
table 中删除 user_id
。相反,您的 users
table.
您的 table 应如下所示:
用户table
id
role
email
password
typable_id - integer
typable_type - string
买家table
id
name
avatar
email
承包商table
id
name
avatar
email
管理员table
id
name
avatar
email
您的模型应如下所示:
用户模型
class User extends Model
{
public function typable()
{
return $this->morphTo();
}
}
买家模式
class Buyer extends Model
{
public function user()
{
return $this->morphMany(User::class, 'typable');
}
}
承包商模型
class Contractor extends Model
{
public function user()
{
return $this->morphMany(User::class, 'typable');
}
}
管理模式
class Admin extends Model
{
public function user()
{
return $this->morphMany(User::class, 'typable');
}
}
您的视图应该如下所示:
<a href="#">
<img src="http://localhost:8000/storage/images/{{auth()->user()->typable->avatar}}">
<span>{{auth()->user()->typable->name}}</span>
</a>
您可以添加任意数量的类型,只需添加另一个 table 和型号即可。您的 User
模型现在可以输入了。
更新:
需要创建新用户时,先创建typable
,再创建关系函数的用户:
public function create(Request $request)
{
$buyer = Buyer::create([
'name' => $request->name,
'avatar' => $request->avatar,
'email' => $request->email,
]);
$buyer->user()->create([
'role' => 'buyer',
'email' => $request->email,
'password' => bcrypt('system'),
'remember_token' => Str::random(60),
]);
}