boldstar
119
16
Laravel

API post to store method return 500 internal server error

Posted 2 months ago by boldstar

Hi, i am trying to post a form to my laravel backend but I keep getting a 500 internal server error. Im using a vue frontend with axios to send the post request.

here is my route

Route::post('/engagements', '[email protected]');

here is the EngagementsController for the store method

public function store(Request $request)
    {
        $data = $request->validate([
            'return_type' => 'required|string',
            'year' => 'required|string',
            'status' => 'required|string',
            'assigned_to' => 'required|string',
            'done' => 'required|boolean'
        ]);

        $engagement = Engagement::create([
            'client_id' => client()->id,
            'return_type' => $request->return_type,
            'year' => $request->year,
            'assigned_to' => $request->assigned_to,
            'status' => $request->status,
            'done' => $request->done,
        ]);

        return response($engagement, 201);
    }

and this is the Engagement model it references

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Engagement extends Model
{
    public function client() 
    {
        return $this->belongsTo('App\Client');
    }

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

}

I believe my issue is coming from the 'client_id' that is being added for the belongsTo() relationship it has with the parent model Client.

Currently my relationships are

  1. User has many Tasks
  2. Task belongs to User
  3. Client has many Engagements
  4. Engagement belongs to Client
  5. Engagement and Tasks have a many to many relationship

So I understand that when I submit data belonging to a user like this

'user_id' => auth()->user()->id,

on the store method it will reference the user model but for some reason that does not work for the client like this

'client_id' => client()->id,

I believe i am missing a step that is telling laravel which client the new engagement belongs to but Im not sure what that step is..

Any help would be greatly appreciated!!!

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

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.