How to remove quotes from SQL query Laravel? - laravel

I have the following query:
$this->data = \DB::table('months')->select(DB::raw("months.id, COUNT(transactions.id) as total"))
->leftJoin('transactions', function($join)
{
$join->on('months.id', '=', DB::raw('MONTH(created_at)'))
->on('transactions.doctor_id', '=', $this->user_id);
})
->groupBy('months.id')
->get();
It involks an error on line ->on('transactions.doctor_id', '=', $this->user_id);. It added single quotes for variable $this->user_id.
How to avoid this eroros:
SQLSTATE[42S22]: Column not found: 1054 Unknown column '2' in
'on clause' (SQL: select months.id, COUNT(clients.id) as total
from `months` left join `clients` on `months`.`id` = MONTH(created_at)
and `clients`.`doctor_id` = `2` group by `months`.`id`)

You can try to use DB:raw like this:
->on('transactions.doctor_id', '=', DB::raw($this->user_id));

Related

Laravel: Column not found

I am trying to set up the following SQL in Laravel.
$projects = DB::table(DB::raw('places, areas, areas_places'))
->select('projects.*')->distinct()
->leftJoin('tags_places as r', 'r.place_id', '=', 'places.place_id')
->leftjoin('tags as t', 't.id', '=', 'r.tag_id')
->join('areas_places as e', 'e.place_id', '=', 'places.place_id')
->join('areas as a', 'a.area_id', '=', 'e.area_id')
However, I keep getting the following error.
Column not found: 1054 Unknown column 'places.place_id' in 'on clause'
The column exists in the database. I've also checked to see if the column is spelled correctly-- it is. So what could I be missing? I am using Laravel Mix version 4.0.7
I figured out what the issue was. I just didn't need the DB::raw statement. This works as expected without problems:
$projects = DB::table('projects')
->select('projects.*')->distinct()
->leftJoin('tags_places as r', 'r.place_id', '=', 'places.place_id')
->leftjoin('tags as t', 't.id', '=', 'r.tag_id')
->join('areas_places as e', 'e.place_id', '=', 'places.place_id')
->join('areas as a', 'a.area_id', '=', 'e.area_id')
IGP's suggestion to check with toSql() was helpful in figuring this out.

Unknown column total when using where

I have made this query:
$query = UserHistoryAccess::with('user')
->select('user_history_accesses.*', DB::raw('count(user_history_accesses.id) as total'))
->join('users', 'user_history_accesses.user_id', 'users.id')
->groupBy('user_history_accesses.user_id')
->where('total', 'like', '%' . $term . '%')
;
Even I have followed this solution but I got this error :
Illuminate\Database\QueryException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'total' in 'where clause' (SQL: select count(*) as aggregate from user_history_accesses inner join users on user_history_accesses.user_id = users.id where total like %388%) in file C:\xampp\htdocs\HabilitisBack\vendor\laravel\framework\src\Illuminate\Database\Connection.php on line 671
you can not use result colum in the where, you should recalculate it in the where to get the desired result, but try using 'having':
->having('total', 'like', '%' . $term . '%');

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();

Problems with join ERROR: 1054

I tried to use a join, with an array given as a condition:
$task = Task::join('oc_groups', function($join) use ($filter) {
foreach($filter['groups']['data'] as $key => $value) {
$join->on('oc_groups.id', $value);
}
});
But I get the error message :
SQLSTATE[42S22]: Column not found: 1054 Unknown column '1' in 'on clause' (SQL: select oc_tasks.title as task_title from oc_tasks inner join oc_groups on oc_groups.id = 1 where oc_tasks.task_date between 2017-07-01 and 2017-07-31)
The 1 is the content of the $value. What I am doing wrong? - The table oc_groups has a field named id.
As you don't have any relation between these two tables so Try with out join. like this
select `oc_tasks`.`title` as `task_title` from `oc_tasks` ,`oc_groups`
where `oc_tasks`.`task_date` between 2017-07-01 and 2017-07-31 and `oc_groups`.`id` = `1`

How to use multiple 'where' clause with multiple tables with Eloquent ORM?

I have a database query with multiple 'JOIN' statements in my Laravel application, but I don't know how to correctly add a where clause to it.
Here is my function:
return Topic::join('blogs', 'topics.blog_id', '=', 'blogs.id')
->join('blog_subscriptions as us', function ($j) use ($userId){
$j->on('us.blog_id', '=', 'blogs.id')
->where('us.user_id', '=', $userId);
})
->take(Config::get('topic.topics_per_page'))
->offset($offset)
->get(['topics.*']);
I would like to add a 'where' clause to the 'topics' table - so I add a line where('rating', '>', 1), after "Topic::", so the code is like this:
Topic::where('rating', '>', 1)
->join('blogs', 'topics.blog_id', '=', 'blogs.id')
->join('blog_subscriptions as us', function ($j) use ($userId){
$j->on('us.blog_id', '=', 'blogs.id')
->where('us.user_id', '=', $userId);
})
->take(Config::get('topic.topics_per_page'))
->offset($offset)
->get(['topics.*']);
but it only leads to an error:
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'rating' in where clause is ambiguous (SQL: select topics.* from topics inner join blogs on topics.blog_id = blogs.id inner join blog_subscriptions as us on us.blog_id = blogs.id and us.user_id = 1 where rating > 1 limit 2 offset 0)
As the error message says, the column rating is ambiguous. Meaning SQL can't tell which column you mean because there might be another one in your join that's named rating as well.
In this situation it helps to clarify in which table the field is. This is simply done by using the [table].[field] syntax
where('topics.rating', '>', 1)

Resources