I will assume that you did infact create your database correctly, this is a full solution approach:
Some information to consider, as you might run into SQL errors if you do not have the correct database names or column names:
Laravel’s naming convention for pivot tables is snake_cased model names in alphabetical order separated by an underscore. So, if one model is Feature, and the other model is Product, the pivot table will be feature_product: naming tables in many to many relationships laravel
In addition to customizing the name of the intermediate table, you may also customize the column names of the keys on the table by passing additional arguments to the belongsToMany method. The third argument is the foreign key name of the model on which you are defining the relationship, while the fourth argument is the foreign key name of the model that you are joining to: https://laravel.com/docs/9.x/eloquent-relationships#many-to-many-model-structure
class Student extends Model
{
public function subjects(){
return $this->belongsToMany(Subject::class);
}
}
Subject Model:
class Subject extends Model
{
public function students() {
return $this->belongsToMany(Student::class);
}
}
StudentController:
/**
* Show the specified model.
*
* @param IlluminateHttpRequest $request
* @param Student $student
* @return IlluminateHttpResponse
*/
public function show(Request $request, Student $student){
// This will load all of the inner relationships in a single query.
$student->load('subjects');
//Assign the loaded subjects to be used
$subjects = $student->subjects;
return view('sampleView', compact('student', 'subjects'));
}
In sampleView Blade:
<p>{{$student->name}}</p>
<!-- You call the subjects relationship and display it !-->
<ul>
@foreach($subjects as $subject)
<li>ID: {{$subject->id}} - Name: {{$subject->name}}</li>
@endforeach
</ul>
To find the subjects that Vince and Jane have in common, you can use a JOIN and WHERE clause in a SELECT statement.
SELECT su.name AS subject_name
FROM students_subjects ss
JOIN students s ON ss.student_id = s.id
JOIN subjects su ON ss.subject_id = su.id
WHERE s.name IN ('Vince', 'Jane')
GROUP BY su.name
HAVING COUNT(su.name) = 2;
This will select the names of the subjects that both Vince and Jane are enrolled in. The JOIN clauses are used to bring in the relevant data from the students and subjects tables, and the WHERE clause is used to filter the results to only include rows where the student’s name is either Vince or Jane. The GROUP BY and HAVING clauses are used to ensure that only subjects that are taken by both students are included in the final result.
The output of this query will be a list of subject names that Vince and Jane have in common.
<?php
namespace AppModels;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
class Student extends Model
{
use HasFactory;
public function subjects(){
return $this->hasMany(Subject::class);
}
}
and
<?php
namespace AppModels;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
class Subject extends Model
{
use HasFactory;
public function students(){
return $this->belongsToMany(Student::class);
}
}
and I hope you have followed laravel naming convention for database tables
If you are looking for the query, this will help you :
select s.name AS subject_name
from students_subjects ss
inner join __subjects s on s.id = ss.subject_id
where student_id in (1,3)
group by ss.subject_id
having count(1) = ( select count(1) from students where id in (1,3) );
4
Answers
I will assume that you did infact create your database correctly, this is a full solution approach:
Some information to consider, as you might run into SQL errors if you do not have the correct database names or column names:
Example:
Student Model:
Subject Model:
StudentController:
In sampleView Blade:
To find the subjects that Vince and Jane have in common, you can use a JOIN and WHERE clause in a SELECT statement.
This will select the names of the subjects that both Vince and Jane are enrolled in. The JOIN clauses are used to bring in the relevant data from the students and subjects tables, and the WHERE clause is used to filter the results to only include rows where the student’s name is either Vince or Jane. The GROUP BY and HAVING clauses are used to ensure that only subjects that are taken by both students are included in the final result.
The output of this query will be a list of subject names that Vince and Jane have in common.
Create Two Models
and
and I hope you have followed laravel naming convention for database tables
students
,subjects
,student_subject
and your query will be
Since you need both students common subjects so you should pass count to
whereHas
methodSo output will be
if you want it to be dynamic then
To understand whereHas Method params.Here is the implementation.
If you are looking for the query, this will help you :