使用Laravel中的WHEN访问关系

17 浏览
0 Comments

使用Laravel中的WHEN访问关系

这个问题已经有答案了:

Laravel - Eloquent \"Has\", \"With\", \"WhereHas\" - What do they mean?

大家好,我需要对Document模型实现过滤结果功能。我的Document模型与Customer有关联,并且我需要通过在customer关联中搜索多个列(名称和地址)来过滤该结果。我尝试了传统的方式$document->customerRelation->namecustomer.name,但是没起作用。

所以我有一个带有打印文档的HTML表,其中还有打印的客户名称和客户地址。当我在文本框中键入一些关键字(如jon或test)时,我需要过滤这些列。

文档 客户 地址 文档类型
00213 Jon Test 发票
00214 Thomas Test 形式发票
00215 Agly Test 保证
00216 Adams Test 用户手册

这里是代码

    public function index(Request $request)
    {
       $documents = 
            Document::when(
                    $request->has('document_tip_id'), function ($q) use ($request) {
                        return $q->where('document_tip_id', $request->query('document_tip_id'));
                    }) // this working 
       // Here i need to filter by relation (customer.name or address and other columns)
         ->when(
            $request->has('keywords'), function ($q) use ($request) {
              return $q->where('customer.name', '%'.$request->query('customer') . '%');
           })
                ->with('products')
                ->with('customer')
                ->orderBy('created_at');
            $documents = $documents->paginate(20)->withQueryString();
            dd($documents);
            return ....
    }

Dump

Illuminate\Pagination\LengthAwarePaginator {#408 ▼ // app/Http/Controllers/Order/BuyerController.php:38
  #items: Illuminate\Database\Eloquent\Collection {#346 ▼
    #items: array:20 [▼
      0 => App\Models\Document {#321 ▼
        #connection: "mysql"
        #table: "dokument"
        #primaryKey: "id"
        #keyType: "int"
        #observables: []
        #relations: array:2 [▼
          "product" => Illuminate\Database\Eloquent\Collection {#343 }
          "customer" => App\Models\Customer {#432 ▼
            #connection: "mysql"
            #table: "customer"
            #primaryKey: "id"
            #keyType: "int"
            +incrementing: true
            #with: []
            #withCount: []
            +preventsLazyLoading: false
            #perPage: 15
            +exists: true
            +wasRecentlyCreated: false
            #escapeWhenCastingToString: false
            #attributes: array:28 [▼
              "id" => 975
              "code" => "123"
              "name" => "Jon Don"
              "address" => "Test address"
              "created_at" => "2020-11-05 21:55:46"
              "updated_at" => "2020-11-05 21:55:46"
              "deleted_at" => null
            ]

模型

// Customer.php
  public function customer()
  {
     return $this->belongsTo(Customer::class, 'subjekat_id');
  }

SQLSTATE [42S22]:Column not found: 1054 Unknown column \'customer.name\'

in \'where clause\'

admin 更改状态以发布 2023年5月24日
0
0 Comments

问题已经通过whereReklation解决了。\n

     ->when(
          $request->has('keywords'),
               function ($q) use ($request) {
                  return $q->whereRelation('customer', 'name', 'LIKE', '%'.$request->query('keywords') . '%');
})

0