Having hard time to do self join with query builder but getting unexpected results or query while using toSql() or get(), please don't suggest eloquent, just query builder with raw query.
Trying to get all child menus followed by their parent.
Id column also required
$users = DB::table('all_menus as A all_menus as B')
->select(DB::raw('A.menu_name__v1 As menu_name_parent, B.menu_name__v1 As menu_name_child'))
->where('A.id', '=' 'B.parent_menu_id__v1')
->toSql();
join() method supports to define an alias name, so you can self join with alias name like this query:
$users = DB::table('all_menus as A')
->join('all_menus as B', 'A.id', '=', 'B.parent_menu_id__v1')
->select('A.menu_name__v1 AS menu_name_parent', 'B.menu_name__v1 AS menu_name_child')
->toSql();
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 have two table customer_id namely tbl_customer and tbl_stocks connected on the same database. My logic about this problem is JOIN sql statement.
This is for Laravel and MySQL, so far i've tried this on PHP and is working fine but when I implement it on laravel it is not working i wonder why?
here is my code in PHP and want to convert it to laravel but I dont know where to put? will i put it in the View or in the Controller
$query = "SELECT c.*, s.* FROM tbl_customer c JOIN tbl_stock s ON s.customer_id = c.customer_id AND c.customer_id = 1";
Controller
$data = DB::table('tbl_customer')
->join ...... //Im not sure about this
->select .... // neither this
->get();
print_r($data)
Model
I have no codes on my model
Routes
Route::get('/admin/shopcontrol', 'Admin\ShopsController#testquery');
I expect a result of fetching or getting the query or result of the values in just a simple echo and the fetch join is connected
Have you checked the Laravel site?
https://laravel.com/docs/5.7/queries#joins
It has a demonstration you could use to reorganize your code.
As it follows below from the site.
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. Of course, as you can see, you can 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();
You may find more information there if it suits you.
Try this:
$data = DB::table('tbl_customer')
->join('tbl_stock', 'customer_id', '=', 'tbl_customer.customer_id')
->select('tbl_customer.*', 'tbl_stock.*')
->where('customer_id', '=', 1)
->get();
I have a Laravel Eloquent query where I am trying to select multiple columns from a MySQL table.
$query = DB::connection('global')
->select(
'mytable.id',
'mytable.column1',
'mytable.another_column',
'mytable.created_at',
'myothertable.id
)
->from('mytable')
->get();
It looks like the select() function takes three arguments: query, bindings and useReadPdo. The above query gives me an error:
{"error":true,"message":"Type error: Argument 1 passed to Illuminate\\Database\\Connection::prepareBindings() must be of the type array, string given" }
How do I write a select with Laravel query builder for the above columns?
I am structuring the query in this way, because I am looking to have a join across another table like so:
$query = DB::connection('global')
->select(
'mytable.id',
'mytable.column1',
'mytable.another_column',
'mytable.created_at',
'myothertable.id
)
->from('mytable')
->leftJoin('myothertable', function($join){
$join->on('mytable.id', '=', 'myothertable.id');
})
->get();
How do I use the select function to grab multiple columns across tables with Eloquent query builder?
How do I write a select with Laravel query builder for the above columns?
You can do:
$data = DB::table('mytable')
->join('myothertable', 'mytable.id', '=', 'myothertable.mytable_id')
->select(
'mytable.id',
'mytable.column1',
'mytable.another_column',
'mytable.created_at',
'myothertable.id'
)
->get();
You can read the documentations here
I'm trying to perform the following SQL query using Eloquent:
select s.username, s.link, s.followers, sum(h.count)
from users s, views h where h.username = s.username order by h.timestamp
I'm aware of the join(), sum() and select() methods, however, how can I perform the sum() and select() together?
To use a SUM() you will have to use DB::raw. Here is an example that should make your query work:
DB::table('users as s')
->select(DB::raw('s.username, s.link, s.followers, sum(h.count)'))
->join('views as h', 'h.username', '=', 's.username')
->orderBy('h.timestamp')
->get();
I am trying to join 2 tables on Laravel 5 and have to use Query Builder. I have already got the sql for it but i am not able to convert it to Query Builder syntax. SQL is below
SELECT v.id, r.full_name, b.full_name, s.full_name
FROM vehicles v
LEFT JOIN clients r ON v.representive_client_id = r.id
LEFT JOIN clients b ON v.buyer_client_id = b.id
LEFT JOIN clients s ON v.seller_client_id = s.id
and what i tried is
$query_result = DB::table('vehicles')
->selectRaw($query)
->leftJoin('clients', 'vehicles.representive_client_id', '=', 'clients.id')
->leftJoin('clients', 'vehicles.buyer_client_id ', '=', 'clients.id')
->leftJoin('clients', 'vehicles.seller_client_id ', '=', 'clients.id')
->paginate(30);
The problem is i dont know how to use AS caluse for Query Builder as i need to retrive 3 different types of full_name columns from vehicles table.Anybody can help me about how to write it in a proper Query Builder syntax ?. Any help would be appreciated.
You can use aliases with select columns and tables as well as joins, because the Query Builder will know to quote them correctly. So you can do this without any problems:
$query_result = DB::table('vehicles v')
->select('v.id', 'r.full_name as r_name', 'b.full_name as b_name', 's.full_name as s_name')
->leftJoin('clients r', 'vehicles.representive_client_id', '=', 'r.id')
->leftJoin('clients b', 'vehicles.buyer_client_id ', '=', 'b.id')
->leftJoin('clients s', 'vehicles.seller_client_id ', '=', 's.id')
->paginate(30);
Of course, you can use whatever aliases you want for the selected columns. Actually one of the examples in the Query Builder Selects Documentation uses aliases: email as user_email.
To check the SQL query generated by the Query Builder you can use the toSql method. So in your case instead of ->paginate(30) you can have ->toSql(), which will return a string with the SQL generated by the Query Builder, which you can compare to your raw query and see if it matches.