Database name :- 'hdwallpapers'
Table :- 'images'
Table :- 'categories'
Whenrever I run the following query it generates output shown in the image.
select categories.cid, categories.category_name, COUNT(images.category) as "Count" from categories LEFT JOIN images on categories.cid=images.category GROUP BY categories.category_name ORDER BY categories.category_name
Output
I've created Laravel App called "HDWallpapers" in Laravel version 5.8 and I'm using XAMPP and database is in MySQL and I'm using Visual Studio Code Editor
I've created separate models 'Categories' for table 'categories' and "Images" for table 'images'.
The Problem is that, that mysql query gives me the output I wanted but whenever I try that query in Laravel it generates Syntax Error or Access Violation.
Here is the laravel query that I tried but I am getting the same Syntax Error or Access Violation error
1) $categoryData=DB::select("SELECT categories.cid, categories.category_name, count(images.category) from 'categories' left join 'images' on categories.cid=images.category GROUP BY categories.category_name order by categories.category_name ASC");
2) $categoryData=DB::table('categories')->leftjoin('images','categories.cid','=','images.category')->select('categories.cid','categories.category_name', DB::raw("COUNT(images.category) as count"))->groupby('categories.category_name')->get();
3) $categoryData=Categories::leftjoin('images','categories.cid','=','images.category')->select('categories.cid','categories.category_name', DB::raw("count(images.category) as count"))->groupby('categories.category_name')->get();
I tried searching on many websites but because I'm new to laravel I couldn't understand what should I do, so please help me!!!