简体   繁体   中英

How to add records to a pivot table in Laravel

I have a user, student and subject model and I want to register a student into many subjects. So I created a StudentRegistration controller and in my create view I show all the subjects that belong to the course of the current logged in user.

StudentRegistration.php create function

public function create()
{
    $user_id = Auth::user()->id;
    $student_id = Student::where('user_id', $user_id)->first();
    $course = $student_id->course->id;
    $subjects = Subject::where('course_id', $course)->get();

    return view('student.create', compact('subjects'));

}

In the create template I show all the subjects as checkbox because a user can register for multiple subjects.

{!! Form::open(['method' => 'POST', 'action'=>'StudentRegistration@store', 'files'=>true]) !!}
    @foreach($subjects as $subject)
    <div class="label-box">
        {!! Form::label('name', $subject->name) !!}
        {!! Form::checkbox('subject_id[]', $subject->id, null, ['class'=>'form-control']) !!}
    </div>
    @endforeach
    <div class="form-group">
        {!! Form::submit('Create User', ['class'=>'btn btn-primary']) !!}
    </div>      
{!! Form::close() !!}

I have this in my Student.php for the many to many relationship:

public function subjects()
{
    return $this->belongsToMany('App\Subject');
}

I created a pivot table named Student_Subject. So, during the store, how can I save all the selected subjects into pivot table ( student_subject ).

I tried using this:

public function store(Request $request)
{
    $data = $request->except('_token');
    $subject_count = count($data['subject_id']);
    for($i=0; $i < $subject_count; $i++){
       $student = Student::where('user_id', Auth::user()->id);
       $student->subjects()->attach($data['subject_id'][$i]);
    }
}

But I get the following error:

"Method Illuminate\Database\Query\Builder::subjects does not exist."

And how can I view all the course subjects which the student is not registered at?

I have this:

Route::get('/studentsubjects', function(){
    $student_id = Student::where('user_id', Auth::id())->first();
    $course = $student_id->course->id;
    $subjects = $student_id->subjects;

    echo 'Registered at' .'<br>';
    foreach ($subjects as $registered) {
        echo $registered->name .'<br>';
    }

    $unregistered = Subject::where('course_id', $course)->except($subjects);
});

And see this error:

"Method Illuminate\Database\Query\Builder::except does not exist."
$student = Student::where('user_id', Auth::user()->id);

is not enough to get the Student model, you're only getting the query object here.

In order to actually get the student, use the following:

$student = Student::where('user_id', Auth::user()->id)->first();

Even better if user_id is the primary key for your model Student :

$student = Student::find(Auth::user()->id);

As a side note, you can access directly the user ID from the Auth interface using Auth::id() instead of Auth::user()->id , resulting in:

$student = Student::find(Auth::id());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM