I'm new to laravel and I have some issues with the query builder.
The query I would like to build is this one:
SELECT SUM(transactions.amount)
FROM transactions
JOIN categories
ON transactions.category_id == categories.id
WHERE categories.kind == "1"
I tried building this but it isn't working and I can't figure out where I am wrong.
$purchases = DB::table('transactions')->sum('transactions.amount')
->join('categories', 'transactions.category_id', '=', 'categories.id')
->where('categories.kind', '=', 1)
->select('transactions.amount')
->get();
I would like to get all the transactions that have the attribute "kind" equal to 1 and save it in a variable.
Here's the db structure:
transactions(id, name, amount, category_id)
categories(id, name, kind)
You don't need to use select() or get() when using the aggregate method as sum:
$purchases = DB::table('transactions')
->join('categories', 'transactions.category_id', '=', 'categories.id')
->where('categories.kind', '=', 1)
->sum('transactions.amount');
Read more: http://laravel.com/docs/5.0/queries#aggregates
If one needs to select SUM of a column along with a normal selection of other columns, you can sum select that column using DB::raw method:
DB::table('table_name')
->select('column_str_1', 'column_str_2', DB::raw('SUM(column_int_1) AS sum_of_1'))
->get();
You can get some of any column in Laravel query builder/Eloquent as below.
$data=Model::where('user_id','=',$id)->sum('movement');
return $data;
You may add any condition to your record.
Thanks
MyModel::where('user_id', $_some_id)->sum('amount')
Related
I have two tables (Table1, Table2).
I want to print the sum of the records whose properties match Table1 in Table2 while listing the Table1 table.
My two tables contain very large records, performance is important to me.
// Model -> relationships
public function cars()
{
return $this->hasMany('App\Models\Table2', 'list_id', 'list_id');
}
// Controller
$table1 = Table1::with('cars' => function($query){
$query->where('table2.color','=', 'table1.color')
$query->where('table2.year','=', 'table1.year')
}])
->get();
I'm adding the sample database pictures:
Thank you.
First, add below use statement to the top of your controller:
use DB;
Now, If you only want to get the records amount, you can do this:
$amount = DB::table('table1')
->join('table2', function($join){
$join->on('table1.color', '=', 'table2.color');
$join->on('table1.year', '=', 'table2.year');
})->count();
But if you want to get the list of the table1 records, you can change the query a bit like below:
$records = DB::table('table1')
->select('table1.*')
->join('table2', function($join){
$join->on('table1.color', '=', 'table2.color');
$join->on('table1.year', '=', 'table2.year');
})->count();
Please follow Laravel's official guide on this topic:
https://laravel.com/docs/8.x/queries#advanced-join-clauses
Use join to merge the two tables read more about join here https://www.w3schools.com/sql/sql_join.asp
I have 3 tables :
*hinteractions (id, name...)
*effects (id, name...)
*hinteractions_has_effects (hinteractons_id, effects_id)
I might have one to many effects in a hinteractions et one hinteraction might have one to many effects, that's why I have this join table.
I have this Eloquent query :
Query 1 :
$informations_plante = DB::table('herbs')
->select('herbs.name as hname', 'herbs.sciname', 'herbs.id as herbid','hinteractions.id as hinteractionid','hinteractions.note as hinteractionnote','hinteractions.force_id','targets.name as targetname', 'forces.name as force_name')
->leftJoin('hinteractions', 'herbs.id', '=', 'herb_id')
->leftJoin('forces', 'forces.id', '=', 'force_id')
->leftJoin('targets', 'targets.id', '=', 'hinteractions.target_id')->where('herbs.id', $id)
//I would like to use the query 2 here to select effects.name with hinteraction.id used in this query to get effects' name in this query (I might have one to many).
->get();
Query 2 :
$hinteractions_has_effects = DB::table('hinteraction_has_effects')
->select(DB::raw('effect_id, hinteraction_id','effects.name'))
->where('hinteraction_has_effects', '=', hinteraction.id (from the query 1))
->get();
With query 1, I retrieve some informations like hinteractions_id.
I would like to use those hinteractions_id and use them in the query 2.
The best way will be to merge both queries (1 and 2) to get only one Eloquent query.
Do you have any idea ?
Try below query and let me know if this is what you're looking for -
$informations_plante = DB::table('herbs')
->select('herbs.name as hname', 'herbs.sciname', 'herbs.id as herbid','hinteractions.id as hinteractionid','hinteractions.note as hinteractionnote','hinteractions.force_id','targets.name as targetname', 'forces.name as force_name', 'effects.id as effects_id', 'effects.name as effects_name')
->leftJoin('hinteractions', 'herbs.id', '=', 'herb_id')
->leftJoin('forces', 'forces.id', '=', 'force_id')
->leftJoin('targets', 'targets.id', '=', 'hinteractions.target_id')->where('herbs.id', $id)
//added below lines
->leftJoin('hinteraction_has_effects', 'hinteraction_has_effects.hinteractons_id', '=', 'hinteractions.id')
->leftJoin('effects', 'hinteraction_has_effects.effects_id', '=', 'effects.id')
->get();
HTH!
I'm printing some data in mi index.blade.php file but it returns duplicated values. This is my query:
$hist = DB::table('codigo_sisnova')
->join('llamada', 'codigo_sisnova.idPaciente', '=', 'llamada.id_paciente')
->join('medico', 'llamada.id_medico', '=', 'medico.id_medico')
->where('llamada.status_llamada', 'Finalizada')
->where(function($query){
$query->where('llamada.status_pago', '=', 'Sisnova')
->orWhere('llamada.status_pago', '=', 'RedireccionadaSisnova');
})
->distinct()
->get();
I already tried with unique() but it doesn't work too.
EDIT
The relations between tables are one to may from "codigo_sisnova" to "llamada", if I take out the join with the table "medico" the rows keep duplicating
Every row gets a duplicate
You will have multiple codigo_sisnova rows if you are joining tables that have multiple matches on that table. DISTINCT would not eliminate those since the joined data will make it non-distinct in those results. I would recommend trying to use groupBy() to eliminate the redundant rows.
You are missing the group_by statement. Assuming that you have an ID column in codigo_sisnova, besides adding it in a SELECT clause, you need to add it before your get() method :
$hist = DB::table('codigo_sisnova')
->select('codigo_sisnova.id')
->join('llamada', 'codigo_sisnova.idPaciente', '=', 'llamada.id_paciente')
->join('medico', 'llamada.id_medico', '=', 'medico.id_medico')
->where('llamada.status_llamada', 'Finalizada')
->where(function($query){
$query->where('llamada.status_pago', '=', 'Sisnova')
->orWhere('llamada.status_pago', '=', 'RedireccionadaSisnova');
})
->group_by('codigo_sisnova.id')
->get();
I have 2 tables: users and articles. To fetch all columns from the articles table and only user_name column from the users table, I use this code:
$articles = Article::join('users', 'articles.user_id', '=', 'users.user_id')
->get(array('articles.*', 'users.user_name'));
and it works fine, but when I use paginate() method like this:
$articles = Article::join('users', 'articles.user_id', '=', 'users.user_id')
->paginate(10);
it fetches all columns from both tables, which I don't want. My question is: How can I select columns that will be returned in the result if I use paginate() method in Laravel framework?
The select function does this.
$articles = Article::join('users', 'articles.user_id', '=', users.user_id')
->select('articles.*', 'users.user_name')
->paginate(10);
In Laravel, we could join 2 tables with this:
DB::table("users")->join('moreusers', 'users.id', '=', 'moreusers.user_id')->get();
And retrieve data with this:
$querydata[$i]->email
If both tables contain the same column name, how can I specifically retrieve it from first and second table?
Thanks.
Use select() method:
DB::table("users")
->join('moreusers', 'users.id', '=', 'moreusers.user_id')
->select('users.email as user_email', 'moreusers.email as other_email')
->get();