Why does Laravel give error in SQL query? - laravel

If this query is executed in my database directly:
select * from `account_transactions`
where `AT_createuser` = 4 group by `AT_transactionficheno`
But this returns en error:
Transactions::with('type')->where('AT_createuser', JWTAuth::user()->id)
->groupBy('AT_transactionficheno')->get();
Error is:
SQLSTATE[42000]: Syntax error or access violation: 1055
'fee.account_transactions.AT_id' isn't in GROUP BY (SQL: select * from
account_transactions where AT_createuser = 4 group by
AT_transactionficheno)

try to go to config/database.php
on mysql > strict => false

Related

Query on a view ,Postgres and Laravel

I try to do a query on a view from DB, with Laravel but I get an error, and I am not sure why.
ViewData::query()->where('page', '=', '918');
"SQLSTATE[42601]: Syntax error: 7 ERROR: zero-length delimited
identifier at or near """"↵LINE 1: ...data" where "page" = $1 order by
"view_full_data"."" asc lim...↵
^ (SQL: select * from "view_full_data" where "page" = 918 order by
"view_full_data"."" asc limit 1000 offset 0)"
From Postgres, a working one.
Select * from view_full_data where "page = 918;
I found the problem.
From Maatwebsite\Excel I get an order_by that is not ok (Maybe because I don't have an ID column).
I had to add orderBy manually.
ViewData::query()->where("page", '=', "918")->orderBy('page', 'asc');

in sql the following query is given my desired output but in laravel model how can I retrieve it?

sql query is
select *,sum(diposit_amount) as total from `diposits` group by `member_id`;
I am trying in model like this way
App\diposit::groupBy('member_id')
->selectRaw('*,sum(diposit_amount) as total')
->get();
but it gives errors "SQLSTATE[42000]: Syntax error or access violation: 1055 'pos.diposits.id' isn't in GROUP BY (SQL: select *,sum(diposit_amount) as total from diposits group by member_id) (View: C:\xampp\htdocs\pos\resources\views\welcome.blade.php) "

I Have Database Errors on my Codeigniter

i receive the following errors when i try to login to my project, Please Help
A Database Error Occurred
Error Number: 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 'WHERE `_id` = '1'' at line 2
SELECT * WHERE `_id` = '1'
Filename: views/backend/header.php
Line Number: 34
...........................................................................
Here is the code line for the header file
..........................
<?php
$name = $this->db->get_where($this->session->userdata('login_type'),
array($this->session->userdata('login_type').'_id' => $this->session->userdata('login_user_id')))
->row()
->name;
echo $name;
A Database Error Occurred
Error Number: 1054
Unknown column '_id' in 'where clause'
UPDATE `ci_sessions` SET `timestamp` = 1519230907 WHERE `_id` = '1' AND `id` = 'd25c0dcdaa9a86810d791b05ba53fe45b76a7bcd'
Filename: libraries/Session/drivers/Session_database_driver.php
Line Number: 243
You didn't define from which table you have to fetch data, that's why this error comes. Normally we write query like
SELECT * FROM `table_name` WHERE `_id` = 1
you just the missed out selecting table. In codeigniter form the query should like
$this->db->select('*');
$this->db->from('table_name');
$this->db->where('_id', '1');
$data = $this->db->get();
This is the normal procedure to fetch data in codeigniter.
It's better to add manually column and table name. And value store in variable
$id = $this->session->userdata('login_user_id');
$name = $this->db->get_where('table-name',array('column-name' => 'your-value'))->row();
$echo $name->column-name; //Using this code you can echo all your data retrieved from database

Laravel morph query join relationship

I need to do a join statement on a query that has a morph relationship and I need to select the first image row and a field which is images.small but I'm getting the following error.
Query:
Movie::select(['id', 'images.small as poster', 'title', 'release_date', 'created_at', 'updated_at'])->join('images', function($join) {
$join->on('images.imageable_id', '=', 'movie.id')
->where('images.imageable_type', '=', 'App\Movie')->first();
})
Error:
(2/2) QueryException
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 'on images.imageable_id = movie.id and images.imageable_type = ? limi' at line 1 (SQL: select * on images.imageable_id = movie.id and images.imageable_type = App\Movie limit 1)

Laravel query builder how to write orderBy query having CAST and REPLACE functions

i have a query like this
SELECT area FROM history_cost_estimation ORDER BY CAST(REPLACE(area, ' ', '') AS INT)
i'm trying to translate such query into laravel query builder but i got many errors
this is my try
App\Models\HistoryCostEstimation::orderBy("CAST(REPLACE(area, ' ', '') AS INT)",'asc')->count();
and here is sample of the 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 'as `)` asc' at line 1 (SQL: select count(*) as aggregate from `engineertec`.`history_cost_estimation` order by `CAST(REPLACE(area,` as `)` asc)
I would use the DB facade and use raw mysql
make sure to add the DB facade to the controller or wherever you are running this
use Illuminate\Support\Facades\DB;
then do
DB::table('history_cost_estimation')->select('area')->orderBy(DB::raw('CAST(REPLACE(area, ' ', ''), 'INT')));

Resources