Laravel whereJsonContains query not working in 5.7 - php

I have been facing a problem in my project. I want to search my json data. But could not achieve to do this. Here is my query
$variations = User::whereJsonContains('addon->ram', '>', 0)->count();
return $variations;
And My json data is stored in the addon column and the data is
{"ram":"87","base-driven":"89"}
In this query I am getting error says
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an
error in your SQL syntax; (SQL: select count(*) as aggregate from
users where 0 json_contains(addon->'$."ram"', ">")
I google it but could not solve this
Thanks in advance

Just try this
$variations = User::where('addon->ram', '>', 0)->count();
return $variations;
and let time know, if you get some error.
As you are saving it as string, you may need to do something like
WHERE JSON_EXTRACT(addon, "$[ram]") > 0; in Laravel

It's because of you are providing "87" and "89" as strings. Try to replace it with numbers:
{"ram":87,"base-driven":89}

Related

How to Add OR Condition in Laravel Sphinx query?

I am using the fobia/laravel-sphinx package and want to add a condition like city != 0 or state != 0 but the or condition is not working and gives tjis error:
"Syntax error or access violation: 1064 sphinxql: syntax error,
unexpected OR, expecting $end near 'or "
$query->where('state_id', '!=', 0)->orWhere('city_id', '!=', 0);
It uses \Fobia\Database\SphinxConnection\Eloquent\Model
If you have a version of sphinx/manticore server that is not supporting OR conditions, have to 'fake' using a fake attribute/expression.
Need to add a column. Want SphinxQL somewhat like
SELECT *, (state_id!=0)+(city_id!=0) as filter FROM index WHERE filter > 0
... ie will only inlcude rows, where the sum of state_id and city_id is above zero. Ie either one of them is not zero.
Alas reading the laravel-sphinx source, can't figure out how to add to the columns list. Seems needs needs adding to $query->columns. So dont know if something like
$query->columns[] = "(state_id!=0)+(city_id!=0) AS filter";
$query->where('filter', '>', 0);
or maybe
$query->select(['*',"(state_id!=0)+(city_id!=0) AS filter"])->where('filter', '>', 0);
seems the there is a 'select' method in the base query builder
https://laravel.com/api/5.5/Illuminate/Database/Query/Builder.html
you cant use OR condition in Sphinx
you can handle it with whereNotIn and whereIn
These are the top rated real world PHP examples of yii\sphinx\Query::orWhere extracted from open source projects. You can rate examples to help us improve the quality of examples.
Please Visit and see example

How to fix column name is not in a groupBy laravel?

I am facing this error for the past 2-3 days. I hold this task to check this later in my free time. But still, I am getting the same error. Please help me to find the error. I am tired to check all solutions on google. But nothing works.
Error
Illuminate\Database\QueryException: SQLSTATE[42000]: Syntax error or access violation: 1055 'dating-app.messages.id' isn't in GROUP BY (SQL: select * from messages group by room_id having is_read = 0) in file C:\xampp\htdocs\dating-app\vendor\laravel\framework\src\Illuminate\Database\Connection.php on line 692
Here is my query
$messages = Message::groupBy('room_id')->having('is_read', 0)->get();
return response()->json(['status' => true, 'message' => $messages]);`
Any solution appreciated!
That's because any column you use in a select statement must appear in the group by clause. So select * and group by don't get along. Remove groupBy('room_id') in your relationship and see how that goes.
Alternatively, disable MySQL strict mode by going into config/database.php and setting strict => false for MySQL, if you are using MySQL version >5.7.
source

SQL command is not giving expected result in Laravel 5.7. Do I need to modify my table or my query?

I have my table data like below photo.
My expected data should be like this below photo.
My implemented laravel SQL code is like below.
$img = DB::table('table_name')
->select('table_name.user_id','table_name.post_id','table_name.name')
->groupBy('table_name.user_id')
->get();
This is how I'd do it. This way it's cleaner and working for me.
$img = DB::table('table_name')->select('user_id','post_id','name')
->groupBy('post_id')
->get();
$img = DB::table('table_name')
->select('user_id','post_id','name')
->groupBy('post_id')
->get();
If you're running into this SQLSTATE[42000]: Syntax error or access violation: 1055 error be sure to check that the groupBy parameter is a valid column on the table

Executing raw query in laravel 5.3

I am trying to execute some raw queries in laravel 5.3. the queries are simple but im getting errors.
table name: users
columns: id|name|email|phone|created_at|updated_at
my query:
SELECT created_at AS member_since, count(*) as row_count
FROM users
GROUP by MONTH(created_at);
This raw query works fine when I execute this on phpmyadmin. But when I execute this using laravel's database query builder i get error.
SQLSTATE[42000]: Syntax error or access violation: 1055
'query.users.created_at' isn't in GROUP BY
SQLSTATE[42000]: Syntax error or access violation: 1055
'query.users.created_at' isn't in GROUP BY (SQL: select created_at AS
member_since from `users` group by MONTH(created_at))
here is my controller:
$users = DB::table('users')
->select(DB::raw('created_at AS member_since', 'count(*) AS row_count'))
->groupBy(DB::raw('MONTH(created_at)'))
->get();
return response()->json($users);
Please correct me if Im wrong. Are there any better ways to execute raw queries?
First, you need to understand the error you're getting. This question is all about it https://stackoverflow.com/a/38551525/2474872
Now, with that in mind you have two choices
Disable only_full_group_bysetting.
Follow the indications provided
by the error message:
$users = DB::table('users')
->select(DB::raw('created_at AS member_since, count(*) AS row_count'))
->groupBy(DB::raw('MONTH(created_at)'), 'created_at')
->get();
groupBy(DB::raw('MONTH(created_at)'), 'created_at') can also be groupBy(DB::raw('MONTH(created_at), created_at')

CAKEphp Virtual field not working

Because i have two price columns in my table (subscription_discount and subscription_price). I need to find a way to find the lowest of the two and use it. The problem is that this only has to occur when subscription_discount is lower then subscription_price and when subscription_discount is not 0.00.
This is my code to create the virtual field in my model:
public $virtualFields = array(
'end_price' => "IF(Abonnement.subscription_discount IS NOT NULL OR Abonnement.subscription_discount < Abonnement.subscription_price, Abonnement.subscription_price), Abonnement.subscription_discount)"
);
Somehow this ends up in an error in my view:
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You
have an error in your SQL syntax; check the manual that corresponds to
your MySQL server version for the right syntax to use near '),
Abonnement.subscription_discount)) AS Abonnement__end_price FROM
`cake' at line 1
I don't understand what is going wrong here.
public $virtualFields = array(
'end_price' => "IF(Abonnement.subscription_discount IS NOT NULL OR Abonnement.subscription_discount < Abonnement.subscription_price, Abonnement.subscription_price>>>**)**<<<, Abonnement.subscription_discount)"
);
remove it

Categories