Laravel left join showing error on 'ON' multiple condition - laravel

When i tried using left join in laravel I am getting below error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column '$dat' in 'on clause' (SQL: select students.id, students.name, attendance_student.date from students left join attendance_student on students.id = attendance_student.student_id and attendance_student.date = $dat)
Basically I am trying to get attendance of all students on a particular date
My Code:
$dat = 2015-10-15;
$student = DB::table('students')
->leftJoin('attendance_student', function($join)
{
$join->on('students.id', '=', 'attendance_student.student_id');
$join->on('attendance_student.date', '=', $dat);
})
->select('students.id', 'students.name', 'attendance_student.date'
)
->get();
PLease help

Your issue: Variable scopes
Solution:
$dat = 2015-10-15;
$student = DB::table('students')
->leftJoin('attendance_student', function($join) use ($dat)
{
$join->on('students.id', '=', 'attendance_student.student_id');
$join->on('attendance_student.date', '=', $dat);
})
->select('students.id', 'students.name', 'attendance_student.date'
)
->get();
Explanation:
When accessing variables that were declared outside the scope of the lambda function (the function($join) one), make use of use statement to allow access to those variables inside the function.
Source: http://php.net/manual/en/functions.anonymous.php

Related

Add custom columns to select

I am trying to add custom row and column to all results. I am getting below 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 '.png as badge, 3.5 as rating from instructors as i inner join use' at line 1 (SQL: selecti.id,u.name,i.about,i.price_per_hour,u.profile_picture,https://www.pngitem.com/pimgs/m/575-5754595_why-bali-best-driver-driver-icons-hd-png.pngasbadge,3.5asratingfrominstructorsasiinner joinusersasuonu.id=i.user_idwhere 0 = 1 andgenderin (male) order byi.id` desc limit 10 offset 0)
The associated code:
public function findInstructors($instrutors, $gender, $offet, $fetch) {
return DB::table('instructors as i')
->select(
'i.id',
'u.name',
'i.about',
'i.price_per_hour',
'u.profile_picture',
'https://www.pngitem.com/pimgs/m/575-5754595_why-bali-best-driver-driver-icons-hd-png.png as badge',
'3.5 as rating'
)
->join('users as u', 'u.id', 'i.user_id')
->whereIn('i.id', $instrutors)
->whereIn('gender', $gender)
->offset($offet)
->limit($fetch)
->orderBy('i.id', 'desc')->get();
}
I would suggest to stick with Laravel Collection map() function when it comes to adding additional data to a collection.
return DB::table('instructors as i')
->select(
'i.id',
'u.name',
'i.about',
'i.price_per_hour',
'u.profile_picture', // Notice* column removed from here
'3.5 as rating'
)
->join('users as u', 'u.id', 'i.user_id')
->whereIn('i.id', $instrutors)
->whereIn('gender', $gender)
->offset($offet)
->limit($fetch)
->orderBy('i.id', 'desc')
->get()
->map(function($row) {
// Here you can add like this. Keep in mind, this will be common for all the rows.
$row['badge'] = 'https://www.pngitem.com/pimgs/m/575-5754595_why-bali-best-driver-driver-icons-hd-png.png';
return $row;
});
mysql understand
'https://www.pngitem.com/pimgs/m/575-5754595_why-bali-best-driver-driver-icons-hd-png.png as badge',
as tableName.columnNmae as alias
because of dot using in extra column name:
to avoid this behavior
you should use MySQL Backticks `` :
`https://www.pngitem.com/pimgs/m/575-5754595_why-bali-best-driver-driver-icons-hd-png.png as badge`,
`3.5 as rating`
more details in:
https://riptutorial.com/mysql/example/18445/backticks-usage

How to fix Laravel query builder where clause integer variable translated to string

I have a function to get a pass a language number to get language categories record for API purpose. I use a database query statement to select categories table and join the category language table to get category id, parent_id and name (specified language). When execute return error and select the underlying SQL converted the language value to string (e.g. languages_id = 1). I google a lot and no ideas what's wrong. Can anyone advise how to resolve. Thanks a lot.
I tried to copy the underlying SQL to MySQL Workbench and remove the languages_id = 1 --> languages_id = 1 can working properly. I guess the 1 caused error.
Code Sample:
private function getCategories($language) {
$categories = DB::table('categories')
->select(DB::raw('categories.id, categories.parent_id, categories_translation.name'))
->join('categories_translation', function($join) use ($language) {
$join->on('categories_translation.categories_id', '=', 'categories.id');
$join->on('categories_translation.languages_id', '=', $language);
})
->where([
['parent_id' ,'=', '0'],
['categories.id', '=', $id]
])
->get();
return $categories;
}
Error return the converted SQL:
"SQLSTATE[42S22]: Column not found: 1054 Unknown column '1' in 'on
clause' (SQL: select categories.id, categories.parent_id,
categories_translation.name from categories inner join
categories_translation on categories_translation.categories_id =
categories.id and categories_translation.languages_id = 1
where (parent_id = 0 and categories.id = 1))"
You are trying to join using a comparison to an scalar value, instead of a column. I think you actually want to put that comparison as a "where" condition, rather than a "join on"
->where([
['parent_id' ,'=', '0'],
['categories.id', '=', $id],
['categories_translation.languages_id', '=', $language]
])
there is another thing i just discover with your code. when joining table, you are suppose to be joining 'categories_translation.languages_id' with another table id field. in your case, it is not so. you are not joining 'categories_translation.languages_id' with any table field. so ideally, what you are going to do is this
private function getCategories($language) {
$categories = DB::table('categories')
->select(DB::raw('categories.id, categories.parent_id, categories_translation.name'))
->join('categories_translation', function($join) use ($language) {
$join->on('categories_translation.categories_id', '=', 'categories.id');
})
->where([
['parent_id' ,'=', '0'],
['categories.id', '=', $id]
['categories_translation.languages_id', '=', $language]
])
->get();
return $categories;
}
hope this helps

