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?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/24r0zTL
via IFTTT

Aucun commentaire:

Enregistrer un commentaire