lundi 18 janvier 2016

Laravel whereHas returning error

This is my first time of using whereHas in Laravel. I am using it to search my table. This is the query I have used for searching.

$term = $request->query;

$category = Category::whereHas("contacts", function($query) use($term){
    $query->where("name", "like", "%".$term."%");
})->orWhere('name','LIKE','%'.$term.'%')->orderBy("id", 'desc')->get();

But it returns error

Object of class Symfony\Component\HttpFoundation\ParameterBag could not be converted to string

This is my Category model

<?php 


namespace App;

use Illuminate\Database\Eloquent\Model;

/**
* 
*/
class Category extends Model{

    protected $fillable = ['name'];

    public function contacts(){
        return $this->hasMany("App\Contact");
    }

}

And this is my Contact model

class Contact extends Model
{
    use SoftDeletes;

    /**
     * The attributes that should be mutated to dates.
     *
     * @var array
     */
    protected $dates = ['deleted_at'];

    protected $fillable = ['name', 'phone', 'added_by', 'city', 'location', 'verified', 'category_id', 'bio'];

public function category(){
        return $this->belongsTo("App\Category");
    }

}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire