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

Popular posts from this blog

gridview - Yii2 DataPorivider $totalSum for a column -

java - Suppress Jboss version details from HTTP error response -

Sass watch command compiles .scss files before full sftp upload -