Laravel有很多关系无法正常工作

时间:2018-08-21 13:07:40

标签: laravel eloquent

我有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

所以我想使用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] =>
                )
)

1 个答案:

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