Reputation: 41
I have 2 master table
and 2 user table
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
So I want to get data from tbl_users_business_sub_delivery_options
table using relationship in laravel.
I have tried using hasMany relationship in function some as below code.
public function usersBusinessDeliveryTypesWeb()
{
return $this->hasMany('App\UsersBusinessSubDeliveryOption', 'business_id');
}
but I am getting the null value.
[usersBusinessDeliveryTypesWeb] => Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
)
)
But if I make the function using belongsTo.
public function usersBusinessDeliveryTypesWeb()
{
return $this->belongsTo('App\UsersBusinessSubDeliveryOption', 'business_id');
}
then I am getting only one value as given below.
[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] =>
)
But actually I want the output like this.
[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] =>
)
)
Upvotes: 0
Views: 922
Reputation: 2972
hasOne -> returns one item
hasMany -> returns a collection
belongsTo -> returns one item
belongsToMany -> returns a collection. (works only with pivot tables)
Also, in your models, do you specify your primary key, cause you are not using the standard 'id' col for each table... instead you use names like: delivery_type_id.
i would, either Check that my models have the proper id set.
Or Use explicitly
return $this->hasMany('App\UsersBusinessSubDeliveryOption', 'business_id', 'business_id');
Upvotes: 0