-1

Database name :- 'hdwallpapers'

Table :- 'images'

enter image description here

Table :- 'categories'

enter image description here

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

enter image description here

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!!!

Shailendra
  • 391
  • 1
  • 10
  • 17

3 Answers3

1

I don't recommend it for scalable projects, but you can disable ONLY_FULL_GROUP_BY mode (which is a constrain for MySQL to execute only queries which contain non ambiguous GROUP BY statements) from database configuration.

Go to /app/database.php at connections -> mysql, and append the following code to the end:

'mysql' => [
   'driver' => 'mysql',
   ....

   // append the following:
   'modes' => [
       //'ONLY_FULL_GROUP_BY',
       'STRICT_TRANS_TABLES',
       'NO_ZERO_IN_DATE',
       'NO_ZERO_DATE',
       'ERROR_FOR_DIVISION_BY_ZERO',
       'NO_AUTO_CREATE_USER',
       'NO_ENGINE_SUBSTITUTION',
   ]
]

Then execute php artisan config:cache

user8555937
  • 2,161
  • 1
  • 14
  • 39
0

just group by all the columns you have selected ...

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.cid','categories.category_name')->get();

OMR
  • 11,736
  • 5
  • 20
  • 35
0

In config\database.php --> "mysql" array

Set

'strict' => false

Details is here

Mr. Perfectionist
  • 2,605
  • 2
  • 24
  • 35