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();
Related
How do you know the other code to get this query in laravel using eloquent?
$variable_value= DB::select(
'SELECT
sv.VARIABLE_NAME as sv_variable_name, sv.TYPE as sv_type, sv.ADDRESS as sv_address, sv.VALUE as sv_value,
ms.VARIABLE_NAME as ms_variable_name, ms.TYPE as ms_type, ms.ADDRESS as ms_address, ms.VALUE as ms_value
FROM MASTER_VARIABLES ms
JOIN SLAVE_VARIABLES sv ON ms.SLV_ADDRESS=sv.ID_VARIABLE'
);
Thank you for your help, guys!
It's all in the documentation. Give it a read?
# Selects
Specifying A Select Clause
You may not always want to select all columns from a database table. Using the select method, you can specify a custom select clause for the query:
$users = DB::table('users')->select('name', 'email as user_email')->get();
# Joins
Inner Join Clause
The query builder may also be used to write join statements. To perform a basic "inner join", you may use the join method on a query builder instance. The first argument passed to the join method is the name of the table you need to join to, while the remaining arguments specify the column constraints for the join. You can even join to multiple tables in a single query:
$users = DB::table('users')
->join('contacts', 'users.id', '=', 'contacts.user_id')
->join('orders', 'users.id', '=', 'orders.user_id')
->select('users.*', 'contacts.phone', 'orders.price')
->get();
$query = DB::table('MASTER_VARIABLES as ms')
->select(
'sv.VARIABLE_NAME as sv_variable_name',
'sv.TYPE as sv_type',
'sv.ADDRESS as sv_address',
'sv.VALUE as sv_value',
'ms.VARIABLE_NAME as ms_variable_name',
'ms.TYPE as ms_type',
'ms.ADDRESS as ms_address',
'ms.VALUE as ms_value'
)
->join('SLAVE_VARIABLES as sv', 'ms.SLV_ADDRESS', '=', 'sv.ID_VARIABLE')
->get();
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();
Okay I have a some code written to retrieve data from some left join in my database, after retrieving this data I use the id from each to populate a modal. The thing is it works for some of the data that has been retrieve. For when i look at the query result I notice the id's are null for some valid data an some or not
$someuser = DB::table('users')
->join('sometable', 'sometable.user_id', '=','users.id')
->leftjoin('sometable', 'users.id', '=', 'skills.user_id')
->leftjoin('sometable', 'users.id', '=','schools.user_id')
->leftjoin('sometable', 'users.id', '=', 'languages.user_id')
->leftjoin('sometable','users.id', '=', 'memberships.user_id')
->leftjoin('sometable', 'users.id', '=', 'convictions.user_id')
->leftjoin('sometable', 'users.id', '=', 'work_experiences.user_id')
->select('users.*','sometable.name as sometablename','sometable.*')
->where('users.first_name', '<>', '')->whereNotNull('users.id')
->orderBy('sometable.updated_at', 'DESC')->groupby('users.first-name')->distinct()->paginate(100);
from that, the query result looks like:
{"total":966,"per_page":100,"current_page":1,"last_page":10,"from":1,"to":100,"data":[{"id":null, etc
What am simple saying why is my id null
I think its not showing me no ids over 1900 so if a user is over 1900 it showing me null
You are selecting * from multiple tables. Since you are also left joining, it's very likely there is nothing to join on so your database is returning those columns as null. Specifically select the id you need and alias it as something which will not be overwritten by another column.
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')
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);