【发布时间】: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