I want to execute a join statment in laravel by passing a php varaiable,Bit its not working when passing a varibale. Following is my code
$loc_services = Clinic::select('*')
->join('locations', 'locations.clinicID', '=', 'clinics.clinicID')
->join('location_services', 'location_services.locationID', '=', 'locations.locationID')
->join('services', 'services.serviceID', '=', $services_id)
->get();
I tried to execute it as statment and got the following
select * from `clinics` inner join `locations` on `locations`.`clinicID` = `clinics`.`clinicID` inner join `location_services` on `location_services`.`locationID` = `locations`.`locationID` inner join `services` on `services`.`serviceID` = `10`
When i directly executed it in phpmyadmin it returns fllowing error
Column not found: 1054 Unknown column '10' in 'on clause', i found that error is triggering because `10` is inside `''` quotes, how can i execute this
You have to pass $services_id in where cloud not in join on
$loc_services = Clinic::select('*')
->join('locations', 'locations.clinicID', '=', 'clinics.clinicID')
->join('location_services', 'location_services.locationID', '=', 'locations.locationID')
->join('services', 'services.serviceID', '=', 'clinics.services_id')//service_id column in Clinic
->where('services.serviceID',$services_id)
->get();
The third parameter in join will be treated as column. If you want to join the column with a specific value, you can use closure like this:
$loc_services = Clinic::select('*')
->join('locations', 'locations.clinicID', '=', 'clinics.clinicID')
->join('location_services', 'location_services.locationID', '=', 'locations.locationID')
->join('services', function($join) use ($service_id) {
$join->where('services.serviceID', $service_id);
})
->get();
The Raw sql will be:
inner join `services` on `services`.`serviceID` = 10
Related
I am new to Laravel,
can anyone help me with this how should I get unique records through this query in laravel query builder?
as of now, I'm getting duplicate records for example red, pink, red, pink, green
here is my query
MySQL query:
select count(*) as aggregate from (select `product`.`id`, `name`, `category`.`category`, group_concat(product_synonyms.product_synonym)as product_synonym, group_concat(product_tags.product_tag) as product_tag from `product` left join `product_tags` on `product`.`id` = `product_tags`.`product_id` left join `category` on `category`.`id` = `product`.`category_id` left join `product_synonyms` on `product`.`id` = `product_synonyms`.`product_id` where `user_id` = 1 and `product`.`deleted_at` is null group by `product_tags`.`product_id`) as `aggregate_table`;
Query builder:
Product::leftJoin('product_tags', 'product.id', 'product_tags.product_id')
->leftJoin('category', 'category.id', 'product.category_id')
->leftJoin('product_synonyms', 'product.id', 'product_synonyms.product_id')
->where('user_id', auth()->user()->id)
->select('product.id', 'name','category.category',DB::raw('group_concat(product_synonyms.product_synonym)as product_synonym'),DB::raw('group_concat(product_tags.product_tag)) as product_tag')
->groupBy('product_tags.product_id')
->orderBy('product.name', 'ASC')
->paginate(10);
I tried with different join and did many changes, seem I missing something and I couldn't figure it out, and
ending with expecting something from a helping hand
For get only one result in your query you need use:
$productDetails = Product::leftJoin('product_tags', 'product.id', 'product_tags.product_id')
->leftJoin('category', 'category.id', 'product.category_id')
->leftJoin('product_synonyms', 'product.id', 'product_synonyms.product_id')
->where('user_id', auth()->user()->id)
->select('product.id', 'name','category.category',DB::raw('group_concat(product_synonyms.product_synonym)as product_synonym')**->first();**
for example.
Paginate if you need more result of query. If you need more than one result use ->get()
You can try this
Product::leftJoin('product_tags', 'product.id', 'product_tags.product_id')
->leftJoin('category', 'category.id', 'product.category_id')
->leftJoin('product_synonyms', 'product.id', 'product_synonyms.product_id')
->where('user_id', auth()->user()->id)
->select('product.id', 'name','category.category',DB::raw('group_concat(product_synonyms.product_synonym)as product_synonym'),DB::raw('group_concat(product_tags.product_tag)) as product_tag')
->distinct()
->groupBy('product_tags.product_id')
->orderBy('product.name', 'ASC')
->paginate(10);
I'm trying to join two tables using more than one condition. The following query is not working because of the second join condition.
$all_update = DB::table('posts as p')
->join('cprefs as c','p.qatype', '=', 'c.qatype')
->where('c.wwide', '=', 'p.wwide') //second join condition
->where('c.user_id', $u_id)
->where('p.arank', 1)
->get();
The where() functions expects the last parameter to be a parameter where as you are passing in a column name.
To compare two columns you should use the whereColumn method.
With that in mind, you could also write your code like below:
$all_update = DB::table('posts as p')
->join('cprefs as c','p.qatype', '=', 'c.qatype')
->whereColumn('c.wwide', '=', 'p.wwide') //second join condition
->where('c.user_id', $u_id)
->where('p.arank', 1)
->get();
However, this would only work properly if the the join is an INNER JOIN which is true in your case.
The correct method to add multiple join clauses is as below
$all_update = DB::table('posts as p')
->join('cprefs as c', function($q) {
$q->on('p.qatype', '=', 'c.qatype')
->on('c.wwide', '=', 'p.wwide'); //second join condition
})
->where('c.user_id', $u_id)
->where('p.arank', 1)
->get();
Just use this one.
You need the keyword join to use multiple join condition. Irrespective of table.
$all_update = DB::table('posts as p')
->join('cprefs as c','p.qatype', '=', 'c.qatype')
->join('cprefs as c2','p.wwide', '=', 'c2.wwide') //second join condition
->where('c.user_id', $u_id)
->where('p.arank', 1)
->get();
How can I write the query like that on Laravel, btw I'm using DB
I followed this instruction
https://dba.stackexchange.com/questions/175786/join-multiple-tables-for-aggregates, but still not get it
mysql query:
SELECT inptkegiatan.IDKEGIATAN,
(SELECT COUNT(kuesioner.PERTANYAAN)
FROM kuesioner
WHERE inptkegiatan.IDKEGIATAN = kuesioner.IDKEGIATAN) as totalPertanyaan,
(SELECT SUM(hasilkuesioner.JAWABAN)
FROM hasilkuesioner
WHERE kuesioner.IDKEGIATAN = hasilkuesioner.IDKEGIATAN) as totalJawaban,
(SELECT COUNT(regiskegiatan.IDKEGIATAN)
FROM regiskegiatan
WHERE hasilkuesioner.IDKEGIATAN = regiskegiatan.IDKEGIATAN ) as totalUser
FROM inptkegiatan
LEFT JOIN kuesioner ON inptkegiatan.IDKEGIATAN = kuesioner.IDKEGIATAN
LEFT JOIN hasilkuesioner ON inptkegiatan.IDKEGIATAN = hasilkuesioner.IDKEGIATAN
LEFT JOIN regiskegiatan ON inptkegiatan.IDKEGIATAN = regiskegiatan.IDKEGIATAN
GROUP BY inptkegiatan.IDKEGIATAN
I tried this on my Laravel with that url, still not get it
$datatwo = DB::table('inptkegiatan')
->join('kuesioner', 'inptkegiatan.IDKEGIATAN', '=', 'kuesioner.IDKEGIATAN')
->join('hasilkuesioner', 'inptkegiatan.IDKEGIATAN', '=', 'hasilkuesioner.IDKEGIATAN')
->join('regiskegiatan', 'inptkegiatan.IDKEGIATAN', '=', 'regiskegiatan.IDKEGIATAN')
->where('IDNARASUMBER', '=', $value->PROFILEUSERS_ID)
->select('kuesioner.PERTANYAAN', 'hasilkuesioner.*', 'regiskegiatan.*',
DB::raw('count(DISTINCT(kuesioner.IDKEGIATAN)) + SUM(DISTINCT(hasilkuesioner.IDKEGIATAN)) as articles')
)
->groupBy('inptkegiatan.IDKEGIATAN')
->get();
First of all, use leftJoin instead of join, because join is alternate for innerJoin
$datatwo = DB::table('inptkegiatan')
->leftJoin('kuesioner', 'inptkegiatan.IDKEGIATAN', '=', 'kuesioner.IDKEGIATAN')
->leftJoin('hasilkuesioner', 'inptkegiatan.IDKEGIATAN', '=', 'hasilkuesioner.IDKEGIATAN')
->leftJoin('regiskegiatan', 'inptkegiatan.IDKEGIATAN', '=', 'regiskegiatan.IDKEGIATAN')
->where('inptkegiatan.IDNARASUMBER', '=', $value->PROFILEUSERS_ID)
->select([
'inptkegiatan.IDKEGIATAN',
\DB::raw('SELECT COUNT(kuesioner.PERTANYAAN)
FROM kuesioner
WHERE inptkegiatan.IDKEGIATAN = kuesioner.IDKEGIATAN) as totalPertanyaan'),
\DB::raw('(SELECT SUM(hasilkuesioner.JAWABAN)
FROM hasilkuesioner
WHERE kuesioner.IDKEGIATAN = hasilkuesioner.IDKEGIATAN) as totalJawaban'),
\DB::raw('(SELECT COUNT(regiskegiatan.IDKEGIATAN)
FROM regiskegiatan
WHERE hasilkuesioner.IDKEGIATAN = regiskegiatan.IDKEGIATAN ) as totalUser')
])
->groupBy('inptkegiatan.IDKEGIATAN')
->get();
Try with this. It should work
I use to PHP and Laravel Framework on my project.
Above code works perfectly
$users = User::select([
'users.id',
'users.name',
'users.company',
'users.country',
'users.city',
'users.email',
'users.created_at',
\DB::raw('SUM(reservations.dolar) as dolar'),
\DB::raw('count(reservations.confirmation) as confirmation'),
])->join('reservations','reservations.user_id','=','users.id')
->groupBy('reservations.user_id');
Now Counting all reservation.confirmation column but I want to count only reservation.confirmation column values 1
How I can edit
\DB::raw('count(reservations.confirmation) as confirmation'),
this code
Have you tried see in https://laravel.com/docs/5.6/queries#joins in the section "Advanced Join Clauses", you can add condition in Join function
$users = User::select([
'users.id',
'users.name',
'users.company',
'users.country',
'users.city',
'users.email',
'users.created_at',
\DB::raw('SUM(reservations.dolar) as dolar'),
\DB::raw('count(reservations.confirmation) as confirmation'),
])->join('reservations', function ($join) {
$join->on('reservations.user_id', '=', 'users.id')
->where('reservations.confirmation', '=', 1);
})->groupBy('reservations.user_id');
If you want have all sum dolar not depends with confirmation,
you can use 'case' statement:
\DB::raw('sum(case when reservations.confirmation=1 then 1 else 0 end) as confirmation')
or subquery:
\DB::raw('(Select count(*) FROM reservations WHERE user_id = users.id AND confirmation=1) as confirmation')
I'm trying to convert this query to query builder in Laravel 5.4:
SELECT
oc.id,
oc.name,
oat.user_id,
p.first_name,
p.last_name
FROM
oauth_clients oc
LEFT JOIN oauth_access_tokens oat ON oc.id = oat.client_id
JOIN users u on u.id = oat.user_id
JOIN people p on p.id = u.person_id
WHERE oc.revoked = false AND oc.password_client = true
GROUP BY oc.id, oat.user_id
And getting this error barf: Argument 1 passed to Illuminate\Database\Connection::prepareBindings() must be of the type array, string given, called in /var/www/html/source/luniverse/vendor/laravel/framework/src/Illuminate/Database/Connection.php on line 648 and defined
This is my attempt at it (one of many):
$tokens = DB::select('oc.id','oc.name','oat.user_id','p.first_name','p.last_name')
->from('oauth_clients as oc')
->leftJoin('oauth_access_tokens as oat', 'oc.id', '=', 'oat.client_id')
->join('users as u', 'u.id', '=', 'oat.user_id')
->join('people as p', 'p.id', '=', 'u.person_id')
->where('oc.revoked', '=', 'false')
->where('oc.password_client', '=', 'true')
->groupBy('oc.id')
->groupBy('oat.user_id')
->get();
The database config is set to strict mode, but that doesn't exactly seem to explain that particular error. The raw query runs fine in a DB gui.
Change the groupby code like
$tokens = DB::select('oc.id','oc.name','oat.user_id','p.first_name','p.last_name')
->from('oauth_clients as oc')
->leftJoin('oauth_access_tokens as oat', 'oc.id', '=', 'oat.client_id')
->join('users as u', 'u.id', '=', 'oat.user_id')
->join('people as p', 'p.id', '=', 'u.person_id')
->where('oc.revoked', '=', 'false')
->where('oc.password_client', '=', 'true')
->groupBy('oc.id','oat.user_id')
->get();
DB::select() executes a query, you have to use DB::table():
DB::table('oauth_clients as oc')
->select('oc.id','oc.name','oat.user_id','p.first_name','p.last_name')