首页 > 解决方案 > Laravel hasMany Relation 不起作用

问题描述

我有 2 个主表

  1. tbl_master_delivery_type
  2. tbl_master_business_types

和 2 个用户表

  1. tbl_users_business_sub_delivery_options
  2. 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] =>
                )
)

标签: laraveleloquent

解决方案


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');


推荐阅读