Add custom columns to select - laravel

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

Related

How do I search by a table included using with() statement

I am trying to search with a column on a connected table using with. According to laravel the following should work.
$lineItems = Invoice::with(['invoiceHeader' => function ($query) {
$query->where('community_id', '=', 1);
}, 'invoiceLineItems'])
->limit(10)
->get()
->toArray();
However, I don't get anything from the invoiceHeader table and I get all the invoices available. If I take out the function I get the same but with invoiceHeader's table values showing up.
$lineItems = Invoice::with(['invoiceHeader', 'invoiceLineItems'])
->limit(10)
->get()
->toArray();
It seems I might be doing something of a right join where I get all the Invoices but then only the invoiceHeader values when applicable to the foreign key.
Edit:
I put ->toSql(); after the limit() and it shows I only get the following.
"select * from `invoice` limit 10"
You should use whereHas mixing with with:
$lineItems = Invoice::with(['invoiceHeader', 'invoiceLineItems'])
->whereHas('invoiceHeader', function ($query) {
return $query->where('community_id', 1);
})
->limit(10)
->get()
->toArray();

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

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?

Conditional Join Eloquent

I have been looking through multiple question on stack overflow and also in the eloquent documents and I cannot seem to find how to join a table only if a variable is true.
I have my eloquent query.
$test = false;
$orderBy = 'name'; // Default order by
$sortBy = 'asc'; // Default sort by
$wheres[] = ['is_deleted', '=', $is_deleted];
Comm::where($wheres)
->select($selects)
->join('div', function($join) use($test) {
if($test) {
$join->on('Comm.div_id', '=', 'div.div_id')
}
}
->orderBy($orderBy, $sortBy)
->paginate($per_page, '*', 'page', $page);
Now I know you can join using the document join methods like
->join('div', 'comm.div_id', '=', 'div.div_id')
Is there anyway to accomplish the conditional add I am trying to do? This is for an api and I want the user to be able to specify if they want certain tables included in the returns results. I havent been able to find a post about this type of functionality. When I run my example code with the test if statement i get an error of:
Parse error: syntax error, unexpected '}'
So after a while of trial and error, I found you can use conditional clauses to help with joining tables.
$comm = Comm::select($selects)
->when($useJoins, function($query) use ($withs) {
return $query->with($withs);
})
->orderBy($orderBy, $sortBy)
->paginate($per_page, '*', 'page', $page);
I'm leaving this here for future reference since there is not a very large amount of data for this type of joining. I have also tried it with the eloquent join methods and those also work I just left it in an array for the with for display purposes.

Laravel left join showing error on 'ON' multiple condition

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

Resources