多重雄辩关系

时间:2017-05-17 11:19:11

标签: php laravel eloquent relation laravel-eloquent

在我的应用程序中,我有一些模型与每个模型有一些关系。 我的问题是我必须得到所有相关数据,所以我使用的是querybuilder,我不知道从哪里开始。

如果有人知道并想帮助我,这些是我的模型和关系。(我将解释我想要的数据)。

Fabricacion:(产物)

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

分类:(类别)

<?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:(测量)

<?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:(属性)

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

- 分类关系:(类别)

<?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关系:(任务)

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

问题在于我希望通过类别(类别),ubicacion(ubication),zona(工作空间)和tarea(任务)获取所有fabricacion(产品)数据过滤,获取它的atributos(属性)和medidas(度量) )。

例如:我想从zona(工作空间)=&gt;获取所有fabricacion(产品)数据X,其中categoria(category)=&gt; Y和ubicacion(ubication)=&gt;一个。 产品有一个类别,类别与任务相关(因此,具有X类别的产品将在工作区Y中)。 并且产品具有状态(因此,如果在状态1和类别中1是任务,则产品将在该任务中)。

这可能会被严重解释。有时我不解释自己,我不太懂英语。

2 个答案:

答案 0 :(得分:0)

在这种情况下,我将创建一个控制器方法,从视图中接收所有过滤器(zona,categoria和ubicacion),通过ajax发送。之后,您可以使用查询中的参数,并具有应用过滤器的通用函数,并返回您在视图中指定的内容。

所以,步骤将是:

  1. 创建一个调用控制器功能的路由。

  2. 使用要在邮件数据中应用的过滤器向该路线发送ajax请求。

  3. 在控制器功能中接收过滤器的信息并创建查询。

  4. 路线

    在web.php或routes.php文件中添加路由。

    Route::get('fabricaciones/data', 'HomeController@data'); // whatever controller name
    

    控制器

    进行查询的控制器方法的示例如下:

    function data(Request $request)
    {
        $categoria_id = $request->input('categoria_id');
    
        // ...
    
        $result = DB::table('fabricacion')
            ->join('categorias', 'categorias.id', '=', 'fabricacion.categoria_id')
            ->join(...) // more joins with the other tables
            ->where('categorias.id', '=', $categoria_id)
            ->where(...) // apply more filters
            ->select('fabricacion.*')
            ->get();
    
        return $result;
    }
    

    视图中的Ajax请求

    您的View中的JavaScript(使用jQuery)中的ajax请求将是这样的,但指向您创建的路径:

    $.ajax({
        url: 'fabricaciones/data',
        type: 'GET',
        dataType: 'json',
        data: { categoria_id: 1 },
    })
    .done(function(received_data) {
        console.log(received_data);
    })
    .fail(function() {
        console.log("error");
    });
    

答案 1 :(得分:0)

嗯,最后得到了这个,谢谢你的反应^^

$productos = Fabricacion::where(
            $where
        )
        ->whereHas("categoria", function($query) use($id_zona) {
            $query->whereHas("tareas", function($query) use($id_zona) {
                $query->whereHas("zonas", function($query) use($id_zona) {
                    $query->where([
                        ["zonas.id_zona", "=", $id_zona]
                    ]);
                    $query->whereRaw("categorias_tareas.orden = `fabricacion`.`estado`");
                });
            });
        })
        ->with(["medidas", "atributos"])
        ->orderBy("fabricacion.date_update", $date);
相关问题