Laravel / Eloquent - Consultation in more than one table

Asked

Viewed 706 times

1

I have the following Tabelas/Models and I’m not getting a consultation:

Model 1

id | Name
---+---------------
1  | Model1.1
2  | Model1.2
3  | Model1.3

Model 2

id | Name     | model1_id
---+----------+---------- 
1  | Model2.1 | 1
2  | Model2.2 | 2
3  | Model2.3 | 3

Model 3

id | Name     | model2_id
---+----------+---------- 
1  | Model3.1 | 1
2  | Model3.2 | 2
3  | Model3.3 | 3

class Model1 extends Model
{
   public $table = 'model1';

   const CREATED_AT = 'created_at';
   const UPDATED_AT = 'updated_at';
   protected $dates = ['deleted_at'];
   public $fillable = ['name','model2_id'];
   protected $casts = ['name' => 'string'];
   public static $rules = [];

   public function model2()
   {
      return $this->belongsTo(\App\Models\Model2::class);
   }
}

class Model2 extends Model
{
   public $table = 'model2';

   const CREATED_AT = 'created_at';
   const UPDATED_AT = 'updated_at';
   protected $dates = ['deleted_at'];
   public $fillable = ['name'];
   protected $casts = ['name' => 'string'];
   public static $rules = [];

   public function model1()
   {
      return $this->hasMany(\App\Models\Model1::class);
   }

   public function model2()
   {
       return $this->belongsTo(\App\Models\Model2::class);
   }
}

class Model3 extends Model
{
   public $table = 'model3';

   const CREATED_AT = 'created_at';
   const UPDATED_AT = 'updated_at';
   protected $dates = ['deleted_at'];
   public $fillable = ['name',];
   protected $casts = ['name' => 'string'];
   public static $rules = [];

   public function model2()
   {
      return $this->hasMany(\App\Models\Model2::class);
   }
}

With the example below I could not because what I need is to know Model1 that related to the Model3.

$model1 = Model3::where('model2_id', 2);

1 answer

1


Relationship N:M Laravel

The models are set wrong in item fillable failed to put the fields correctly and their relations are all wrong, see documentation for more information and the links below with examples of how to set up relationships 1:1, 1:N and N:M:


Modified example:

class Model1 extends Model
{
   public $table = 'model1';

   const CREATED_AT = 'created_at';
   const UPDATED_AT = 'updated_at';
   protected $dates = ['deleted_at'];
   public $fillable = ['name'];
   protected $casts = ['name' => 'string'];
   public static $rules = [];

   public function model2()
   {
      return $this->hasMany(\App\Models\Model2::class,'model1_id', 'id');
   }
}

class Model2 extends Model
{
   public $table = 'model2';

   const CREATED_AT = 'created_at';
   const UPDATED_AT = 'updated_at';
   protected $dates = ['deleted_at'];
   public $fillable = ['name','model1_id'];
   protected $casts = ['name' => 'string'];
   public static $rules = [];

   public function model1()
   {
      return $this->belongsTo(\App\Models\Model1::class,'model1_id', 'id');
   }

   public function model3()
   {
       return $this->hasMany(\App\Models\Model3::class,'model2_id', 'id');
   }
}

class Model3 extends Model
{
   public $table = 'model3';

   const CREATED_AT = 'created_at';
   const UPDATED_AT = 'updated_at';
   protected $dates = ['deleted_at'];
   public $fillable = ['name','model2_id'];
   protected $casts = ['name' => 'string'];
   public static $rules = [];

   public function model2()
   {
      return $this->belongsTo(\App\Models\Model2::class,'model2_id','id');
   }
}

The question I got a little bit in doubt, but, I think it’s a simple join between the tables:

With the example below I could not, because what I need is to know Model1 that related to the Model3.

$model1 = Model1::join('model2','model2.model1_id','=','model1.id')
                ->join('model3','model3.model2_id','=','model2.id')
                ->where('model3.model2_id', 2)
                ->get();

References

  • 1

    Helped a lot... I managed to solve the "problem" with all the tips.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.