Php 将SQL转换为雄辩的关系

Php 将SQL转换为雄辩的关系,php,mysql,laravel,eloquent,Php,Mysql,Laravel,Eloquent,我有以下表格: items (id, name, description, subcategory_id, sublocation_id) categories (id, name) subcategories (id, name, category_id) locations (id, name) sublocations (id, name, location_id) 我正在尝试为laravel 5.4应用程序实现搜索功能。我使用以下用户输入来运行搜索:1)搜索字符串2)类别ID 3)位置

我有以下表格:

items (id, name, description, subcategory_id, sublocation_id)
categories (id, name)
subcategories (id, name, category_id)
locations (id, name)
sublocations (id, name, location_id)
我正在尝试为laravel 5.4应用程序实现搜索功能。我使用以下用户输入来运行搜索:1)搜索字符串2)类别ID 3)位置ID

但是,这三个输入位于3个不同的表中。因此,在传统SQL中,要获取所有具有category_id=1和location_id=1的项,我应该编写如下语句:

SELECT * FROM `items` WHERE name LIKE 'A%' AND subcategory_id IN (SELECT id FROM sub_categories WHERE category_id = '1') AND sublocation_id IN (SELECT id FROM sub_locations WHERE location_id = '1')
型号:

SubCategory.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class SubCategory extends Model
{
    protected $fillable = [
      'name', 'category_id',
    ];

    public function category(){
        return $this->belongsTo(Category::class);
    }
}
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    protected $fillable = [
      'name',
    ];

    public function subcategories(){
        return $this->hasMany(SubCategory::class);
    }
}

我建议您使用Algolia实现搜索功能:

以下是larvel 5.4的一些教程:

Algolia将为您提供大量的过滤选项,并将生成超级高效的查询:)


如果您尝试过这个,并且有任何疑问,请告诉我

我建议您使用Algolia实现搜索功能:

以下是larvel 5.4的一些教程:

Algolia将为您提供大量的过滤选项,并将生成超级高效的查询:)


如果您尝试过这个,并且有任何疑问,请告诉我

您可以通过以下方式构建相同的查询:

$subCategories = SubCategory::where('category_id', 1)->select('id');
$subLocations  = SubLocation::where('location_id', 1)->select('id');
$items = Item::where('name', 'like', 'A%')
    ->whereIn('subcategory_id', $subCategories)
    ->whereIn('sublocation_id', $subLocations);
也可以用一个表达式来表示:

$items = Item::where('name', 'like', 'A%')
    ->whereIn('subcategory_id', SubCategory::where('category_id', 1)->select('id'))
    ->whereIn('sublocation_id', SubLocation::where('location_id', 1)->select('id'));
这不是最有效的方法,但相当简单

在纯SQL中,我将使用联接,而不是在条件中:

select i.*
from items i
join subcategories sc on sc.id = i.subcategory_id
join sublocations  sl on sl.id = i.sublocation_id
where i.name like 'A%'
  and sc.category_id = 1
  and sl.location_id = 1

您可以通过以下方式生成相同的查询:

$subCategories = SubCategory::where('category_id', 1)->select('id');
$subLocations  = SubLocation::where('location_id', 1)->select('id');
$items = Item::where('name', 'like', 'A%')
    ->whereIn('subcategory_id', $subCategories)
    ->whereIn('sublocation_id', $subLocations);
也可以用一个表达式来表示:

$items = Item::where('name', 'like', 'A%')
    ->whereIn('subcategory_id', SubCategory::where('category_id', 1)->select('id'))
    ->whereIn('sublocation_id', SubLocation::where('location_id', 1)->select('id'));
这不是最有效的方法,但相当简单

在纯SQL中,我将使用联接,而不是在条件中:

select i.*
from items i
join subcategories sc on sc.id = i.subcategory_id
join sublocations  sl on sl.id = i.sublocation_id
where i.name like 'A%'
  and sc.category_id = 1
  and sl.location_id = 1
用于按关系约束

假设您在
项上有以下关系

    class Item extends Model
    {
        public function subcategories()
        {
            return $this->belongsTo(SubCategory::class);
        }

        public function sublocation()
        {
            return $this->belongsTo(SubLocation::class);
        }
    }
然后你可以做:

Item::where('name', 'LIKE', 'A%')
    ->whereHas('subcategories', function($query) use ($categoryId) {
        return $query->where('category_id', $categoryId);
    })
    ->whereHas('sublocation', function($query) use ($locationId) {
        return $query->where('location_id', $locationId);
    })
    ->get();
为了更进一步,您还可以在
类上实现,例如:

    class Item extends Model
    {
        //Item Class

        public function scopeWhereCategory($query, $categoryId)
        {
            return $query->whereHas('subcategories', function($query) use ($categoryId) {
                return $query->where('category_id', $categoryId);
            });
        }

        public function scopeWhereSublocation($query, $locationId)
        {
            return $query->whereHas('sublocation', function($query) use ($locationId) {
                return $query->where('location_id', $locationId);
            });
        }
    }
那么您的查询将是:

Item::where('name', 'LIKE', 'A%')
    ->whereCategory($categoryId)
    ->whereSublocation($locationId)
    ->get();
用于按关系约束

假设您在
项上有以下关系

    class Item extends Model
    {
        public function subcategories()
        {
            return $this->belongsTo(SubCategory::class);
        }

        public function sublocation()
        {
            return $this->belongsTo(SubLocation::class);
        }
    }
然后你可以做:

Item::where('name', 'LIKE', 'A%')
    ->whereHas('subcategories', function($query) use ($categoryId) {
        return $query->where('category_id', $categoryId);
    })
    ->whereHas('sublocation', function($query) use ($locationId) {
        return $query->where('location_id', $locationId);
    })
    ->get();
为了更进一步,您还可以在
类上实现,例如:

    class Item extends Model
    {
        //Item Class

        public function scopeWhereCategory($query, $categoryId)
        {
            return $query->whereHas('subcategories', function($query) use ($categoryId) {
                return $query->where('category_id', $categoryId);
            });
        }

        public function scopeWhereSublocation($query, $locationId)
        {
            return $query->whereHas('sublocation', function($query) use ($locationId) {
                return $query->where('location_id', $locationId);
            });
        }
    }
那么您的查询将是:

Item::where('name', 'LIKE', 'A%')
    ->whereCategory($categoryId)
    ->whereSublocation($locationId)
    ->get();

Laravel还具有与Algolia集成的搜索功能。它的名字叫.Laravel,它有一个与Algolia集成的搜索功能。它叫。谢谢,但是你认为哪种方法是处理这种情况最有效的方法?正如你可能已经猜到的那样,我是拉威尔的新手。@Eisenheim-我会用纯SQL编写一个连接查询。谢谢,但是你认为哪种方法是处理这种情况最有效的方法?正如您可能已经猜到的那样,我是拉威尔的新手。@艾森海姆-在普通SQL中,我会编写一个连接查询。未定义的变量:categoryIdOk我的道歉,我忘记在whereCategory和whereSublocation方法中传递值定义的变量:categoryIdOk我的道歉,我忘记在whereCategory和whereSublocation方法中传递值