这是我第一次在whereHas
中使用Laravel
。我用它来搜索我的桌子。这是我用于搜索的查询。
$term = $request->query;
$category = Category::whereHas("contacts", function($query) use($term){
$query->where("name", "like", "%".$term."%");
})->orWhere('name','LIKE','%'.$term.'%')->orderBy("id", 'desc')->get();
但它返回错误
Object of class Symfony\Component\HttpFoundation\ParameterBag could not be converted to string
这是我的Category
型号
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
/**
*
*/
class Category extends Model{
protected $fillable = ['name'];
public function contacts(){
return $this->hasMany("App\Contact");
}
}
这是我的Contact
型号
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");
}
}