Quick Tip: The Laravel API docs and eloquent fromSub

I have worked with a lot of frameworks whether it be PHP or JavaScript and hands down I feel that Laravel has one of the best documentation that you can read. 

But still you can’t find everything in the docs, that is nowhere a complaint, you can’t fit in every feature that the framework has into the docs and still keep it concise. Being concise on docs is important and Laravel does a great job of it. If you want you can go through the entire docs in 3 days of effort, that’s wonderful. 

Now how to go about knowing other features, functionality that the framework has? 

There are multiple ways: 

  1. If you have an IDE like PHPStorm it will help you with autocomplete and let you find relevant methods that are not in the docs.
  2. You can explore the source code. Which not everyone might be comfortable with. 
  3. Explore the Laravel API docs.

The Laravel API docs is what I refer to usually. It can be found here. Choose the version framework and you can quickly search for classes, methods etc. 

For example, once I wanted to have a union of two different tables and wanted to show the total number of rows. For that I had to select “from” the union query. You might not find something like this in the docs, but with a couple of searches and scrolling through the QueryBuilder I found fromSub and I was able to do what I wanted. 

Just give you some code here is how I used it. 

 

 /**
     * Get number of client sites that are using a particular source's assignments or directions
     *
     * @param int $sourceId id of the source
     * @return int
     */
    public function sitesUsingPackage($sourceId)
    {
        $assignments = DB::table('assignments')
            ->select('site_id')
            ->join('model_has_sites as mhs', function ($join) {
                $join->on('mhs.model_id', '=', 'assignments.id')
                    ->on('mhs.model_type', '=', DB::raw("'Assignment'"));
            })
            ->where('assignments.source_id', $sourceId)
            ->where('assignments.source_type', $this->sourceType)
            ->groupBy('mhs.site_id');

        $directions = DB::table('directions')
            ->select('site_id')
            ->join('model_has_sites as mhs', function ($join) {
                $join->on('mhs.model_id', '=', 'directions.id')
                    ->on('mhs.model_type', '=', DB::raw("'Direction'"));
            })
            ->where('directions.source_id', $sourceId)
            ->where('directions.source_type', $this->sourceType)
            ->groupBy('mhs.site_id');

        $sites = $assignments->union($directions);

        return DB::table('combined')->select(DB::RAW('count(*) as sites'))
            ->fromSub($sites, 'combined')->first()->sites;
}

So next time you are thinking whether or not this is something that Laravel does out of the box and do not find it in the docs, do look in the API docs and it might save you from reinventing the wheel.

Leave a Reply

Your email address will not be published. Required fields are marked *