How to create with Laravel query in controller - php

who can help me? I was selected from sql. This is my sql.
How to create with Laravel query in controller.
SELECT
`sp`.name_en,
`sp`.email,
`sp`.phone,
`post`.`name`
AS
`position_name`
FROM
`staff_profiles` `sp`
INNER JOIN
`staff_positions` `p`
INNER JOIN
`positions` `post`
WHERE
`sp`.id=`p`.`staff_id` AND `p`.`position_id`=`post`.id

You can use the Query Builder to build the same query:
https://laravel.com/docs/5.4/queries
Or you can just run your raw query using the DB Facade:
https://laravel.com/docs/5.4/database#running-queries
Something like this:
DB::select('SELECT sp.name_en, sp.email, sp.phone, post.name AS position_name FROM staff_profiles sp INNER JOIN staff_positions p INNER JOIN positions post WHERE sp.id=p.staff_id and p.position_id=post.id');

Include you model like use BusinessObject\User; than following query is example query.
$matchThese = ["job_comments.status" =>1,'job_id'=>$job_id];
$job_com = DB::table('job_comments')
->select('job_comments.*','users.id as userid','users.first_name','users.last_name','users.image','users.role')
->where($matchThese)
->join('users', 'job_comments.commenter_id', '=','users.id' )
->get();

Related

Write SQL sum with join and group by in Laravel?

I wish to write a query from SQL Server to laravel.
I want to write like this SQL:
select SUM(TRANSTKD.TRD_U_PRC) as prctotal from DOCINFO
inner join [TRANSTKH] on [DOCINFO].[DI_KEY] = [TRANSTKH].[TRH_DI]
inner join [SKUMASTER]
inner join [TRANSTKD]
on [TRANSTKD].[TRD_SKU] = [SKUMASTER].[SKU_KEY]
on [TRANSTKH].[TRH_KEY] = [TRANSTKD].[TRD_TRH]
where [DOCINFO].[DI_KEY] = 148978
GROUP BY [SKUMASTER].[SKU_CODE];
Try this below laravel query
DB::table('DOCINFO')
->join('TRANSTKH', 'DOCINFO.DI_KEY', '=', 'TRANSTKH.TRH_DI')
->join('TRANSTKD', 'TRANSTKH.TRH_KEY', '=', 'TRANSTKD.TRD_TRH')
->join('SKUMASTER', 'TRANSTKD.TRD_SKU', '=', 'SKUMASTER.SKU_KEY')
->select(DB::raw('SUM(TRANSTKD.TRD_U_PRC) as prctotal'))
->where('DOCINFO.DI_KEY',148978)
->groupBy('SKUMASTER.SKU_CODE')
->get();
Let me know how it helps you!

This row query working properly please help to change it eloquent query

SELECT `foduu_listing`.`id`,`foduu_listing_filedetail`.`primary`, `foduu_listing`.`name`,`foduu_listing`.`filemanager_id`,`foduu_filemanager`.`filepath`,`foduu_detail_orders`.`listing_id`, COUNT(`foduu_detail_orders`.`listing_id`) AS count,SUM(`foduu_detail_orders`.`total`) AS total
FROM foduu_listing
left join `foduu_detail_orders` on `foduu_listing`.`id` = `foduu_detail_orders`.`listing_id`
left join `foduu_listing_filedetail` on `foduu_listing`.`id` = `foduu_listing_filedetail`.`listing_id`
left join `foduu_filemanager` on `foduu_listing`.`filemanager_id` = `foduu_filemanager`.`id`
where `foduu_detail_orders`.`listing_id` = 593
I would do it like this:
DB::table('foduu_listing')->select('foduu_listing.id`,foduu_listing_filedetail.primary, foduu_listing.name,foduu_listing.filemanager_id,foduu_filemanager.filepath,foduu_detail_orders.listing_id')
->leftJoin('foduu_detail_orders', 'foduu_listing.id', '=', 'foduu_detail_orders.listing_id')
->leftJoin('foduu_listing_filedetail', 'foduu_listing.id', '=', 'foduu_listing_filedetail.listing_id')
->leftJoin('foduu_filemanager', 'foduu_listing.filemanager_id', '=', 'foduu_filemanager.id')
->selectRaw('COUNT(`foduu_detail_orders`.`listing_id`) AS count')
->selectRaw('SUM(`foduu_detail_orders`.`total`) AS total')
->where('foduu_detail_orders.listing_id', 593)
->get();
If instead of calling get at the end you call toSql you can check the query generated:
select `foduu_listing`.`id``,foduu_listing_filedetail`.`primary, foduu_listing`.`name,foduu_listing`.`filemanager_id,foduu_filemanager`.`filepath,foduu_detail_orders`.`listing_id`, COUNT(`foduu_detail_orders`.`listing_id`) AS count, SUM(`foduu_detail_orders`.`total`) AS total from `foduu_listing` left join `foduu_detail_orders` on `foduu_listing`.`id` = `foduu_detail_orders`.`listing_id` left join `foduu_listing_filedetail` on `foduu_listing`.`id` = `foduu_listing_filedetail`.`listing_id` left join `foduu_filemanager` on `foduu_listing`.`filemanager_id` = `foduu_filemanager`.`id` where `foduu_detail_orders`.`listing_id` = ?
However you are not making use of Laravel tools if you just translate your queries from raw SQL to Eloquent like this, you should define your models and your relationships properly and then make use of that for querying your data.

