0

Hello guys i need to implement filtering result for Document model. My document model have relation with Customer and i need to filter that results with searching multiple columns in customer relation (name and address). I try with traditional way $document->customerRelation->name, customer.name and isn't working.

So i have HTML table with prited documents and there is also printed customer name and customer address. When i type in text box some keywords like (jon or test) i need to filter that columns.

Document Customer Address Document Type
00213 Jon Test Inovice
00214 Thomas Test Proforma
00215 Agly Test Guaranty
00216 Adams Test User manual

Here is code

    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
            ]

Model

// 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'

Ivan
  • 5,139
  • 11
  • 53
  • 86
  • Can you add migration file of documents – Wael Khalifa Nov 12 '22 at 10:02
  • You can see in dump structure. Migration is not important i have only two column in customer table what i need to search `name(varchar)` and `address(varchar)`. Also in dump you can see primary – Ivan Nov 12 '22 at 10:03
  • How come your `Customer` model has a `public function customer(): \Illuminate\Database\Eloquent\Relations\BelongsTo` relationship? Please share your relevant migration files ('documents', 'customers', 'products'). As it currently stands, we don't know how all these *'objects'* are related. Nonetheless, I think a combination of Eloquent queries and Query Builder `->join(...)` would do the trick. – steven7mwesigwa Nov 12 '22 at 10:09
  • Its already printed with relation. You can see model method on bottom. Also migration show full table structure. For this problem in dump is all showed what is important for this problem. In this case join is not requered becouse relation already works. By the way i slove problem and post it. Thanks for your time bro. By – Ivan Nov 12 '22 at 10:19
  • [Querying Relationship Existence](https://laravel.com/docs/9.x/eloquent-relationships#querying-relationship-existence) – steven7mwesigwa Nov 12 '22 at 10:32

1 Answers1

0

Problem is sloved using whereReklation

     ->when(
          $request->has('keywords'),
               function ($q) use ($request) {
                  return $q->whereRelation('customer', 'name', 'LIKE', '%'.$request->query('keywords') . '%');
})
Ivan
  • 5,139
  • 11
  • 53
  • 86