naekobest
5 months ago
1012
17
Eloquent

Store multiple relations in one table

Posted 5 months ago by naekobest

Im currentlly struggling with relations (again)

This

$exam = auth()->user()->exams()->create($this->validateData());
protected function validateData()
    {
        return request()->validate([
            'name' => 'required',
            'lehrgang' => 'required',
            'abbr' => 'required'
        ]);
    }

works just fine to put the user_id into my exam table. I just did it for testing purpose...

Now i want to use it on my subject table

Subject Controller

public function store(Exam $exam)
    {
        $data = request()->validate([
            'name' => 'required',
            'abbr' => 'required'
        ]);

        $subject = $exam->subjects()->create($data);

        return redirect('/exam/'.$exam->id);
    }

I dont understand how to put php auth()->user()this into my function. The subject belongsTo an exam (which already works just fine without the user_id wannabe implementation)

Please sign in or create an account to participate in this conversation.