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?
Related
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 am trying to configure a query within a Laravel app that is equivalent to this:
SELECT SUM(balance), name FROM db.statement_versions
INNER JOIN statements ON statement_versions.statement_id = statements.id
INNER JOIN accounts ON statements.account_id = accounts.id
GROUP BY name;
This query works when I run it in MySQL Workbench, but when I try to translate it into PHP with the Laravel query builder I am getting an error. What I ultimately want is to return all accounts with their summed balance of statement_versions.balance. Here is my code right now:
public static function query(LensRequest $request, $query)
{
return $request->withOrdering($request->withFilters(
$query->select('accounts.name')->sum('statement_versions.balance')
->join('statements', 'statement_versions.statement_id', '=', 'statements.id')
->join('accounts', 'statements.account_id', '=', 'accounts.id')
->orderBy('balance', 'desc')
->groupBy('statement_versions.balance', 'accounts.name')
));
}
I have tried a couple different variations of this, but I get the error SQLSTATE[42S22]: Column not found: 1054 Unknown column 'statement_versions.balance' in 'field list'. How can I solve this and get the query working correctly?
Not having your tables it will be a bit hard, but I hope that this will give you a path to what you want to achieve, it might be luck that it will work from the first shot :)
DB::table('statement_versions as sv')
->select([
'name',
DB::raw('sum(balance) as total')
])
->join('statements as s', 'sv.statement_id', '=', 's.id')
->join('accounts as a', 's.account_id', '=', 'a.id')
->groupBy('name');
Fellow Coders,
I'm trying to use an inner join query to get multiple data from my relative tables.. What i'm trying to do is to get the (project_id as company_name - subproject_id as subproject_title). I tried to use the query as I will state below. Also I will post printscreens of my tables.
$values = DB::table('hour_registrations')->join('projects', 'id', '=', 'id')->join('subprojects','id', '=', 'id')->select('projects.*', 'id', 'subprojects.id')->get();
I would love some help with my question as I do not understand what to do now..
I think this should do the trick (if I understood your table structure correctly).
I would encourage you to read the official Laravel documentation on Joins.
$query = DB::table('projects')
->join('subprojects', 'projects.id', '=', 'subprojects.project_id')
->join('companies', 'projects.company_id', '=', 'companies.id')
->select('companies.company_name', 'projects.id', 'subprojects.id', 'subprojects.title')
->get();
I'm trying to select a number of columns along with MAX. The raw query would be something like: SELECT users.id, ..., MAX(ur.rank) AS rank but I cannot figure out how to do it using the query builder supplied by Laravel in Eloquent.
This is my attempt:
$user = User::join('users_ranks AS ur', function($join) {
$join ->on('ur.uid', '=', 'users.id');
})
->where('users.id', '=', 7)
->groupBy('users.id')
->first(['users.id', 'users.username', 'MAX(ur.rank) AS rank']);
I simply cannot figure it out. What I want to achieve is I'm selecting a user where users.id = 7, and I'm wanting to select the MAX rank that's in users_ranks where their users_ranks.uid = users.id.
I was told to avoid sub-queries as when working with large result sets, it can slow things down dramatically.
Can anyone point me in the right direction? Thanks.
I think you should rewrite it like this:
DB::table('users')
->select(['users.id', 'users.username', DB::raw('MAX(ur.rank) AS rank')])
->leftJoin('users_ranks AS ur', 'ur.uid', '=', 'users.id')
->where('users.id', '=', 7)
->groupBy('users.id')
->first();
No sense to use User:: if you use table names later and want to fetch not all of the fields ( 'users.id', 'users.username' ).
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();