C
C
CrimsonFox2022-01-21 07:26:03
Laravel
CrimsonFox, 2022-01-21 07:26:03

How to link tables through two intermediate ones?

The essence is this
There are four tables: Habitation, HabitationPlace, Place and Room
They are connected as follows

//Habitation и HabitationPlace
    public function places() {
        return $this->hasMany('App\Habitationplace', 'habitation_id', 'id');
    }
//HabitationPlace и Place 
    public function place()
    {
        return $this->hasOne('App\Place', 'id', 'place_id');
    }
//Place и Room
    public function room()
    {
        return $this->hasOne('App\Room', 'id', 'room_id');
    }

as a result, Habitation and Room are not directly related, but I need to get the level_id value from the Room for searching (it is not displayed anywhere else)
how can I connect them and where should it be described (in the model or controller)?

ps at the moment I have such a code, it works, but level does not participate in it, in honor of which the question is:
{
            $level_idd = $request['level']+2;
            $level_id = DB::table('room')->where('level_id', '=', $level_idd)->get();

        $habitation =  Habitation::with(["places"])
        ->where('status','=','saselen')
        ->whereDate('dogovor_date','>=',$start_date)
        ->whereDate('dogovor_date','<=',$date_end)
        ->where('documenul_id','=',$request['documenuls'])
        ->get();    
        }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question