can not fetch records by using codeigniter 4 Model - php

i am creating restful api, i have created my model in codeignter 4 and in controller when i try to fetch record it gives me this error
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'IS NULL
ORDER BY `id` DESC' at line 3;
am fetching like in controller
$model = new PlacesModel();
$data['places'] = $model->orderBy('id', 'DESC')->findAll();
return $this->respond($data);
i did not mentioned any null or limit then why am getting this ?

Related

Laravel GroupBy work in mysql but throws error in MariaDB

I'm Running the following query without any problem in MySQL (5.7.21) Database but this query throws error on my hosting server database which is MariaDB (10.1.47-MariaDB) and I don't have super permission to change the sql mode.
$projects = V2Project::leftJoin('v2_project_locations as v2pl', 'v2pl.project_id', 'v2_projects.id')
->leftJoin('v2_locations as v2l', 'v2l.id', 'v2pl.location_id')
->selectRaw('v2_projects.id, v2_projects.name, group_concat(v2l.name) as locations')
->withDepartmentInMind()
->onlyAuthorizedObjects();
if($by_current_step == true) {
$projects = $projects->where('current_step', 'plan');
}
if($search_val) {
$projects = $projects->whereIn('v2_projects.id', $search_val);
}
$projects = $projects->groupBy('v2_projects.id')->paginate(10);
$projects->appends(Input::except('page', '_token'));
Error:
SQLSTATE[42000]: Syntax error or access violation: 1055 'pm_form_moe.v2_projects.name' isn't in GROUP BY (SQL: select v2_projects.id, v2_projects.name from `v2_projects` group by `v2_projects`.`id`)
I also tried to add every column, but I need the functionality the same as mySQL without adding all columns.
Read about ONLY_FULL_GROUP_BY, then decide what to remove from your SELECT or what to add to the GROUP BY. Don't worry about setting the flag; your code is bad and needs to be fixed. And it was bad in the older version when it did not complain.
(If you show us the generated SQL, we can discuss it further.)

Where JSON in Laravel gives error SQLSTATE[42000]: check the manual that corresponds to your MariaDB server

I have tried to get data from database with laravel Where JSON. But it gives an error. I have added the data like this.
$event = new Event;
$event->scores = json_encode([['user_ids' => [1,2,3,4],'score' => 15]]);
$event->save();
When I want to return the data in database.
Event::where('scores->score',15)->get()
Shows this error:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an
error in your SQL syntax; check the manual that corresponds to your
MariaDB server version for the right syntax to use near '>'$."score"'
= ?' at line 1 (SQL: select * from `events` where `scores`->'$."score"' = 15)
My MariaDB version is 10.2.1
Array & JSON Casting
On your Event model you need to add
class Event extends Model {
protected $casts = ['scores'=>'array'];
// ...
}
and then when saving the data
$event = new Event;
// you had what appeared to be an extra array, that could cause the issue?
$event->scores = ['user_ids' => [1,2,3,4],'score' => 15];
$event->save();
Then the data will be saved as JSON automatically as long as the column type is TEXT or JSON(new versions of MySQL).
See Array & JSON casting on the Laravel Documentation: https://laravel.com/docs/5.5/eloquent-mutators#array-and-json-casting
Then to retrieve:
Event::where('scores->score', '15')->get();
Here is the relevant documentation for JSON Where clauses
https://laravel.com/docs/5.5/queries#json-where-clauses
Again, pretty confused as to what you are asking. Might help to see structure of the table or what the expected SQL should look like.
If you are trying to return rows based off values contained inside a string of JSON stored on the table... Then you need to do something like...
->whereRaw("JSON_CONTAINS(score, '[15]' )")->get();
Or something along those lines... https://dev.mysql.com/doc/refman/5.7/en/json-search-functions.html

Pagination not working in Laravel 5.1

I am trying to set pagination in Laravel 5.1, here is what I am trying to do:
$bridal_requests_data = \DB::table('bridal_requests')->leftJoin('audiences', function($join) {
$join->on('bridal_requests.id', '=', 'audiences.request_id');
})
->orderBy('bridal_requests.id', 'DESC')->paginate('15', array('bridal_requests.*'));
it is giving the following 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 '*) as aggregate from bridal_requests left join audiences on bridal_requests' at line 1 (SQL: select count(bridal_requests.*) as aggregate frombridal_requestsleft joinaudiencesonbridal_requests.id=audiences.request_id`)
As stated above, it is not working for bridal_requests.*, but if I use brial_requests.id then it works, but I need to use get all data from bridal_requests table.
You should use:
$bridal_requests_data = \DB::table('bridal_requests')->select('bridal_requests.*')->leftJoin('audiences', function($join) {
$join->on('bridal_requests.id', '=', 'audiences.request_id');
})
->orderBy('bridal_requests.id', 'DESC')->paginate(15);

How to call a model function insider another controller in cakephp

i have my controller function like this:
private function _send_refund_scores($userId,$scores,$orderId){
$this->loadModel('Score');
$rtn = $this->Score->refund_user_scores($userId,$scores,$orderId);
if(!empty($rtn)){
return true;
}else return false;
}
while in another model Score , I have model function refund_user_scores;
the error like this:
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
'refund_user_scores' at line 1
so,any idea?
I guess you have defined refund_user_scores() as a private function in model Score.
That is why you are getting this error.

How to call stored procedure on Laravel?

Error comes when i run update stored procedure in laravel5 like this..
QueryException in Connection.php line 620:
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 'Sandeep,09999999999,,,sandeep#gmail.com,,,,,,,)' at line 1 (SQL: call sp_clientupdate(108, Sandeep,09999999999,,,sandeep#gmail.com,,,,,,,))
my code is....
return DB::select('call sp_clientupdate(108, Sandeep,0999999999,,,sandeep#gmail.com,,,,,,,)');
Pls anyone give me solution.....
Try this one,
DB::statement('call sp_clientupdate("108", "Sandeep","0999999999","","","sandeep#gmail.com","","","","","","",)');
It works perfectly.
You change.
return DB::select('call sp_clientupdate("108", "Sandeep","0999999999","","","sandeep#gmail.com","","","","","","",)');
Try it's working perfectly....

Categories