Php 多重雄辩关系

Php 多重雄辩关系,php,laravel,eloquent,relation,laravel-eloquent,Php,Laravel,Eloquent,Relation,Laravel Eloquent,在我的应用程序中,我有几个模型,每个模型都有一些关系。 我的问题是,我必须获取所有相关数据,所以我正在使用querybuilder,我不知道从哪里开始 如果有人知道并想帮助我,这些就是我的模型和关系(下面我将解释我如何需要这些数据) 生产商:(产品) 在本例中,我将创建一个控制器方法,该方法通过ajax从视图接收所有过滤器(zona、categoria和ubicacion)。之后,您可以在查询中使用参数,并使用一个通用函数应用过滤器并返回您在视图中指定的内容 因此,步骤如下: 创建调用控制器函数

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

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

生产商:(产品)


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

因此,步骤如下:

  • 创建调用控制器函数的路由

  • 使用要在消息数据中应用的过滤器向该路由发送ajax请求

  • 在控制器函数中接收过滤器的信息并创建查询

  • 路线 将路由添加到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请求 视图中JavaScript中的ajax请求(使用jQuery)类似于以下内容,但指向您创建的路由:

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

    好了,a终于得到了这个,谢谢你的回复^^

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

    最好的解决方案是:不要将ORM用于复杂的SQL。我同意@tereško的观点。在本例中,我将创建一个控制器方法,通过ajax接收来自视图的所有过滤器(zona、categoria和ubicacion)。之后,您可以在查询中使用参数,并拥有一个通用函数。
    <?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");
        }
    }
    
    <?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");
        }
    }
    
    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({
        url: 'fabricaciones/data',
        type: 'GET',
        dataType: 'json',
        data: { categoria_id: 1 },
    })
    .done(function(received_data) {
        console.log(received_data);
    })
    .fail(function() {
        console.log("error");
    });
    
    $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);