joining multiple tables using laravel - php

Hi I am trying to do the following query using join in laravel.
select advertisers.advertiserName,brands.brandName
,campaigns.campaignName,banner_creatives.bannerName
from vicinity_adman_dev.advertisers
join vicinity_adman_dev.brands
join vicinity_adman_dev.campaigns
join vicinity_adman_dev.banner_creatives
on advertisers. advertiserId = brands.advertiserId
where
banner_creatives.campaignId = campaigns.campaignId
What i DID so far:
$campaigns =DB::table('brands')
->join('advertisers','brands.advertiserId' , '=','advertisers.advertiserId'
->join('campaigns','banner_creatives.campaignId',
'=','campaigns.campaignId')
select('advertisers.advertiserName','brands.brandName','campaigns.campaignName','banner_creatives.bannerName')
->get();
getting error on second join "banner_creatives.campaignId"
thanks in advance

You missed one of your joins. Just add it to the others
$campaigns = DB::table('brands')
->join('advertisers', 'brands.advertiserId', '=', 'advertisers.advertiserId')
->join('campaigns', 'banner_creatives.campaignId', '=', 'campaigns.campaignId')
->join('banner_creatives', 'banner_creatives.bannerCreativeId', '=', 'other_table.bannerCreativeId')
->select(
'advertisers.advertiserName',
'brands.brandName',
'campaigns.campaignName',
'banner_creatives.bannerName'
)
->get();

Related

raw query join left with select to laravel 8 query

i'm using raw query on my laravel function, and i wanna make it into laravel query builder, but i really have no idea how to do it, i've read laravel documentary about advanced join clause or subquery joins, but still cant figuring it out how to convert it
raw query :
$buku = DB::select(
DB::raw('
SELECT buku.*, kategory, tag
FROM buku
LEFT JOIN kategori_buku ON buku.id_kategori = kategori_buku.id
LEFT JOIN detail_buku_tag ON buku.id = detail_buku_tag.id_buku
LEFT JOIN (SELECT tag_buku.id, GROUP_CONCAT(tag) AS tag FROM tag_buku) AS tag_buku ON tag_buku.id = detail_buku_tag.id_tag
GROUP BY buku.id')
);
there some sql chaining issue. #Ramiz Kongulov forget to split group by clause try this.
$buku = \DB::table('buku')
->select(['buku.*','kategory.*', 'tag.*'])
->leftJoin('kategori_buku', 'kategori_buku.id', '=', 'buku.id_kategori')
->leftJoin('detail_buku_tag', 'detail_buku_tag.id_buku', '=', 'buku.id')
->leftJoin(\DB::raw('(SELECT tag_buku.id, GROUP_CONCAT(tag) AS tag FROM tag_buku) AS tag_buku'),
'tag_buku.id', '=', 'detail_buku_tag.id_tag')
->groupBy('buku.id')
->get();
try this
$buku = \DB::table('buku')
->select([
'buku.*',
'kategory.*',
'tag.*'
])
->leftJoin('kategori_buku', 'kategori_buku.id', '=', 'buku.id_kategori')
->leftJoin('detail_buku_tag', 'detail_buku_tag.id_buku', '=', 'buku.id')
->leftJoin(DB::raw('SELECT tag_buku.id, GROUP_CONCAT(tag) AS tag FROM tag_buku) AS tag_buku ON tag_buku.id = detail_buku_tag.id_tag GROUP BY buku.id'))
->get();

laravel query problem using join, what is missing?

first of all Im not good at english and im new using laravel framework. Im developing some school site for thesis purposes. And I think this might be a chance for me to learn laravel since many developers recommends laravel.
back to my problem hehe
This query runs right.
$questions = DB::table('questions')
->select('questions.id','questions.role','questions.postBody', 'questions.classCode', 'questions.postedById', 'questions.created_at', 'classroom.classroomName','classroom.icon','users.picture', 'students.firstName', 'students.middleName', 'students.lastName', 'students.suffix','students.studentNumber')
->join('classroom', 'classroom.classCode', '=', 'questions.classCode')
->join('users', 'users.id', '=', 'questions.postedById')
->join('students', 'students.email' , '=', 'users.email')
->orderBy('questions.created_at', 'DESC')
->skip($request->page - 5)
->take($request->page)
->get();
and when I adding other table ("teacher") i got no returns. This is the example of my query trying to join 'teacher' table and in here, im trying to select email of teacher
$questions = DB::table('questions')
->select('questions.id','questions.role','questions.postBody', 'questions.classCode', 'questions.postedById', 'questions.created_at', 'classroom.classroomName','classroom.icon','users.picture', 'students.firstName', 'students.middleName', 'students.lastName', 'students.suffix','students.studentNumber','teacher.email')
->join('classroom', 'classroom.classCode', '=', 'questions.classCode')
->join('users', 'users.id', '=', 'questions.postedById')
->join('students', 'students.email' , '=', 'users.email')
->join('teacher', 'teacher.email' , '=', 'users.email')
->orderBy('questions.created_at', 'DESC')
->skip($request->page - 5)
->take($request->page)
->get();
sorry for my bad english.
Can anyone help me? Thank you very much!
The problem is that after your first inner join there are only useres left that are also students. Because inner join is taking the cut-set of the joined tables:
https://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins
In the next innerjoin you are trying to get the cut-set of email addresses that are in the students and also teacher table. This will probably always be empty.
I recommend that you look up DB joins and I would use leftJoin for your use case.
->leftJoin('students', 'students.email' , '=', 'users.email')
->leftJoin('teacher', 'teacher.email' , '=', 'users.email')
Here are some good answers on the topic:
What's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN?

How to join tables with more than one attribute match?

I am trying to turn my raw sql into laravel query builder and I encounter difficulty on how to join multiple tables using with many attributes match.
In this case, I want to join the table jr_h and jr_d with three attributes match (book,p_seq and staff_code) rather than one (book).
Raw sql:
$sql = "select from_time,to_time,t.staff_code,s.name_t as staff_name,t.book,t.p_code,t.p_seq,p.hrs1,s.img_file,
t.hrs_work,p.sharing_cnt as hrs_work, t.hrs_ot as hrs_ot from jr_d as t
inner join jr_h as p on(t.book=p.book and t.p_seq=p.p_seq and t.staff_code=p.staff_code)
inner join astaff as s on(t.staff_code=s.staff_code) ";
Laravel query builder:
$jr_d = DB::table('jr_d')
->join('jr_h', 'jr_d.book', '=', 'jr_h.book')
->join('astaff', 'jr_d.staff_code', '=', 'astaff.staff_code')
->select('jr_h.*','jr_d.*','astaff.*','astaff.name_t as staff_name')
->where('jr_d.ref_group','=','E')
->get();
and also want to know if there is a way to make the query faster since it has a lot of data in the tables.
Laravel joins with multiple conditions:
$results = DB::table('jr_d')
->select('jr_h.*','jr_d.*','astaff.*','astaff.name_t as staff_name')
->join('jr_h', 'jr_d.book', '=', 'jr_h.book')
->join('jr_h as p', function($query){
$query->on('t.book','=', p.book');
$query->on('t.p_seq','=', 'p.p_seq');
$query->on('t.staff_code', '=', 'p.staff_code');
})
->where('jr_d.ref_group','=','E')
->get();
`
Try this:
// ...
->join('jr_h p', function($join) {
$join->on('t.book', '=', 'p.book');
$join->on('t.p_seq', '=', 'p.p_seq');
// ... more conditions
});
Try this.
$jr_d = DB::table('jr_d')
->join('jr_h', 'jr_d.book', '=', 'jr_h.book')
->join('astaff', 'jr_d.staff_code', '=', 'astaff.staff_code')
->select('*','astaff.name_t as staff_name')
->where('jr_d.ref_group','=','E')
->get();

How to create SQL Join Statement in Laravel Controller

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();

Convert SQL to Eloquent to Join multiple table and Count

I have been this SQL working fine, but trying to convert it to Eloquent format, it keeps returning a wrong, SQL entirely which flags error.
Coding with Laravel 5.5
Select arm_articles.article_topic, arm_articles.id, arm_articles.article_id,
COUNT(arm_article_views.view_article_id) AS TotalViews,
COUNT( arm_article_likes.liked_article_id) AS TotalLikes,
COUNT( arm_article_comments.comment_article_id) AS TotalComments
FROM arm_articles
LEFT JOIN arm_article_views ON arm_articles.article_id = arm_article_views.view_article_id
LEFT JOIN arm_article_likes ON arm_articles.article_id = arm_article_likes.liked_article_id
LEFT JOIN arm_article_comments ON arm_articles.article_id = arm_article_comments.comment_article_id
GROUP BY arm_articles.article_id
ORDER BY TotalLikes, TotalLikes, TotalComments ASC`
HOW I MADE ELOQUENT QUERY BUILDER Not working though:
return Datatables::of(PostModel::leftJoin('arm_article_views', 'arm_article_views.view_article_id', '=', 'arm_articles.article_id')
->leftJoin('arm_article_likes','arm_article_likes.liked_article_id', '=', 'arm_articles.article_id')
->leftJoin('arm_article_comments', 'arm_article_comments.comment_article_id','=','arm_articles.article_id')
->selectRaw(
'arm_articles.*,
count(arm_article_views.view_article_id) AS ViewCount'
)
->groupBy('arm_articles.article_id')->orderBy('ViewCount','DESC')
->where('arm_articles.article_contributor_id','=',$contributor_id)
->getQuery())->make(true);
Any hint would be appreciated
Try with this:
DB::table('arm_articles')
->leftJoin('arm_article_views', 'arm_articles.article_id', '=', 'arm_article_views.view_article_id')
->leftJoin('arm_article_likes', 'arm_articles.article_id', '=', 'arm_article_likes.liked_article_id')
->leftJoin('arm_article_comments', 'arm_articles.article_id', '=', 'arm_article_comments.comment_article_id')
->groupBy('arm_articles.article_id', 'article_topic')
->select(
DB::raw('count(arm_article_views.view_article_id) as TotalViews'),
DB::raw('count(arm_article_likes.liked_article_id) as TotalLikes'),
DB::raw('count(arm_article_comments.comment_article_id) as TotalComments'),
'arm_articles.article_id',
'article_topic'
)->get();

Categories