staudenmeir / eloquent-has-many-deep

Laravel Eloquent HasManyThrough relationships with unlimited levels
MIT License
2.67k stars 157 forks source link

relation between 3 tabels #43

Closed mRamadan0101 closed 4 years ago

mRamadan0101 commented 5 years ago

i have 3 table and want to make relation between them

order: id,total

order_details: order_id dish_name

extra_items id title

order_details_extra_items order_details_id extra_items_id

how can make relation between them in Order Model؟ i try to use this function

public function order_details_extra()
{
    return $this->hasManyDeep(
        'App\Models\OrderDetails',
        ['order_details_extra_items', 'App\Models\ExtraItems'], 
        [           
           'order_details_id',
           'id',   
           'order_id'  
        ],
        [          
          'id',    
          'extra_items_id', 
          'id'     
        ]
    );
} 
staudenmeir commented 5 years ago

Use this relationship:

public function order_details_extra()
{
    return $this->hasManyDeep(
        'App\Models\ExtraItems',
        ['App\Models\OrderDetails', 'order_details_extra_items']
    );
} 
mRamadan0101 commented 5 years ago

need to defined column 'order_details_id','extra_items_id' where

staudenmeir commented 5 years ago

This shouldn't be necessary, you are using the Laravel conventions for naming columns.

Do you get an error?

mRamadan0101 commented 5 years ago

no error but no data added

staudenmeir commented 5 years ago

What does your query look like?

mRamadan0101 commented 5 years ago

$form->multipleSelect('order_details_extra', 'Extra Items')->options(ExtraItems::all()->pluck('title', 'id'));

staudenmeir commented 5 years ago

Where does multipleSelect() come from?

mRamadan0101 commented 5 years ago

Laravel Admin https://laravel-admin.org/docs

$form->multipleSelect($column[, $label])->options([1 => 'foo', 2 => 'bar', 'val' => 'Option name']);

staudenmeir commented 5 years ago

Please try a simple query like dd(Order::find($id)->order_details_extra); first.

mRamadan0101 commented 5 years ago

null

mRamadan0101 commented 5 years ago

i add manual data and displayed dd(Order::find($id)->order_details_extra);

staudenmeir commented 5 years ago

What's the result of dd(Order::find($id)->order_details_extra());?

mRamadan0101 commented 5 years ago

too many data started from order_details