Laravel morph query join relationship - laravel

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)

Related

how to join 3 tables using laravel eloquent?

my table
Code:
function viewPDF()
{
$reports = Report::join('president_report', 'reports.id', '=', 'president_report.report_id')
->join('president_report', 'presidents.id', '=', 'president_report.president_id')->
select('reports.*')->where('president_report.report_id')
->filter()->latest()->get();
$pdf = PDF::loadView('reports.test1', ['reports' => $reports]);
return $pdf->stream('reports.pdf');
}
Error:
SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique
table/alias: 'president_report' (SQL: select * from reports inner
join president_report on reports.id =
president_report.report_id inner join president_report on
presidents.id = president_report.president_id where
president_report.report_id is null order by created_at desc)
Use separate aliases for the two joins with the president_reports table:
function viewPDF() {
$reports = Report::join('president_report pr1', 'reports.id', '=', 'pr1.report_id')
->join('president_report pr2', 'presidents.id', '=', 'pr2.president_id')->
select('reports.*')->where('pr1.report_id')
->filter()->latest()->get();
$pdf = PDF::loadView('reports.test1', ['reports' => $reports]);
return $pdf->stream('reports.pdf');
}
Note that the second join looks suspicious to me, because I don't see where the presidents table is getting included in the join query. But the general solution to use problem is to alias president_report differently for the two joins.

Why does Laravel give error in SQL query?

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

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) "

MySql Query error in Laravel

I am using laravel 5.4. When trying to run
select * from `users` inner join `addprojects` on `users`.`emp_id` = `addprojects`.`emp_id` where `emp_id` = $emp_id)"
It produces:
"SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'emp_id'
in where clause is ambiguous (SQL: select * from users inner join
addprojects on users.emp_id = addprojects.emp_id where
emp_id = $emp_id)"
$emp_id= Auth::user()->emp_id;
$projects_for_emp = DB::table('users')->join('addprojects', 'users.emp_id', '=', 'addprojects.emp_id')->where('emp_id', '$emp_id')->get();
emp_id in both tables , so can use users.emp_id or addprojects.emp_id
->where('users.emp_id', '=', $emp_id)
You have specified column name which exists in both tables, so use users.emp_id or addprojects.emp_id doesn't matter which one
Example:
$emp_id= Auth::user()->emp_id;
$projects_for_emp = DB::table('users')
->join('addprojects', 'users.emp_id', '=', 'addprojects.emp_id')
->where('users.emp_id', '$emp_id')
->get();
Because both users and addprojects tables have same named field "emp_id".
You can change your code like following.
$emp_id= Auth::user()->emp_id;
$projects_for_emp = DB::table('users')
->join('addprojects', 'users.emp_id', '=', 'addprojects.emp_id')
->where('users.emp_id', $emp_id)
->get();

Using a helper function to change a field of a query in Laravel 4 using DB:raw

I have a query function that I am using to pull some data from my database. I also have a function that changes around the "created_at" field to print it like so: Month Day YY'
Here is the query function:
public static function friend_activity_json($start = 0, $number_of_posts = 2) {
$friend_activity = DB::table('fanartists')
->join('fans', 'fanartists.fan_id', '=', 'fans.id')
->join('artists', 'fanartists.artist_id', '=', 'artists.id')
->orderBy('fanartists.created_at', 'DESC')
->select(DB::raw('StringEdit::getDate(fanartists.created_at) as created_at, fans.fbid, fans.first_name, fans.last_name, fans.gender, fans.city, fanartists.artist_id, artists.stage_name'))
->get();
The function is in the folder helpers, in a filed called "StringEdit.php". The function is getDate:
public static function getDate($date) {
$full_date = explode(" ", $date);
$date_pieces = explode("-", $full_date[0]);
$year = substr($date_pieces[0], -2);
$monthNum = $date_pieces[1];
$monthName = date("F", mktime(0, 0, 0, $monthNum, 10));
$final_date = $monthName ." ". $date_pieces[2] ." '".$year;
return $final_date;
}
I have been able to call this function elsewhere, so I know it works. How do I get it to work in this context to change around my "created_at" field? When running this I get 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 MySQL server version for the right syntax to use near '::getDate(fanartists.created_at) as created_at, fans.fbid, fans.first_name, fans' at line 1 (SQL: select StringEdit::getDate(fanartists.created_at) as created_at, fans.fbid, fans.first_name, fans.last_name, fans.gender, fans.city, fanartists.artist_id, artists.stage_name from `fanartists` inner join `fans` on `fanartists`.`fan_id` = `fans`.`id` inner join `artists` on `fanartists`.`artist_id` = `artists`.`id` order by `fanartists`.`created_at` DESC) (Bindings: array ( ))
EDIT: New 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 '"%M %d %y) as created_at, fans.fbid, fans.first_name, fans.last_name, fans.gende' at line 1 (SQL: select DATE_FORMAT(fanartists.created_at, "%M %d %y) as created_at, fans.fbid, fans.first_name, fans.last_name, fans.gender, fans.city, fanartists.artist_id, artists.stage_name from `fanartists` inner join `fans` on `fanartists`.`fan_id` = `fans`.`id` inner join `artists` on `fanartists`.`artist_id` = `artists`.`id` order by `fanartists`.`created_at` DESC) (Bindings: array ( ))
This won't work like the way you used it in your query here
DB::raw('StringEdit::getDate(fanartists.created_at) as created_at, fans.fbid, fans.first_name, fans.last_name, fans.gender, fans.city, fanartists.artist_id, artists.stage_name')
Here, getDate function is being treated as mysql function and it's not valid, anyways, you can use mysql's native DATE_FORMAT function instead, like
DATE_FORMAT(fanartists.created_at, "%M %d %y") as created_at
Check the documentation.

Resources