laravel DB query select multiple tables without joining

I have a Raw SQL query here and what to know how to write this Raw query in laravel query builder
Here is the Raw Query :
SELECT tbl_stock.* , series.*, series_size.*, tbl_size.* FROM
tbl_stock , series, series_size, tbl_size
WHERE
tbl_stock.series_id = series.series_id AND
tbl_stock.size_id = series_size.size_id AND
tbl_stock.size_id = tbl_size.size_id
I have Tried DB Query builder laravel but getting exception
$stock_info = DB::table(['tbl_stock', 'shades'])->get();
This gives me this
Array to String Conversion Exception
THANKS IN ADVANCE
First of all, you should replace your archaic join syntax with modern syntax:
SELECT
t1.*,
t2.*,
t3.*,
t4.*
FROM tbl_stock t1
INNER JOIN series t2
ON t1.series_id = t2.series_id
INNER JOIN series_size t3
ON t1.size_id = t3.size_id
INNER JOIN tbl_size t4
ON t1.size_id = t4.size_id;
Then, use Laravel's query builder syntax to build your query in PHP code:
$stocks = DB::table('tbl_stock')
->join('series', 'tbl_stock.series_id', '=', 'series.series_id')
->join('series_size', 'tbl_stock.size_id', '=', 'series_size.size_id')
->join('tbl_size', 'tbl_stock.size_id', '=', 'tbl_size.size_id')
->select('tbl_stock.*', 'series.*', 'series_size.*', 'tbl_size.*')
->get();

inner join Eloquent as JSON, bad answer

I have problem with join in Eloquent. I have function:
public function getSearchQuery($search_phrase) {
return Car::select("car_models.id", "car_models.name", "brands.name")
->join("car_models", "cars.car_model_id", "=", "car_models.id")
->join("brands", "car_models.brand_id", "=", "brands.id")
->get();
}
This function generate correct request SQL like:
select `car_models`.`name`, `brands`.`name` from `cars` inner join `car_models` on `cars`.`car_model_id` = `car_models`.`id` inner join `brands` on `car_models`.`brand_id` = `brands`.`id` where `cars`.`deleted_at` is null
and it works but I get only brand, without car_model, someone know why ? I return JSON to typeahead.
Regards
Both columns you're selecting are called "name". Try using an alias:
Car::select("car_models.id", "car_models.name as model_name", "brands.name as brand_name")
and you should be fine.

Can I query relations using an INNER JOIN instead of two queries in Eloquent?

Can I write something like this:
$post = Post::join(['author'])->find($postId);
$authorName = $post->author->name;
To produce only ONE select with inner join (no 2 selects) and without using DB query builder
SELECT
post.*,
author.*
FROM post
INNER JOIN author
ON author.id = post.author_id
WHERE post.id = ?
You can do it in Eloquent using the join method:
$post = Post::join('author', function($join)
{
$join->on('author.id', '=', 'post.author_id');
})
->where('post.id', '=', $postId)
->select('post.*', 'author.*')
->first();
Please note that your results in $post will be an object where their attributes will correspond to the result set, if two columns has the same name it will be merged. This happen when using:
->select('post.*', 'author.*')
To avoid this, you should create alias to those columns in the select clause as shown below:
->select('post.id AS post_id', 'author.id AS author_id')
Try
Post::join('author',function($join){
$join->on('author.id','=','post.author_id');
})->where('post.id','=',$postId)->select('post.*','author.*');

Categories