Php 在Laravel 5中构建多对多关系的动态查询
我对Laravel雄辩的ORM非常陌生,我很难构建一个动态查询来查询某个类别的产品 我解析请求对象并根据已传递的变量返回产品。当我查询单个模型时,这很容易,但我想知道如果类别传递到,如何动态构建查询。使用标准MYSQL和PHP很容易做到这一点,但我不确定如何在LAravel中实现这一点 这是我的密码: 产品型号:Php 在Laravel 5中构建多对多关系的动态查询,php,laravel-5,eloquent,Php,Laravel 5,Eloquent,我对Laravel雄辩的ORM非常陌生,我很难构建一个动态查询来查询某个类别的产品 我解析请求对象并根据已传递的变量返回产品。当我查询单个模型时,这很容易,但我想知道如果类别传递到,如何动态构建查询。使用标准MYSQL和PHP很容易做到这一点,但我不确定如何在LAravel中实现这一点 这是我的密码: 产品型号: <?php namespace App; use Illuminate\Database\Eloquent\Model; class Product extends Mod
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $primaryKey = 'id',
$table = 'products',
$fillable = array('title', 'SKU', 'description', 'created_at', 'updated_at');
public $timestamps = true;
/**
* Get the categories assoicated with the product
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*
*/
public function categories() {
return $this->belongsToMany('App\Category')->withTimestamps();
}
}
下面是filterProduct方法:
public static function filterProduct($vars) {
$query = Product::query();
if((array_key_exists('order_by', $vars)) && (array_key_exists('order', $vars))) {
$query = $query->orderBy($vars['order_by'], $vars['order']);
}
if(array_key_exists('cat', $vars)) {
$query = $query->whereHas('categories', function($q) use ($vars){
return $q->where('category_id', $vars['cat']);
});
}
return $query->get();
产品数据库迁移:
class CreateProductsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('products', function(Blueprint $table) {
$table->increments('id');
$table->string('title', 75);
$table->string('SKU')->unique();
$table->text('description')->nullable();
$table->timestamps();
});
}
以及显示类别表、透视表和外键等结构的迁移:
class CreateCategoriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('description');
$table->timestamps();
});
Schema::create('category_product', function(Blueprint $table) {
$table->integer('product_id')->unsigned()->index();
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
$table->integer('category_id')->unsigned()->index();
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
$table->timestamps();
});
}
我曾尝试在查询中合并“has”方法,但这似乎不起作用。有人能告诉我哪里出了问题吗
谢谢 您可能需要
在哪里
编辑
您应该在where has
方法中使用id列,因为您将where条件应用于类别表,而该表没有类别_id列
public static function filterProduct($vars) {
$query = Product::query();
if((array_key_exists('order_by', $vars)) && (array_key_exists('order', $vars))) {
$query = $query->orderBy($vars['order_by'], $vars['order']);
}
if(array_key_exists('cat', $vars)) {
$query = $query->whereHas('categories', function($q) use ($vars){
$q->where('id', $vars['cat']);
});
}
return $query->get();
}
谢谢你的回复。我尝试了这个方法,但它没有返回任何结果,只是返回了一个空数组。在代码中,在
if
和return
之前调用get()
方法两次。试着只打一次电话。对不起,我更新了原来的问题。我只调用了get方法一次,它仍然没有返回任何内容,您的DB模式是什么?您是否使用laravel FK命名约定?您的数据透视表名称是什么?列?我更新了主要问题,其中显示了构建表和透视表的迁移。这也会显示外键
class CreateCategoriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('description');
$table->timestamps();
});
Schema::create('category_product', function(Blueprint $table) {
$table->integer('product_id')->unsigned()->index();
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
$table->integer('category_id')->unsigned()->index();
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
$table->timestamps();
});
}
$query = $query->whereHas('categories', function($q) use ($vars) {
$q->where('id', $vars['cat']);
});
public static function filterProduct($vars) {
$query = Product::query();
if((array_key_exists('order_by', $vars)) && (array_key_exists('order', $vars))) {
$query = $query->orderBy($vars['order_by'], $vars['order']);
}
if(array_key_exists('cat', $vars)) {
$query = $query->whereHas('categories', function($q) use ($vars){
$q->where('id', $vars['cat']);
});
}
return $query->get();
}