Adding Multiple Where Clause from Model in Laravel

I am trying to clean my code up, and working on the Models
I have the following 2 tables broken down like this:
Roll Table
|id|roll_id|member_id|.......
Members table
|id|first_name|last_name|rank|
I have the following on my Roll Model
public function member()
{
return $this->belongsTo('App\Member');
}
This on my Member model
public function roll()
{
return $this->hasMany('App\Roll');
}
While the following code does return the correct results
$roll = Roll::with(['member'])
->where('status', '!=', 'A')
->get();
return ($roll);
I would like to add an extra where clause
->where('rank','<', 12)
However, I get the following error
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'member.rank'
in 'where clause' (SQL: select * from Roll where roll_id = 4 and
status != A and `mem ▶"
You can use whereHas method to filter on the relations:
$roll = Roll::with(['member'])
->where('status', '!=', 'A')
->whereHas('member', function($query) {
$query->where('members.rank', '<', 12);
})
->get();
Hope this will resolve your issue.

Use Pivot Table to Fetch Data from Main Table

I am using two tables and a pivot table
Table 1 named calendars.
Table 2 named calendar_groups.
Pivot table calendar_calendar_group.
I'm trying to get data from Table 1 based on a where value in the pivot table. Where calendar_groups_id = 1 then use calendar_id to get data from table 1. I can't get it to work.
$event = new Calendar();
$event->orderBy('start', 'asc')
->whereHas('calendar_groups', function ($q) {
$q->wherePivot('calendar_groups_id', '=', '1');
})->with('calendar_groups')
->first();
This gives me the following error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'pivot' in
'where clause'
This is the relationship:
public function calendar_groups()
{
return $this->belongsToMany(CalendarGroup::class);
}
Your help is very much appreciated.
I think #Tpojka is right here. If you're trying to get Calendar instances that belong to desired CalendarGroup then replace the code should look like this:
$event = new Calendar();
$group = 1;
$event->orderBy('start', 'asc')
->whereHas('calendar_groups', function ($q) use ($group) {
//$q->wherePivot('calendar_groups_id', '=', $group);
$q->where('id', '=', $group);
})->with('calendar_groups')
->first();
If I'm reading the documentation correctly wherePivot() should be used like this:
$event = new Calendar();
$event->calendar_groups()->wherePivot('some_pivot_column',1)->get();
But this would return you the CalendarGroup instances.
If you'd want to do it through Eloquent but without going all the way to the CalendarGroup then you'd probably need to create a Model (let's call it CalendarCalendarGroupPivot) for the pivot table and add another relation (hasMany('CalendarCalendarGroupPivot')) to your Calendar model.
Ok finally got it working.
I used your suggestions but still don't understand a little part of it.
When I use the suggestions made:
$event = new Calendar;
$event->orderBy('start', 'asc')
->whereHas('calendar_groups', function($q) {
$q->where('calendar_group_id', '=', '1');
})->with('calendar_groups')
->first();
I get an empty collection.
But if I run this:
$event = Calendar::orderBy('start', 'asc')
->whereHas('calendar_groups', function($q) {
$q->where('calendar_group_id', '=', '1');
})->with('calendar_groups')
->first();
I get the desired results
Can anybody tell me the difference between so I can learn from it?

Convert a many to many raw sql statement to eloquent query builder

I need to translate this working sql statement:
select model_names.name
FROM blog_posts
INNER JOIN model_names_relations
INNER JOIN model_names
ON blog_posts.id = model_names_relations.blog_post_id and model_names.id = model_names_relations.model_name_id
WHERE blog_posts.id = '12'
to laravel query builder. I'm NOT using the full orm, so I can't use the belongstomany feature. I'm restricted to the query builder.
I tried this:
$query = ( new DbSql )->db()->table( 'blog_posts' )
->join( 'model_names_relations', 'blog_post_id.id', '=', 'model_names_relations.blog_post_id' )
->join( 'model_names', 'model_names.id', '=', 'model_names_relations.model_name_id' )
->where( 'blog_posts.id', '12')
->select( 'model_names.name' )
->get();
var_dump( $query );
exit;
But it won't work I get:
protected 'message' => string 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'blog_post_id.id' in 'on clause' (SQL: select model_names.name from blog_posts inner join model_names_relations on blog_post_id.id = model_names_relations.blog_post_id inner join model_names on model_names.id = model_names_relations.model_name_id where blog_posts.id = 12)' (length=357)
private 'string' (Exception) => string '' (length=0)
What would be the correct conversion syntax ?
Here is Laravel query builder
$query =DB::table('blog_posts')
->join('model_names_relations', 'blog_posts.id', '=', 'model_names_relations.blog_post_id')
->join('model_names', 'model_names.id', '=', 'model_names_relations.model_name_id')
->where('blog_posts.id', '12')
->get();
However your error means there is no 'id' in blog_post_id table.

Resources