Php 为什么Laravel说“将nvarchar值“[]”转换为int数据类型时转换失败”

Php 为什么Laravel说“将nvarchar值“[]”转换为int数据类型时转换失败”,php,sql-server,laravel,laravel-5,Php,Sql Server,Laravel,Laravel 5,我正在用Laravel编写一个搜索函数,它抛出以下错误:Connection.php第651行中的QueryException: SQLSTATE[22018]:[Microsoft][ODBC驱动程序11 for SQL Server][SQL Server]将nvarchar值“[]”转换为int数据类型时失败。SQL:从产品中选择*,其中pID=[] 我的控制器内容如下: public function productSearch(Request $request){ $s

我正在用Laravel编写一个搜索函数,它抛出以下错误:Connection.php第651行中的QueryException: SQLSTATE[22018]:[Microsoft][ODBC驱动程序11 for SQL Server][SQL Server]将nvarchar值“[]”转换为int数据类型时失败。SQL:从产品中选择*,其中pID=[]

我的控制器内容如下:

public function productSearch(Request $request){
        $searchResult;
        if($request->input('category') == 'all' ){
            $searchResult = Product::where("pCategory", "like", '%')
                ->where('pName', 'like', $request->input('search'))->get();
        }else{
            $searchResult = Product::where("pCategory", "like", $request->input('category'))
                ->where('pName', 'like', $request->input('search'))->get();
        }
        //dd($searchResult);
        return view('products',['products' => $searchResult]);
    }
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $table = 'Product';
    protected $primaryKey = 'pID';
    //

    public function orderDetails(){
        return $this->hasMany('App\OrderDetail','ProductID','pID');
    }

}
模型内容如下:

public function productSearch(Request $request){
        $searchResult;
        if($request->input('category') == 'all' ){
            $searchResult = Product::where("pCategory", "like", '%')
                ->where('pName', 'like', $request->input('search'))->get();
        }else{
            $searchResult = Product::where("pCategory", "like", $request->input('category'))
                ->where('pName', 'like', $request->input('search'))->get();
        }
        //dd($searchResult);
        return view('products',['products' => $searchResult]);
    }
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $table = 'Product';
    protected $primaryKey = 'pID';
    //

    public function orderDetails(){
        return $this->hasMany('App\OrderDetail','ProductID','pID');
    }

}

首先,表中没有pCategory列

第二,当您收到all in category参数时,不要应用like子句进行搜索

public function productSearch(Request $request){
    $searchResult = new Product;

    if($request->has('search') && !empty($request->get('search')) && $request->get('search') !== 'all') {
        $searchResult = $searchResult->where("pName", "like", '%'.$request->get('search'));
    }

    $searchResult = $searchResult->get();
    return view('products',['products' => $searchResult]);
}

请参见将int值作为字符串插入数据库。我的意思是int变量在单引号内。你必须移除它