首页 > 解决方案 > 在 Laravel 中的主模型关系中搜索

问题描述

我是 Laravel 的初学者。我的代码有小问题

在我的系统中,我有制作它的橱柜和板子。我正在做一个橱柜制造商。每个机柜都有分配给它的特定板。每个机柜可能有不同的板。

在这一刻,我有:

class Product extends Model
{
    use ScopeActiveTrait;
    use Slugable;

    public function setNameAttribute($value)
    {
        $this->attributes['name'] = $value;
        $this->attributes['slug'] = $this->makeSlug($value);
    }

    protected $fillable = ['mark_popular', 'mark_new', 'delivery_time', 'product_type', 'name', 'title', 'description', 'keywords', 'content', 'vat_id', 'main_category_id', 'enable', 'slug', 'small_description'];
    protected $guarded = ['id'];
    public $timestamps = false;

    public function vat()
    {
        return $this->belongsTo('App\Models\Vat', 'vat_id');
    }

    public function category()
    {
        return $this->belongsTo('App\Models\Category', 'main_category_id');
    }

    public function selectedCategory()
    {
        return $this->hasMany('App\Models\SelectedProductCategory', 'product_id', 'id');
    }


    public function related()
    {
        return $this->hasMany('App\Models\RelatedProduct', 'product_id', 'id');
    }

    public function features()
    {
        return $this->hasMany('App\Models\SelectedProductFeature');
    }


    public function frontImage()
    {
        return $this->hasMany('App\Models\UploadImage', 'file_id', 'id')->orderBy('order', 'ASC')->where('file_type', 'products');
    }

    public function selectedPlates()
    {
        return $this->hasMany('App\Models\SelectedPlates', 'product_id', 'id');
    }

}


class Plate extends Model
{
    use ScopeActiveTrait;

    protected $fillable = ['name', 'enable', 'price', 'vat_id', 'type', 'description'];
    protected $guarded = ['id'];
    public $timestamps = false;

    public function frontImage()
    {
        return $this->hasMany('App\Models\UploadImage', 'file_id', 'id')->orderBy('order', 'ASC')->where('file_type', 'plates');
    }

    public function vat()
    {
        return $this->belongsTo('App\Models\Vat', 'vat_id');
    }

}


class SelectedPlates extends Model
{
    use ScopeActiveTrait;

    protected $guarded = ['id'];
    protected $fillable = ['plate_id', 'status', 'maxvalue', 'minvalue', 'product_id'];

    public function plate()
    {
        return $this->belongsTo('App\Models\Plate', 'vat_id');
    }
}

SelectedPlates - 分配给产品/板柜

盘子 - 盘子

产品 - 橱柜

我需要分配给给定产品/机柜的所有板的可搜索显示

我做这个功能:

public function getPlates(string $query, int $type, int $productId)
{
Product::active()->with(['vat', 'frontImage', 'selectedPlates'])->where(function ($q) use ($query, $type){
            if ($query != "") {
                $q->where('name', 'LIKE', '%' . $query . '%');
                $q->orWhere('description', 'LIKE', '%' . $query . '%');
            }
        })->orderBy('name', 'asc')->get()
}

这段代码给我 2 个盘子:

Illuminate\Database\Eloquent\Collection {#1669 ▼
  #items: array:1 [▼
    0 => App\Models\Product {#1605 ▼
      #fillable: array:14 [▶]
      #guarded: array:1 [▶]
      +timestamps: false
      #connection: "mysql"
      #table: "products"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:16 [▶]
      #original: array:16 [▶]
      #changes: []
      #casts: []
      #classCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: array:3 [▼
        "vat" => App\Models\Vat {#1640 ▶}
        "frontImage" => Illuminate\Database\Eloquent\Collection {#1606 ▶}
        "selectedPlates" => Illuminate\Database\Eloquent\Collection {#1642 ▼
          #items: array:2 [▼
            0 => App\Models\SelectedPlates {#1702 ▶}
            1 => App\Models\SelectedPlates {#1703 ▶}
          ]
        }
      ]
      #touches: []
      #hidden: []
      #visible: []
    }
  ]
}

现在我需要通过以下方式在这个板块中搜索:

if ($query != "") {
                $q->where('plates.name', 'LIKE', '%' . $query . '%');
                $q->orWhere('plates.description', 'LIKE', '%' . $query . '%');
            }

并过滤

->where(function ($q) use ($type) {
                $q->where('plates.type', $type);
                $q->orWhere('plates.type', 3);
            })

像这样的东西:

public function getMaterials(string $query, int $type, int $produktId)
    {
        return Product->active()->with(['vat', 'frontImage'])->where(function ($q) use ($query, $type) {
            if ($query != "") {
                $q->where('plates.name', 'LIKE', '%' . $query . '%');
                $q->orWhere('plates.description', 'LIKE', '%' . $query . '%');
            }
        })
            ->where(function ($q) use ($type) {
                $q->where('plates.type', $type);
                $q->orWhere('plates.type', 3);
            })
            ->orderBy('name', 'asc')->get();
    }

上面的代码不起作用。不搜索/过滤。我怎样才能做到?

标签: phplaravel

解决方案


您需要更改几行代码才能完成这项工作。plates首先为然后添加关系,例如:

public function getMaterials(string $query, int $type, int $produktId)
    {
        return Product->active()->with(['selectedPlates', 'selectedPlates.plate'])
            ->whereHas('selectedPlates', function ($q) use ($query, $type) {
                if (!empty($query)) {
                    $q->where('name', 'LIKE', '%' . $query . '%');
                    $q->orWhere('description', 'LIKE', '%' . $query . '%');
                }
                
                $q->whereHas('plate', function($q) use ($type) {
                    $q->where('type', $type ?: 3);
                }
            })
            ->orderBy('name', 'asc')->get();
    }

whereHashttps://stackoverflow.com/a/30232227/3740460上了解更多信息


推荐阅读