sql - Laravel Eloquent to join table and count related -
how use join eloquent taking in consideration following table structure:
i have properies table
--------------------- id | name --------------------- 1 | property name
than have rooms
---------------------- roomid | property ---------------------- a-212 | 1 ---------------------- f-1231 | 1
here property foreign key want properties , count how many rooms have each
the query retrives looks like
class propertiesrepository extends eloquentbaserepository implements propertiesinterface { use taggablerepository; /** * construct * @param properties $properties */ public function __construct( properties $properties ) { $this->model = $properties; } /** * properties joining rooms * @return properties */ public function getall() { return $this->model->get(); } }
how extend query desired result?
$this->model->leftjoin('rooms', 'properties.id', '=', 'rooms.property') ->selectraw('properties.*, count(rooms.roomid) roomscount') ->groupby('properties.id') ->get();
Comments
Post a Comment