In my app, I have a few models with some relations to each one. My problem is that I must get all related data, so I am using the querybuilder, and I don't know where to begin.
If anyone knows and wants to help me, these are my models and relations.(Down I will explain how I want the data).
Fabricacion:(product)
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Fabricacion extends Model
{
protected $table = "fabricacion";
protected $primaryKey = "id_fabricacion";
protected $fillable = ["id_order", "id_megacart", "reference", "name", "width", "height", "length", "date_update", "id_categoria", "id_ubicacion", "id_incidencia", "estado"];
public $timestamps = true;
public function incidencia() {
return $this->belongsTo("App\Models\Incidencia", "id_incidencia", "id_incidencia");
}
public function categoria() {
return $this->belongsTo("App\Models\Categoria", "id_categoria", "id_categoria");
}
public function ubicacion() {
return $this->belongsTo("App\Models\Ubicacion", "id_ubicacion", "id_ubicacion");
}
public function medidas() {
return $this->belongsToMany("App\Models\Medida", "fabricacion_medidas", "id_fabricacion", "id_medida")->withPivot("medida");
}
public function atributos() {
return $this->belongsToMany("App\Models\Atributo", "fabricacion_atributos", "id_fabricacion", "id_atributo")->withPivot("atributo");
}
}
Categoria:(category)
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Categoria extends Model
{
protected $table = "categorias";
protected $primaryKey = "id_categoria";
protected $fillable = ["id_prestashop", "nom_categoria"];
public $timestamps = false;
public function fabricaciones() {
return $this->hasMany("App\Models\Fabricacion", "id_categoria", "id_categoria");
}
public function tareas() {
return $this->belongsToMany("App\Models\Tarea", "categorias_tareas", "id_categoria", "id_tarea")->withPivot("orden");
}
}
Ubicacion:(ubication)
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Ubicacion extends Model
{
protected $table = "ubicaciones";
protected $primaryKey = "id_ubicacion";
protected $fillable = ["nom_ubicacion"];
public $timestamps = false;
public function fabricaciones() {
return $this->hasMany("App\Models\Fabricacion", "id_incidencia", "id_incidencia");
}
}
Medida:(measure)
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Medida extends Model
{
protected $table = "medidas";
protected $primaryKey = "id_medida";
protected $fillable = ["nom_medida"];
public $timestamps = false;
public function fabricaciones() {
return $this->belongsToMany("App\Models\Fabricacion", "fabricacion_medidas", "id_medida", "id_fabricacion")->withPivot("medida");
}
}
Atributo:(attribute)
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Atributo extends Model
{
protected $table = "atributos";
protected $primaryKey = "id_atributo";
protected $fillable = ["id_prestashop", "nom_medida"];
public $timestamps = false;
public function fabricaciones() {
return $this->belongsToMany("App\Models\Fabricaion", "fabricacion_atributos", "id_atributo", "id_fabricacion")->withPivot("atributo");
}
}
-- Categoria Relation:(category)
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Tarea extends Model
{
protected $table = "tareas";
protected $primaryKey = "id_tarea";
protected $fillable = ["nom_tarea", "posicion"];
public $timestamps = false;
public function categorias() {
return $this->belongsToMany("App\Model\Categoria", "categorias_tareas", "id_tarea", "id_categoria")->withPivot("orden");
}
public function zonas() {
return $this->hasMany("App\Models\Zona", "id_tarea", "id_tarea");
}
}
-- Tarea Relation:(task)
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Zona extends Model
{
protected $table = "zonas";
protected $primaryKey = "id_zona";
protected $fillable = ["nom_zona", "posicion", "id_tarea", "impresora", "minSierra"];
public $timestamps = false;
public function tarea() {
return $this->belongsTo("App\Models\Tarea", "id_tarea", "id_tarea");
}
}
The problem is that I want to get all the fabricacion(product) data filtering by categoria(category), ubicacion(ubication), zona(workspace) and tarea(task), getting it's atributos(attributes) and medidas(measure).
For example: I want to get all fabricacion(product) data from the zona(workspace) => X, where categoria(category) => Y and ubicacion(ubication) => A. The products have a category, and the category is related with a task(so, the products with X category will be in workspace Y). And the products have an state(so, if is in sate 1 and in category the 1 is a task, the product will be in that task).
This might be badly explained. Sometimes I dont explain myself, and I don't know a lot of english.