how to join three table in laravel [duplicate] - php

This question already has answers here:
Laravel join with 3 Tables
(5 answers)
Closed 1 year ago.
i have problem when joining my tables the error says Undefined variable: spps
$siswas = DB::table('siswa')
->join('kelas', 'siswa.id_kelas', '=', 'kelas.id_kelas')
->join('spp', 'spp.id_spp', '=', 'siswa.id_spp' )
->get();
$kelass = DB::table('kelas')->get();
return view('admin.data_siswa',compact('siswas','kelass'));
$spps = DB::table('spp', 'kelas')->get();
return view('admin.data_siswa',compact('siswas','spps'));
}
i dont know how to defining the spp table, any one please suggest me idea

the problem is not in the joining , the problem is with two return statement!
the exception won't reach the last return, you should simply united them.
$siswas = DB::table('siswa')
->join('kelas', 'siswa.id_kelas', '=', 'kelas.id_kelas')
->join('spp', 'spp.id_spp', '=', 'siswa.id_spp' )
->get();
$kelass = DB::table('kelas')->get();
$spps = DB::table('spp', 'kelas')->get();
return view('admin.data_siswa',compact('siswas','spps','kelass'));

pass only one parameter in DB::table($tableName);
$spps = DB::table('spp')->get();

Related

Can't get rows from DB using ->where()->get(); [duplicate]

This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 2 years ago.
I ve got table in data base called invoices and columns automonth and autoyear with values. I don't know why this function returns empty array. I want to get rows with automonth value and autoyear value. Please help me.
$autoyear = date('Y');
$automonth = date('m');
$autonumber = DB::table("invoices as invoices")
->where('automonth', '=', '$automonth')
->where('autoyear', '=', '$autoyear')
->get();
remove quotes from $automonth and $autoyear :
$autonumber = DB::table("invoices as invoices")
->where('automonth', '=', $automonth)
->where('autoyear', '=', $autoyear)
->get();
you pass your variable in a wrong way:
$autonumber = DB::table("invoices as invoices")
->where('automonth', '=', $automonth)
->where('autoyear', '=', $autoyear)
->get();
there is no need of '' when you pass the your variable names ...

Select specific column only from Laravel 5.2 Relation query not working [duplicate]

This question already has answers here:
Get Specific Columns Using “With()” Function in Laravel Eloquent
(19 answers)
Closed 2 years ago.
So I have a query that I only want to get specific columns in a relation but is not working. I'm using Laravel 5.2 by the way. Here's what I have:
$job = Job::query()->whereId($job_id)
->with([
'jobType' => function (Relation $query) {
$query->select(['name']);
},
])
->first();
If I do that, the jobType relationship returns null as seen below:
And if I'll remove the $query->select(['name']);, it has the data from job_type table. How can I just successfully get specific column from a table?
Maybe This can.. To get the specific column you need a specific jobtype
$job = Job::query()->whereId($job_id)
->with(['jobType' => function ($q) use($jobType) {
$q->where(// check the condition on jobtype table);
$q->select(['name']);
},
])
->first();

How to use 'LIKE' in Laravel? [duplicate]

This question already has answers here:
Laravel 'like' query with MongoDB connection
(1 answer)
Laravel-5 'LIKE' equivalent (Eloquent)
(7 answers)
How to query MongoDB with "like"
(45 answers)
Closed 3 years ago.
I'm trying to use 'LIKE' in Laravel. But in the output whole data is displayed. Is my method correct? If not then correct me.
$this->uses('Data');
$a ='nan';
$options = ['sort' => ['city' => 1]];
$results = $this->Part->find([],['city','LIKE',"%$a%"],$options);
return view('testEnv')->with('results',$results);
}
Query Builder
$data = DB::table('table_name')->where('col_name','LIKE','%'.$variable.'%')->get();
"%$a%" is a string ....
You can use where like
$users = DB::table('users')
->where('name', 'like', 'T%')
->get();
Read documentation here

Laravel queryBuilder (5.6)

Hello I'm trying to replicate this SQL code into Laravel qBuilder but I'm not getting the result I want in the collection:
select chars.name, class.name, specs.name
FROM characters as chars
JOIN charclasses as class
JOIN charspecs as specs
WHERE chars.class_id = class.id
AND chars.spec_id = specs.id
I get the following output:
Then I tried this in Laravel:
$charData = DB::table('characters')
->select('characters.*', 'charclasses.name', 'charspecs.name')
->join('charclasses', 'characters.class_id', '=', 'charclasses.id')
->join('charspecs', 'characters.spec_id', '=', 'charspecs.id')
->orderBy('user_id', 'ASC')
->get();
dd($charData);
and the result is:
The problem is that you are selecting three fields with the same name. These fields are added to an array in the php code, but the array field is being overwritten twice because there can be no duplicate keys in an associative array. If you want to select the three names you will have to give them another name.
$charData = DB::table('characters')
->select('characters.*', 'charclasses.name as charclassname', 'charspecs.name as charspecname')
->join('charclasses', 'characters.class_id', '=', 'charclasses.id')
->join('charspecs', 'characters.spec_id', '=', 'charspecs.id')
->orderBy('user_id', 'ASC')
->get();
dd($charData);

Join Query Sort by Related Table in Eloquent Not Quite Working

I have a few tables in my database:
Questions <-(Many to Many)-> Surveys
Questions (Has Many)-> Answers
Questions (Has Many)-> Orders *for sorting
Survey (Has Many)-> Orders *for sorting questions within a survey
My goal is to load all the questions within a given survey and sort them in a specific order. However, I cannot seem to make this work. I've tried many different variations of the query below, and it always "almost" works, but a few questions will be out of order. I'm not great with SQL so sorry if I'm missing something obvious.
$questions = Survey::find($survey->id)->questions()
->join('orders as o', 'o.question_id', '=', 'questions.id')
->orderBy('o.order', 'desc')
->groupBy('questions.id')
->get();
I'm then adding them to an array and calling them in the view with:
($survey_questions[$survey->id] as $q)
UPDATE
Well... I don't think this is probably the best solution, (so I'm not marking it as an answer) but at least it works. To get things to order correctly, I had to initially select surveys and build joins from there, then create a separate array of question_id => answers to cycle through in my view... it works, but I'm quite sure it isn't ideal.
$survey_responses = array();
$survey_questions = array();
$question_answers = array();
foreach($surveys as $survey) {
$responses = $survey->responses()->where('survey_id', '=', $survey->id)->count();
$survey_responses[$survey->id] = $responses;
$questions = Order::where('survey_id', '=', $survey->id)
->join('questions as q', 'q.id', '=', 'orders.question_id')
->select('q.*') // add anything you need here
->orderByRaw('orders.order asc')
->groupBy('q.id')
->get();
$survey_questions[$survey->id] = $questions;
//make question_id => answers array because I can't figure out the damn select query
foreach($survey->questions as $question) {
foreach($question->answers as $answer) {
$question_answers[$question->id][$answer->id] = $answer->answer;
}
}
}
When working with eloquent you need to specify the columns you want to select if using joins. Otherwise ids (and possibly other columns) will be overriden by the related table's values, and the result is wrong.
$questions = Survey::find($survey->id)->questions()
->join('orders as o', 'o.question_id', '=', 'questions.id')
->orderBy('o.order', 'desc')
->groupBy('questions.id')
->select('questions.*') // add anything you need here
->get();

Categories