Select Specific columns from multiple Table with with() and select () - laravel

I have 2 tables test and goals. One goal has many tests. I would like to fetch data like below.
$tests = test::with('goals')
->where('Goal_id', $goal_id)
->select('test.id as id','goals.Goal_id as goal_id','test.mode as mode')
->get();
But I am getting error Illuminate\Database\QueryException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'goals.Goal_id' in 'field list'

The goals relation is being eager loaded in a separate query so you will not have access to goals.Goal_id in your main query builder, instead you can modify your with() clause to pick specific columns from eager loaded relation as with('goals:Goal_id,another_column')
$tests = test::with('goals:Goal_id')
->where('Goal_id', $goal_id)
->select(['test.id as id','test.mode as mode'])
->get();

This should work:
$tests = DB::table('test')
->select(['test.id as id', 'goals.Goal_id as goal_id', 'test.mode as mode'])
->join('goals', 'goals.id', '=', 'test.goal_id')
->where('Goal_id', $goal_id)
->get();

Related

Laravel - select temporary column where temporary column

$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

Eloquent in Laravel-Datatables, select in select?

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

Getting Count of Rows With Join in Laravel

What is the correct way to get a count of rows whose specified column is a foreign key of another table. Here's what I mean:
I have 2 tables providers and products. The providers table has primary key provider_idand the table products has a foreign key reference to provider_id via a column named the same, provider_id. My intention is to fetch the details of each provider including all their products, that is a total of all instances where provider_id in the products table matched providers_id in the providers table.
Here's my code:
$data = DB::table('providers')
->join('products', 'products.provider_id', '=', 'providers.provider_id')
->select(
'providers.provider_id AS id',
'providers.provider_name AS name',
DB::raw("count(products->where('products.provider_id','=','providers.provider_id')) AS total_products"),
'providers.claims AS claims',
'providers.settlements AS settlements')
->groupBy('provider')
->get();
This seems not to work as I am getting the error below:
Undefined property: Illuminate\Database\MySqlConnection::$id (View: /var/www/test/app/resources/views/agent/serviceproviders/index.blade.php)
What am I doing wrong?
The issue was in how I was counting the rows and how I was grouping the data for proper counting. I feel so dumb, this was too easy. Below is the correct code:
$data = DB::table('providers')
->join('products', 'products.provider_id', '=', 'providers.provider_id')
->select(
'providers.provider_id AS id',
'providers.provider_name AS name',
DB::raw("count(products.provider_id) AS total_products"))
->groupBy('providers.provider_id')
->get();

Laravel join with limit

I have the following working for laravel paginate
$query = DB::table('clients')
->leftJoin('radusergroup', 'clients.username', '=', 'radusergroup.username')
->leftJoin('recharge', 'clients.username', '=', 'recharge.soldto');
I want to join some values from two tables radusergroup and recharge. radusergroup always return one row as it has only one row stored whereas recharge table return multiple rows. I want only one row returned from recharge table which is latest entry.
Right now its return all the possible rows from recharge table and showing it on paginated view.
This is Laravel Official documentation
DB::table('clinets')
->leftJoin('radusergroup', 'clients.username', '=', 'radusergroup.username')
->leftJoin('recharge', function ($leftJoin) {
$leftJoin->on('clients.username', '=', 'recharge.soldto')
->where('recharge.create_date', '=', DB::raw("(select max(`create_date`) from recharge)"));
})
->get();
this is the case if you have create_date column in your table, if you haven't got it I strongly recommend to create that column.

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