Laravel orWhere与多列有过滤关系

Laravel orWherehas filtering relationship with multiple columns

我遇到了搜索过滤问题, 项目使用 Laravel、Inertia js、Vue js

我想显示所有带有收件人信息的发票,上面有一个外键来实现这一点,而且我在我的模型上有关系 前端数据显示成功,但当我尝试过滤时显示错误 详情:...

有两个表:

收件人:

id(PRIMARY KEY)自增,外键

名字

customer_number

.../(不需要)

发票:

invoice_nr(主键)自动递增

recipient_id -> 外键{收件人ID}

我的模特也有关系:

Invoices.php型号

//relationship
public function Recipients()
    {
        return $this->belongsTo(Recipients::class,'recipient_id','id');
    }

Recipients.php型号

//relationship
public function invoices()
    {
        return $this->hasMany(Invoices::class);
    }

前端的数据都很好,但是当我尝试搜索过滤时,它向我显示了这个错误:

这是我的发票控制器代码函数show_Invoices


use App\Models\Invoices;
use App\Models\InvoicesDetails;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Carbon;
use Inertia\Inertia;
use App\Models\Recipients;
use Illuminate\Support\Str;
use DB;

 public function show_invoices(){


        $q = Invoices::with('recipients')->get()->all();
    
            if(request('search')){
                $search_keyword = "%".request('search')."%";
                $q ->where(function ($query) use ($search_keyword){

                    $query->where('invoice_nr', 'LIKE', $search_keyword)
                          ->orwhere('recipients.name', 'LIKE', $search_keyword)
                          ->orwhere('recipients.customer_number', 'LIKE', $search_keyword)
                          ->orwhere('recipients.created_at', 'LIKE', $search_keyword);
                });

              
           };
  
        request()->validate([
            'direction' =>['in:asc,desc'],
            'field' =>['in:invoices.invoice_nr,recipients.name,recipients.customer_number,recipients.created_at']
        ]);
        

        if(request()->has(['field','direction'])){
            $test->orderBy(request('field'), request('direction'));
        }


        return Inertia::render('Show_Invoices', [
            'filters' =>request()->all(['search','field','direction']),
            'invoices' => $q
            
         ]);

    


    }

来自后端的数据成功地出现在发票对象上,但是当我尝试搜索时它不起作用

在前端 ...vue 脚本这里是

<script>

import BreezeAuthenticatedLayout from '@/Layouts/Authenticated'
import { pickBy, throttle } from 'lodash';
import Pagination from '@/Components/Pagination'


export default {
metaInfo: { title: 'Invoices' },

components: {
BreezeAuthenticatedLayout,
Pagination,
},

props: {
  invoices: Object,
  filters: Object,
},

data() {
  return {
    params:{
      search:this.filters.search,
      field:this.filters.field,
      direction:this.filters.direction,
    }
  }
},

methods:{
  sort(field){
    this.params.field = field;
    this.params.direction = this.params.direction === 'asc' ? 'desc' : 'asc';
  }
},
watch:{
      params:{
          handler:throttle(function(){
            let params = pickBy(this.params);
              this.$inertia.get(this.route("invoices.show"), params , {replace: true, preserveState: true});
          },150),
          deep:true,
      }
}
}

在 John Lobo 的帮助下,我自己找到了解决问题的方法。

提示:如果你想过滤多列关系和父级 table,这是一个解决方案,在 laravel 中,它有点嵌套,但如果你有相同的,它是一个解决方案我有问题。

 $q = Invoices::with('recipients');

        if(request('search')){
                $search_keyword = request('search');

                $q->where(function ($q) use ($search_keyword) {
                        $q->where('invoice_nr', 'like', '%'.$search_keyword.'%')
                        ->orwhereHas('recipients', function ($subq) use ($search_keyword) {
                            $subq->where(function ($subq2) use ($search_keyword) {
                                $subq2->orWhere('name', 'like', '%'.$search_keyword.'%');
                                $subq2->orWhere('customer_number', 'like', '%'.$search_keyword.'%'); 
             //you can access many columns as you want
                            });
                        });
                });
        }