如何解决未找到的列:1054基于此查询生成器的“字段列表”中的未知列“产品”?

时间:2019-03-19 08:02:06

标签: php mysql laravel

这不是重复的,所以这里是问题。

现在我正在一个项目上,我正在经历一些新的东西,这个项目是由某公司的某人开发的,他没有完成工作,所以我在这个应用程序中遇到了一个问题,当我尝试获取Excel输出时收到此错误。

  

找不到列:1054“字段列表”中的未知列“产品”(SQL:选择created_atfile_idnameorder,{{1} },paid_pricephonepriceproductsproducts_descproducts_order_descreagentsstatus, {{1}中的time_id)”

我以前曾经经历过,但是在这样的项目中没有经历过,所以这里是控制器,模型和刀片文件。

在代码之前,有3个按钮,其中1个将搜索并获取报告,该按钮正在工作,另一个是取消并导出到excel,这在excel中是有问题的,在excel部分中出现了此错误。

表格:

  • 产品:id,名称,desc,created_at,updated_at
  • product_customer:id,product_id,customer_id,created_at,updated_at
  • 试剂:id,名称,desc,created_at,updated_at
  • Reagents_customer:id,customer_id,agent_id,created_at,updated_at
  • 客户:id,time_id,file_id,名称,订单,product_desc,product_order_desc,电话,状态,价格,已付款价格,未付款价格,created_at,updated_at

Customer.php

unpaid_price

Product.php

customers

控制器

class Customer extends Model
{
protected $appends = ['jalali'];
protected $fillable=[
    'name','file_id','phone','quartz','artroz','available','order',
    'paid_price','unpaid_price','price','product_desc','order_desc',
    'products_order_desc','time_id','status','options',
];
// I've added products in fillable and didn't work.
protected $casts=[
    'order'=>'array'
];

public function reagents()
{
    return $this->belongsToMany('App\Reagent', 'reagent_customer', 'customer_id', 'reagent_id');
}
public function products()
{
    return $this->belongsToMany('App\Product', 'product_customer', 'customer_id', 'product_id');
}
public function orders()
{
    return $this->belongsToMany('App\Product', 'product_order_customer', 'customer_id', 'product_id');
}
}

这是他使用vue.js的刀片文件(问题在save_excel中):

protected $fillable=[
    'name','desc','type'
];

搜索响应:

class ReportController extends Controller
{
/**
 * show method for show report form
 */
public function show()
{
    return view('panel.report.report');
}
/**
 * show method for show report form
 */
public function p_show()
{
    // return toJalali(substr(1549202728000,0,10));
    // $v = verta();
    // return $v->timestamp(date(time()));
    // return dd($dt = $v->DateTime());
    // return   $c = Carbon::instance($v->DateTime());
    return view('panel.report.p_report');
}
/** 
 * handle paymanet report route
 */
public function handle_query_p(Request $request)
{
    $where=$this->c_query_builder();

    $start=Carbon::createFromTimestamp(substr($request->start_at,0,10))->hour(0)->minute(0)->second(0);
    $end=Carbon::createFromTimestamp(substr($request->end_at,0,10))->hour(23)->minute(59)->second(59);
    return [
       'customers' =>Customer::with(['products','reagents'])->whereBetween('created_at',[$start,$end])->where($where)->get(),
       'row_headers'=>$this->get_items_generator(),
       'db_query'=>$where,
    ];
}

public function save_as_excel()
{   
    $header=request()->row_headers;
    $q=request()->db_query;
    if(!request()->has('db_query') || count(request()->db_query) ==0 )
    {
        $q=[];
    }
    $customer=Customer::with('products')->where($q)->get($header);
    \Excel::store(new CustomerExport($customer,$header), 'export.xlsx','public');
    return Storage::url('export.xlsx'); 
}
public function save_as_excel_p()
{   
    $header=request()->row_headers;
    $q=request()->db_query;
    $start=Carbon::createFromTimestamp(substr(request()->start_at,0,10))->hour(0)->minute(0)->second(0);
    $end=Carbon::createFromTimestamp(substr(request()->end_at,0,10))->hour(23)->minute(59)->second(59);
    if(!request()->has('db_query') || count(request()->db_query) ==0 )
    {
        $q=[];
    }
    $customer=Customer::with('products')->whereBetween('created_at',[$start,$end])->where($q)->get($header);
    \Excel::store(new CustomerExport($customer,$header), 'export.xlsx','public');
    return Storage::url('export.xlsx'); 
}
public function handle_query(Request $request)
{
    $where=$this->c_query_builder();

    return [
       'customers' =>Customer::with(['products','reagents'])->where($where)->get(),
       'row_headers'=>$this->get_items_generator(),
       'db_query'=>$where,
    ];
}
public function get_items_generator()
{
    if(! request()->has('check_box'))
    {
        return [];
    }
    $items=[];
    foreach(request()->check_box as $key=>$item)
    {
        if($item!=false && $item!="false" )
        {
            $items[]=$key;
        }
    }
    return $items;
}
public function c_query_builder()
{
    $params=$this->get_params();
    $query=[];
    foreach($params as $key=>$param)
    {
        $query[]=[$key,'like',"%".$param."%"];
    }
    return $query;
}
public function get_params()
{
    $table_fileds=\Schema::getColumnListing('customers');
    $data=request()->all();
    foreach($data as $key=>$item)
    {
        if(!in_array($key,$table_fileds) ||  $item==null)
        {
            unset($data[$key]);
        }
    }
    return $data;
}
}

我将产品添加为可填充产品,但没有用。

刀片服务器和控制器<script> new Vue({ el:"#app", data:{ result:[], check_box:{ created_at:true, file_id:true, name:true, order:true, paid_price:true, phone:true, price:true, products:true, products_desc:true, products_order_desc:true, reagents:true, status:true, time_id:true, unpaid_price:true, } , name:null, created_at:null, file_id:null, order:null, paid_price:null, phone:null, price:null, products:null, products_desc:null, products_order_desc:null, reagents:null, status:null, time_id:null, unpaid_price:null, row_headers:null, e_time:null, s_time:null, db_query:null, }, methods:{ save_excel:function(){ let self=this; let start_at=$('#start_at').val(); let end_at=$('#end_at').val(); $.ajax({ type: "post", url: "{{route('report.save_as_excel_p')}}", data: { _token:"{{csrf_token()}}", row_headers :self.row_headers, db_query :self.db_query, 'end_at' :end_at, 'start_at' :start_at, }, success: function (response) { console.log(response); window.open(response); } }); }, query:function(){ let self=this; let start_at=$('#start_at').val(); let end_at=$('#end_at').val(); $.ajax({ type: "post", url: "{{route('report.handle_query_p')}}", data: { _token:"{{csrf_token()}}", name :self.name, created_at :self.created_at, file_id :self.file_id, order :self.order, paid_price :self.paid_price, phone :self.phone, price :self.price, products :self.products, products_desc :self.products_desc, products_order_desc :self.products_order_desc, reagents :self.reagents, status :self.status, time_id :self.time_id, unpaid_price :self.unpaid_price, check_box :self.check_box, 'end_at' :end_at, 'start_at' :start_at, }, success: function (response) { console.log(response); self.result=response.customers; self.row_headers=response.row_headers; self.db_query=response.db_query; } }); } }, 中的

customers: [{id: 1, time_id: 1, file_id: "40001", name: "c1", order: "[]", product_desc: null,…}] 0: {id: 1, time_id: 1, file_id: "40001", name: "c1", order: "[]", product_desc: null,…} created_at: "2019-02-28 11:13:31" file_id: "40001" id: 1 jalali: "1397-12-09 11:13:31" name: "c1" order: "[]" paid_price: "20000" paid_price2: 0 phone: "09167401933" price: "25000" product_desc: null products: [,…] 0: {id: 2, name: "p2", desc: "p2", created_at: "2019-02-28 11:08:48",…} products_order_desc: null reagents: [] status: "in_wait" time_id: 1 unpaid_price: "5000" updated_at: "2019-02-28 11:13:31" db_query: [] 方法导致此问题。

0 个答案:

没有答案