我有2个主表
和2个用户表
delivery_type_id (primary key)
delivery_type_name
created_by
updated_by
is_delete
status
deleted_at
created_at
updated_at
business_type_id (primary key)
business_type_name
description
created_by
updated_by
is_delete
status
deleted_at
created_at
updated_at
business_id (primary key)
business_del_sub_option_id (primary key)
business_id (foreign key)
business_type_id (foreign key)
delivery_type_id (foreign key
deleted_at
created_at
updated_at
所以我想使用laravel中的关系从tbl_users_business_sub_delivery_options
表中获取数据。
我尝试在函数中使用hasMany关系,如下代码。
public function usersBusinessDeliveryTypesWeb()
{
return $this->hasMany('App\UsersBusinessSubDeliveryOption', 'business_id');
}
但是我得到的是空值。
[usersBusinessDeliveryTypesWeb] => Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
)
)
但是如果我使用belongsTo来创建函数。
public function usersBusinessDeliveryTypesWeb()
{
return $this->belongsTo('App\UsersBusinessSubDeliveryOption', 'business_id');
}
那么我只能得到一个如下所示的值。
[usersBusinessDeliveryTypesWeb] => App\UsersBusinessSubDeliveryOption Object
(
[primaryKey:protected] => business_del_sub_option_id
[table:protected] => tbl_users_business_sub_delivery_options
[fillable:protected] => Array
(
[0] => business_id
[1] => business_type_id
[2] => delivery_type_id
)
[hidden:protected] => Array
(
)
[dates] => Array
(
[0] => deleted_at
)
[connection:protected] => pgsql
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[business_del_sub_option_id] => 1
[business_id] => 4
[business_type_id] => 3
[delivery_type_id] => 1
[deleted_at] =>
[created_at] =>
[updated_at] =>
)
[original:protected] => Array
(
[business_del_sub_option_id] => 1
[business_id] => 4
[business_type_id] => 3
[delivery_type_id] => 1
[deleted_at] =>
[created_at] =>
[updated_at] =>
)
[changes:protected] => Array
(
)
[casts:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[dispatchesEvents:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
)
[touches:protected] => Array
(
)
[timestamps] => 1
[visible:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[forceDeleting:protected] =>
)
但是实际上我想要这样的输出。
[usersBusinessDeliveryTypesWeb] => Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => App\UsersBusinessSubDeliveryOption Object
(
[primaryKey:protected] => business_del_sub_option_id
[table:protected] => tbl_users_business_sub_delivery_options
[fillable:protected] => Array
(
[0] => business_id
[1] => business_type_id
[2] => delivery_type_id
)
[hidden:protected] => Array
(
)
[dates] => Array
(
[0] => deleted_at
)
[connection:protected] => pgsql
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[business_del_sub_option_id] => 1
[business_id] => 4
[business_type_id] => 3
[delivery_type_id] => 1
[deleted_at] =>
[created_at] =>
[updated_at] =>
)
[original:protected] => Array
(
[business_del_sub_option_id] => 1
[business_id] => 4
[business_type_id] => 3
[delivery_type_id] => 1
[deleted_at] =>
[created_at] =>
[updated_at] =>
)
[changes:protected] => Array
(
)
[casts:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[dispatchesEvents:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
)
[touches:protected] => Array
(
)
[timestamps] => 1
[visible:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[forceDeleting:protected] =>
)
[1] => App\UsersBusinessSubDeliveryOption Object
(
[primaryKey:protected] => business_del_sub_option_id
[table:protected] => tbl_users_business_sub_delivery_options
[fillable:protected] => Array
(
[0] => business_id
[1] => business_type_id
[2] => delivery_type_id
)
[hidden:protected] => Array
(
)
[dates] => Array
(
[0] => deleted_at
)
[connection:protected] => pgsql
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[business_del_sub_option_id] => 1
[business_id] => 4
[business_type_id] => 3
[delivery_type_id] => 2
[deleted_at] =>
[created_at] =>
[updated_at] =>
)
[original:protected] => Array
(
[business_del_sub_option_id] => 1
[business_id] => 4
[business_type_id] => 3
[delivery_type_id] => 2
[deleted_at] =>
[created_at] =>
[updated_at] =>
)
[changes:protected] => Array
(
)
[casts:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[dispatchesEvents:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
)
[touches:protected] => Array
(
)
[timestamps] => 1
[visible:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[forceDeleting:protected] =>
)
)
答案 0 :(得分:0)
hasOne -> returns one item
hasMany -> returns a collection
belongsTo -> returns one item
belongsToMany -> returns a collection. (works only with pivot tables)
此外,在模型中,您是否指定主键,因为您没有为每个表使用标准的'id'col ...而是使用诸如delivery_type_id之类的名称。
我会选择检查我的模型是否设置了正确的ID。
或明确使用
return $this->hasMany('App\UsersBusinessSubDeliveryOption', 'business_id', 'business_id');