vendredi 29 avril 2016

Why is Laravel saying "Conversion failed when converting the nvarchar value '[]' to data type int"

I'm writing a search function in Laravel, and it throws the following error: QueryException in Connection.php line 651: SQLSTATE[22018]: [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Conversion failed when converting the nvarchar value '[]' to data type int. (SQL: select * from Product where pID = []).

My controller reads as follows:

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

And the model reads as follows:

<?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');
    }

}

I don't understand why it keeps doing this, especially since I am not asking it to look at the ID. What is going on here?



via Chebli Mohamed

1 commentaire:

  1. Laravel doesn't like nvarchar columns in an SQL Server table. I presume you have one in your Product table, so either remove it or change it to a varchar.

    RépondreSupprimer