Reputation: 1671
I am able to search in some columns of my model Unit.
$units = Unit::where('name', 'LIKE', '%' . $query . '%')
->orWhere('address', 'LIKE', '%' . $query . '%')->paginate(15);
Unit belongs to a model named Property:
public function property()
{
return $this->belongsTo('App\Property');
}
How could I search in the attributes of the property?
I tried:
$units = Unit::where('name', 'LIKE', '%' . $query . '%')
->orWhere('address', 'LIKE', '%' . $query . '%')->paginate(15);
->orWhere('property.name', 'LIKE', '%' . $query . '%')->paginate(15);
but I get following error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'property.name' in 'where clause'
Upvotes: 1
Views: 1620
Reputation: 1366
Try this :
I assume that your tables are : units
and properties
and their primary keys are both id
and foreign key in units is : property_id
$units = Unit::select('units.*')
->where('units.name', 'LIKE', '%' . $query . '%')
->orWhere('units.address', 'LIKE', '%' . $query . '%')
->join('properties','properties.id','=','units.property_id')
->orWhere('properties.name', 'LIKE', '%' . $query . '%')->paginate(15);
Upvotes: 3