laravel - Laravel hasMany Relation 不起作用
问题描述
我有 2 个主表
- tbl_master_delivery_type
- tbl_master_business_types
和 2 个用户表
- tbl_users_business_sub_delivery_options
- tbl_users_business
tbl_master_delivery_type 表 - 主表
delivery_type_id (primary key)
delivery_type_name
created_by
updated_by
is_delete
status
deleted_at
created_at
updated_at
tbl_master_business_types - 主表
business_type_id (primary key)
business_type_name
description
created_by
updated_by
is_delete
status
deleted_at
created_at
updated_at
tbl_users_business - 用户表
business_id (primary key)
tbl_users_business_sub_delivery_options - 用户表
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
所以我想tbl_users_business_sub_delivery_options
使用 laravel 中的关系从表中获取数据。我尝试在函数中使用 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] =>
)
)
解决方案
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');
推荐阅读
- php - 将值从视图发送到控制器
- sql-server - SQL Server - OLEDB 查询有时会卡住
- regex - perl中绑定运算符表达式的值
- c++ - 无法在 QML ListView 中调用 Qt c++ 方法
- javascript - 通过示例仍然对 Javascript 中的 Closure 感到困惑
- java - 如何在spring aop中跟踪方法?
- android - 当应用程序被杀死时,Flutter android不会收到通知
- angular - 为什么 Angular HttpClient 类型检查不适用于 Map?
- javascript - 使用 Moment Js 生成时隙
- postgresql - 错误:父目录是全局可写但不是粘性 MAC