blasttech/eloquent-related-plus

为 Laravel Eloquent 相关模型添加搜索和排序功能

1.0.1 2021-04-13 16:10 UTC

README

Latest Version Build Status Software License StyleCI Quality Score Total Downloads

此包提供了一种特质,它为复杂模型添加了额外的排序和搜索方式。在使用 Eloquent 时(例如,HasOne、HasMany、BelongsTo),主模型无法使用相关表中的字段进行排序。

例如,您可能有一份客户列表,这些客户与一个一对一的关系(HasOne)关联到一个 Contacts 模型,用于客户联系。

class Customer extends Eloquent
{
    public function sales_rep()
    {
        return $this->hasOne(Contact::class, 'id', 'sales_rep_id');
    }
}

如果您有一个客户表,并想按联系名称排序,通常在 Laravel 中使用上述关系无法做到,但可以使用此包。

安装

此包可以通过 Composer 安装。

$ composer require blasttech/eloquent-related-plus

用法

要将复杂的排序和搜索行为添加到您的模型中,您必须

  1. 指定模型将符合 Blasttech\EloquentRelatedPlus\RelatedPlusInterface
  2. 使用特质 Blasttech\EloquentRelatedPlus\RelatedPlusTrait

使用先前的示例

use App\Contact;
use Blasttech\EloquentRelatedPlus\RelatedPlusInterface;
use Blasttech\EloquentRelatedPlus\RelatedPlusTrait;

class Customer extends Eloquent implements RelatedPlusInterface
{
    use RelatedPlusTrait;
    
    public function sales_rep()
    {
        return $this->hasOne(Contact::class, 'id', 'sales_rep_id');
    }
}

modelJoin

使用此作用域为相关模型添加一个连接。

  • modelJoin($relation_name, $operator = '=', $type = 'left', $where = false, $related_select = true)

示例

namespace App\Http\Controllers;

use App\Customer;

class CustomerController extends Controller
{
    public function getCustomers()
    {
        return Customer::select('*')
            ->modelJoin('Contacts', '=', 'left', false, false)
            ->get(); 
    }
    
    ...
}

这将使用 Contacts() 关系添加到 Contacts 模型的左连接,使用 'where' 而不是 'on',并包含模型中的所有字段在查询中。

$relation_name

关系的名称。只有 BelongsTo、HasOne、HasMany 或 HasOneOrMany 关系才会工作。

$operator

用于连接相关表的运算符。这将默认为 '=',但可以使用任何标准的 Laravel 连接运算符。

$type

连接类型。这将默认为 'left',但允许使用任何标准的 Laravel 连接类型。

$where

连接表的方法。默认(false)使用 'on' 语句,但如果是 true,则使用 where 语句。

$related_select

相关选择选项确定是否在查询中包含连接表中的字段。如果为 true,字段名称将采用格式 'table_name.column_name',例如 'customer.contact_name',其中表名和点(.)包括在字段名称中。这是为了允许使用具有相同列名的连接表中的字段。

orderByCustom

  • orderByCustom($orderField, $dir, $orderFields = null, $orderDefaults = null)

示例

use App\Customer;
use Blasttech\EloquentRelatedPlus\RelatedPlusInterface;
use Blasttech\EloquentRelatedPlus\RelatedPlusTrait;

class CustomerController extends Controller
{
    public function getCustomers()
    {
        return Customer::select('*')
            ->modelJoin('Contacts', '=', 'left', false, false)
            ->orderByCustom('contact_name'); 
    }
    
    ...
}

这将使用 Contacts() 关系添加到 Contacts 模型的左连接,然后按 contact_name 排序。

search

示例

use Blasttech\EloquentRelatedPlus\RelatedPlusInterface;
use Blasttech\EloquentRelatedPlus\RelatedPlusTrait;

class MyModel extends Eloquent implements RelatedPlusInterface
{
    use RelatedPlusTrait;
    
    public function getContact($contact_name)
    {
        return Customer::select('*')
            ->modelJoin('Contacts', '=', 'left', false, false)
            ->search($contact_name); 
    }
    
    ...
}

这将使用 Contacts() 关系添加到 Contacts 模型的左连接,并搜索 $contact_name。

许可证

MIT 许可证(MIT)。有关更多信息,请参阅许可证文件

鸣谢

http://laravel-tricks.com/tricks/automatic-join-on-eloquent-models-with-relations-setup 中显示的示例被用作模型Join 和 relationJoin 作用域的基础。