Skip to main content
adicionou 93 caracteres ao conteúdo
Fonte Link
novic
  • 36,7mil
  • 4
  • 32
  • 70

Tenho as seguintes tabelas / modelsTabelas/Models e não estou conseguindo fazer uma consulta.:

Model 1

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

Model 2

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

Model 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'['name',
      'model2_id'
   ];
'model2_id'];
   protected $casts = [
      'name'['name' => 'string'
   ];
'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',
   ];
['name'];
   protected $casts = [
      'name'['name' => 'string'
   ];
'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'['name',
   ];
 
   protected $casts = [
      'name'['name' => 'string'
   ];
'string'];
   public static $rules = [
    
   ];[];

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

Com o exemplo abaixo não consegui pois o que preciso é saber os models1Model1 que relacionados ao model3Model3.

Obrigado

Tenho as seguintes tabelas / models e não estou conseguindo fazer uma consulta.

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);
   }
}

Com o exemplo abaixo não consegui pois o que preciso é saber os models1 que relacionados ao model3.

Obrigado

Tenho as seguintes Tabelas/Models e não estou conseguindo fazer uma consulta:

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);
   }
}

Com o exemplo abaixo não consegui pois o que preciso é saber os Model1 que relacionados ao Model3.

Fonte Link
Mamga
  • 413
  • 1
  • 6
  • 19

Laravel / Eloquent - Consulta em mais de uma tabela

Tenho as seguintes tabelas / models e não estou conseguindo fazer uma consulta.

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);
   }
}

Com o exemplo abaixo não consegui pois o que preciso é saber os models1 que relacionados ao model3.

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

Obrigado