如何在laravel中添加多表关系
我有以下表格,并建立了与Laravel关系的查询。在我的面板中,一旦用户登录,我将获得用户详细信息和用户业务详细信息,但现在我也不知道如何获得业务类型详细信息 用户表: 用户业务表 业务类型表 模型: 用户如何在laravel中添加多表关系,laravel,eloquent,Laravel,Eloquent,我有以下表格,并建立了与Laravel关系的查询。在我的面板中,一旦用户登录,我将获得用户详细信息和用户业务详细信息,但现在我也不知道如何获得业务类型详细信息 用户表: 用户业务表 业务类型表 模型: 用户 <?php namespace App; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; use Zizaco\Entrust\Trai
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Zizaco\Entrust\Traits\EntrustUserTrait;
use App\Notifications\ResetPassword as ResetPasswordNotification;
use Laravel\Passport\HasApiTokens;
use Illuminate\Database\Eloquent\SoftDeletes;
use Webpatser\Uuid\Uuid;
use App\RoleUser;
use App\UsersBusiness;
class User extends Authenticatable
{
use Notifiable;
use EntrustUserTrait;
use HasApiTokens;
use SoftDeletes, EntrustUserTrait {
SoftDeletes::restore as sfRestore;
EntrustUserTrait::restore as euRestore;
}
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'business_id', 'username', 'email'
];
public function usersBusiness()
{
return $this->belongsTo('App\UsersBusiness', 'business_id', 'id');
}
}
我得到以下响应数据:
{
"data": {
"id": 1,
"business_id": 1,
"username": "john632",
"email": "john632@gmail.com",
"password": "john632",
"users_business": {
"id": 1,
"user_id": 4,
"business_type_id": 3,
"business_name": "Honest"
},
"business_types": null
},
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6Ijk4YTQ5OTBmOGQxOWQ5NTg1OGFlZWU1MDY0NTBiY2Y2OWJmOWQ3NzFhZjRmN2RmMzBmMWRkZWNmNWY4OTAzM2UyNmI2MzE3MTY3MDMxOTk4In0.eyJhdWQiOiIxIiwianRpIjoiOThhNDk5MGY4ZDE5ZDk1ODU4YWVlZTUwNjQ1MGJjZjY5YmY5ZDc3MWFmNGY3ZGYzMGYxZGRlY2Y1Zjg5MDMzZTI2YjYzMTcxNjcwMzE5OTgiLCJpYXQiOjE1MzIyNjA3ODUsIm5iZiI6MTUzMjI2MDc4NSwiZXhwIjoxNTYzNzk2Nzg1LCJzdWIiOiI1Iiwic2NvcGVzIjpbXX0.f_Xv0SrtTZ9m-40oHjAglCbKv76s_bARQ74XDihhFnI-jtHKwCWiF-jai5Yt6h9QyakCTZEo1bPAJdeph7Bj0_tKJpq3sGvK4t73_LZg_OOcsmAt61a4OSAgI1pjPV0tMMwHCoHm-xLlNnriAyaLCAbTQLQkfrw53467ys6rchE5V0rzy-JswjTfmB6SvZcqXsJQo6CWDRTWYbKvJO0FSmdZfLxxO_u4i_8ah5W63qJ4MSN9q22zkZLQ-L3NZhOux2KkwWiySioL2K25Y_UZmefClYwk1h-EY_LEVht3U7Kpqn9fmM6_Q4ByD-sSzLdAixdbq4REqinSaayzfMY934nijLu7ysEIc0oIukiHYcIk9tGV6DNuQ0CWhqEn0W_308MSBU4Ffyi5SQo7ubb5uPG7l_XOdomIR9dK9KtVONbPe7iF6TuccPCWZwvqKgfFl7TqEgiUWSiAl_ekkiaUDEM3cIuIH8AOLE17UuW4W0VyR2ziIt68au8SEuP2ilMBRsRMsFGbRKQWcvLluNw_qubcdzZ4yX9kuQAvXuBrHAcXb9WMlki2votvd7RKVDwxqwsTJRoeKNtJQdEQRbRZUD6nXyzGkmtEMrfwYoLVgTX3vAgVjO_erYtI5x-NV-EnoLT352odtRDYh5gTzVbmzYAxbLf_XUCDHjvlMEvM81g",
"status": true,
"status_code": 1,
"message": "Login successfully."
}
我也想添加业务类型详细信息,所以请您指导我如何为该表添加关系
谢谢。在您的
UserBusiness
模型中创建一个belongsTo关系,如下所示
$user = Auth::user();
$user->load(['usersBusiness.businessType']); //lazy loading
dd($user);
用户商业模式
public function businessType(){
return $this->belongsTo(BusinessType::class, 'business_type_id');
}
之后,像这样加载它
$user = Auth::user();
$user->load(['usersBusiness.businessType']); //lazy loading
dd($user);
如果您只需要userBusiness及其类型
dd($user->usersBusiness);
旁注:我认为用户表中不需要business\u id
。只需在userbusiness表中保留user\u id
,然后在userbusiness的用户模型中添加hasOne
(如果用户只能有一个业务)或hasMany
(如果用户可以有多个业务)关系 我建议您使用
例如,在我的例子中,我有ChannelItemResource.php:
public function toArray($request)
{
return [
'id' => $this->id,
'channel_id' => $this->channel_id,
'channel' => $this->channel->title,
'type' => $this->type,
'description' => $this->description,
'price' => $this->price,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
}
和ChannelResource.php:
return [
'id' => $this->id,
'title' => $this->title,
'user_id' => $this->user_id,
'description' => $this->description,
'type' => $this->type,
'url' => $this->url,
'ranking_total' => $this->ranking_total,
'ranking_flag' => $this->ranking_flag,
'ranking_country' => $this->ranking_country,
'thumbnail' => $this->thumbnail,
'state' => $this->state,
'hit' => $this->hit,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'items' => ChannelItemResource::collection($this->items),
];
使用此结构,您可以轻松地包含所需的关系,结果将以JSON格式显示
在控制器中使用:
public function all(Request $request)
{
return new ChannelsResource(Channel::where("state", "active")->orderBy('id', 'desc')->paginate(30));
}
您有
UserBusiness
model吗?@rkj有。我已经为每个表创建了模型。我已经更新了我的问题。我已经添加了答案检查您的用户模型与业务的关系是否良好,只需在UsersBusiness中添加一个关系,您就完成了为什么您在userbusiness表中添加了User\u id
,在User表中添加了business\u id
?我认为用户表中不需要business\u id
。只需在userbusiness表中保留user\u id
,然后在用户模型中添加'hasOne'或hasMany
关系即可
$user = Auth::user();
$user->load(['usersBusiness.businessType']); //lazy loading
dd($user);
dd($user->usersBusiness);
public function toArray($request)
{
return [
'id' => $this->id,
'channel_id' => $this->channel_id,
'channel' => $this->channel->title,
'type' => $this->type,
'description' => $this->description,
'price' => $this->price,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
}
return [
'id' => $this->id,
'title' => $this->title,
'user_id' => $this->user_id,
'description' => $this->description,
'type' => $this->type,
'url' => $this->url,
'ranking_total' => $this->ranking_total,
'ranking_flag' => $this->ranking_flag,
'ranking_country' => $this->ranking_country,
'thumbnail' => $this->thumbnail,
'state' => $this->state,
'hit' => $this->hit,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'items' => ChannelItemResource::collection($this->items),
];
public function all(Request $request)
{
return new ChannelsResource(Channel::where("state", "active")->orderBy('id', 'desc')->paginate(30));
}