【问题标题】:Laravel orWherehas filtering relationship with multiple columnsLaravel orWherehas 过滤多列关系
【发布时间】:2021-08-07 13:02:05
【问题描述】:

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

我想显示所有带有收件人信息的发票,其中有一个外键以使这成为可能,我的模型也有关系 前端的数据显示成功,但是当我尝试过滤它时显示错误 详情:...

有两个表:

收件人:

id (PRIMARY KEY) 自增,外键

名字

客户号码

.../(没必要)

发票:

invoice_nr(主键)自动增量

recipient_id -> 外键 {with id of recipients}

我的模型也有关系:

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
            
         ]);

    


    }

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

在 Frontend .. .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,
      }
}
}

【问题讨论】:

  • 因为你调用了get() 方法所以它抛出错误。所以更改如下代码 $q = Invoices::with('recipients');对于使用where 条件,$q 必须返回查询构建器实例。
  • 如果我不使用 get()->all();来自表的数据不会出现在前端 vue 上!
  • 这里调用 get() ... 'invoices' => $q->get()
  • column not found name....它显示我的错误,找不到关系列...
  • 尝试改变 if(request('search')){ $search_keyword = "%".request('search')."%"; $q ->where(function ($query) use ($search_keyword){ $query->orWhere('invoices_nr', 'LIKE', $search_keyword); })->whereHas('recipients',function ($query)使用($search_keyword){ $query->orwhere('name', 'LIKE', $search_keyword) ->orwhere('customer_number', 'LIKE', $search_keyword) ->orwhere('created_at', 'LIKE', $search_keyword);; }); };

标签: php laravel vue.js inertiajs


【解决方案1】:

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

提示:如果你想过滤多个关系列和父表,这是一个解决方案,在 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
                            });
                        });
                });
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-20
    • 2016-10-15
    • 1970-01-01
    • 2014-01-15
    • 2020-12-17
    • 2015-05-17
    • 1970-01-01
    • 2018-04-19
    相关资源
    最近更新 更多