dimanche 23 septembre 2018

Laravel 5.7 query with 3 tables

I'm pretty new to Laravel and i got stuck with building a more complex query:

I've 3 tables with their models:

Codes:

  • id
  • description
  • factor

    public function purposes() {
        return $this->hasMany('App\Purpose', 'code_purposes', 'code_id', 'purpose_id'); 
    //I could be wrong here
    }
    
    

Purpose:

  • id
  • name
  • description

Code_purposes:

  • code_id
  • purpose_id

      public function codes() {
        $this->belongsToMany('App\Code'); //I could be wrong here
    }
    
    public function purposes() {
        $this->belongsToMany('App\Purpose'); //I could be wrong here
    }
    
    

What I want is to fetch all the codes with the condition where the purposes name = 'some_name'

I thought this would be easy with the relationships, but I can't figure it out.

So how do I do this in Laravel?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire