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`
Related
I'm using eloquent query builder in Laravel to help me create the equivalent of
SELECT name,'fund' AS content_type FROM fundraisers
This is what I tried:
$db = DB::table('fundraisers')->select("name,'fund' AS content_type")->get()->toArray();
But I get the error
Illuminate\Database\QueryException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'name,'fund'' in 'field list' (SQL: select `name,'fund'` as `content_type` from `fundraisers`)
What am I doing wrong and how do I fix this?
List the columns you want on select like this:
$db = DB::table('fundraisers')
->select('name', DB::raw("'fund' as content_type"))
//...
Check Laravel docs for more info.
The goal is to sort my campaigns by views in my filtration, which is why i have an analytics table with relations to my campaigns
My campaign model (The DB name is "ads"):
public function views() {
return $this->hasMany('App\Analytic', 'foreign_id', 'id')->where('foreign_type', '=', 'campaign');
}
The controller of my filtration:
$query = Ad::withCount('views')->with('tags');
$query->where("is_active", "=", 1);
$query->where("status", "=", 1);
$query->orderBy('views_count', 'DESC');
$campaigns = $query->get();
Now the reason for not writing it without the $query-> part, is because the query has lots of if statements depending on filtration settings.
The error im getting:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'views_count' in 'order clause' (SQL: select count(*) as aggregate from `ads` where `is_active` = 1 and `status` = 1 and `from_year` >= 7 and `to_year` <= 88 and `price` >= 1000 and `price` <= 64000 order by `views_count` desc)
The error is it tries to fetch a column, but i can't figuere out why.
If i try to access $campaign->views_count in my blade template, it shows the count just fine.
Thank you for your time, i hope someone can tell me what I'm doing wrong here.
The error you're getting is a result of a count() method not a get().
something like this
$query = Ad::withCount('views')->with('tags');
$query->where("is_active", "=", 1);
$query->where("status", "=", 1);
$query->orderBy('views_count', 'DESC');
$campaignCount = $query->count();
wich replaces the complex select part with:
select count(*) as aggregate
if you need the count() and the get(), do it like this:
$query = Ad::withCount('views')->with('tags');
$query->where("is_active", "=", 1);
$query->where("status", "=", 1);
$campaignCount = $query->count();
$query->orderBy('views_count', 'DESC');
$campaigns = $query->get();
$search_str = "full name search";
$user = App\User::selectRaw("CONCAT(`f_name`, `l_name`) AS `fullname`")
->where('fullname', 'LIKE', '%$search_str%')
->get();
Based on the code above, the column fullname actually does not exist in DB Table. fullname column is just a temporary column.
But when I use it on where clause, Laravel return an error:
Column not found: 1054 Unknown column 'fullname' in 'where clause'
You can use the CONCAT function in your where filter.
$search_str = "full name search";
$user = App\User::selectRaw("CONCAT(`f_name`, `l_name`) AS `fullname`")
->whereRaw("CONCAT(`f_name`, `l_name`) LIKE '%?%'", [$search_str])
->get();
Use Having for aliases. See below reference link.
https://laravel.com/docs/5.6/queries#ordering-grouping-limit-and-offset
I have 3 tables, the 1st has a FK from the 2nd, and the 2nd has a FK from the 3rd:
Table 1: [admin_demandas]
-------------------------
id_demanda| projec_id
-------------------------
Table 2: [admin_projec]
-------------------------
id_projec | sub_id
-------------------------
Table 3: [admin_sub]
-------------------------
id_sub | name
-------------------------
What I need is to get the 'name' from Table 3 but starting with the Model of the table 1.
I was trying something like this:
$data = AdminDemanda::select([
'id_demanda',
'admin_projec.sub_id AS sub_id',
'admin_sub.name AS name',])
->join('admin_projec', 'admin_demandas.projec_id', '=', 'admin_projec.id_projec')
->join('admin_sub', 'admin_projec.sub_id', '=', 'admin_sub.id_sub')
->get();
return Datatables::of($data)->make(true);
I've done my Datatables with only 1 JOIN (2 tables) but not sure how to do those 2 JOIN (3 tables). I got this error:
[Err] 1054 - Unknown column 'admin_projec.sub_id' in 'on clause'
What should I modify in my query?
Should I need to use query builder instead Eloquent, with a DB::raw() query?
Solved with this:
Inner join between three tables in mysql
it was simple... now in case anyone need it, my Datatable query is:
$datos = AdminDemanda::select([
'id_demanda',
'admin_dis.nombre AS nombre_dist',
'admin_sub.nombre AS nombre_subes',
'mes AS mes_demanda',
'admin_sistemas.nombre AS nombre_sistema',
'admin_demandas.demanda_mwh AS mwh'])
->join('admin_projec', 'admin_demandas.proyeccion_demanda_id', '=', 'admin_projec.id_proyeccion_demanda')
->join('admin_sub', 'admin_projec.subestacion_id', '=', 'admin_sub.id_subestacion')
->join('admin_dis', 'admin_projec.dist_id', '=', 'admin_dis.id_dist')
->join('admin_sistemas', 'admin_projec.sistema_id', '=', 'admin_sistemas.id_sistema')
->get();
You should edit your models
By exemple in AdminSub model
public function projec(){
return $this->hasMany(AdminProjec::class , 'sub_id');
}
You should be able to do
\AdminSub::first()->projec();
And continue by editing your AdminProjec model with the same kind of relationship
public function demandas(){
return $this->hasMany(AdminDemandas::class , 'projec_id');
}
And now you could try
//I do not remember which one will works
\AdminSub::first()->projec()->first()->demandas()->name
//or
\AdminSub::first()->projec()->first()->demandas()->get()->name
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));