Reputation: 3074
This is an awesome package: https://github.com/staudenmeir/eloquent-json-relations But I can't make it work with couple of days struggle. Let me explain my situation and my approach.
orders table has json type column named sender_address
with the following value:
{
"title":"office",
"street":"Mohakhali Flyover",
"lat":"23.7794099",
"lng":"90.3983494",
"district":"1",
"area":"2"
}
My Order Model Definition:
class Order extends Model
{
use \Staudenmeir\EloquentJsonRelations\HasJsonRelationships;
/**
* @var string The database table used by the model.
*/
public $table = 'orders';
/**
* @var array Validation rules
*/
public $rules = [
];
protected $casts = [
"sender_address" => "array"
];
public $belongsTo = [
'client' => [ '\ItScholarBd\Api\Models\User'],
'customer' => [ 'ItScholarBd\Order\Models\Customer'],
'district' => [ 'ItScholarBd\Location\Models\District'],
'clientArea' => [ '\ItScholarBd\Location\Models\Area','sender_address->area']
];
}
Usage:
$records = Order::with(['client.company','clientArea'])->get();
Here I just want to get \ItScholarBd\Location\Models\Area
data matching with sender_address->area
. But $records
show the following data:
Array
(
[0] => Array
(
[id] => 1
[number] => AWB2000001
[sender_address] => Array
(
[title] => office
[street] => Mohakhali Flyover
[lat] => 23.7794099
[lng] => 90.3983494
[district] => 1
[area] => 2
)
[client_id] => 309
[client] => Array
(
[id] => 309
[name] => client
[email] => [email protected]
[password] => $2y$10$BIQgKvOV4h0lWOO66W6CC.rCEnFB6c6mBCh3WF2t1eDFueEJmgb7y
[activation_code] =>
[persist_code] => $2y$10$1Njt/420DYIyW3azOYh9z./WwURdgFf.7JKCGE0jqjTQmBPK/nmSe
[reset_password_code] =>
[permissions] =>
[is_activated] => 1
[activated_at] => 2020-11-27 18:41:04
[last_login] => 2020-12-17 13:50:48
[created_at] => 2020-11-27 18:41:04
[updated_at] => 2020-12-17 13:50:48
[username] => client
[surname] =>
[deleted_at] =>
[last_seen] => 2020-12-17 16:55:42
[is_guest] => 0
[is_superuser] => 0
[phone] =>
[company] => Array
(
[id] => 4
[name] => Bracnect Academy
[email] => [email protected]
[url] => https://academy.bracnet.net/
[phone] => 01708006884
[alt_phone] => 01709007885
[address] => mohakhali
[product_type] => Course
[created_at] => 2020-12-02 16:20:54
[updated_at] => 2020-12-02 16:22:23
[code] => bracnectacademy
)
)
[client_area] =>
)
)
Here client_area is empty. But I double the areas table where a records exist with id = 2
. What's wrong in my approach? I am struggling with this issue for 2 days. Any Idea?
Upvotes: 0
Views: 4127
Reputation: 400
I've never seen anyone define relationships this way nor did I find it in the documentation:
public $belongsTo = [
'client' => [ '\ItScholarBd\Api\Models\User'],
'customer' => [ 'ItScholarBd\Order\Models\Customer'],
'district' => [ 'ItScholarBd\Location\Models\District'],
'clientArea' => [ '\ItScholarBd\Location\Models\Area','sender_address->area']
];
I think if you define the relationships in this way:
public function clientArea(){
return $this->belongsTo('\ItScholarBd\Location\Models\Area', 'sender_address->area');
}
it should work.
And dont forget to use the HasJsonRelationships
trait in the Area model as well.
Upvotes: 1