TJ Weems
TJ Weems

Reputation: 1114

Storing New Record Through Pivot Table In Laravel

I 'm trying to create a new record when I submit my form data from my front end and I am struggling to wrap my head around how I can do it. I've looked for examples and have had no luck yet in finding something. So I am submitting data for a new engagement and my engagements share a pivot table relationship with tasks. Here is the relationship structure.

  1. User hasMany Tasks
  2. Task belongsTo User
  3. Client hasMany Engagements
  4. Engagement belongsTo Client
  5. Task belongsToMany Engagements (and of course vise versa)

If I submit data to store a New Engagement and I also want to store a New Task at the same time I am struggling to understand what method I should use to accomplish this.

I do understand that I need the Engagement and Task to exist prior to the pivot table being populated.

EngagementsController.php (to store a new engagement)

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

    $engagement = Engagement::create([
        'client_id' => $request->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);
}

After it runs the $engagement = Engagement I am wondering If I can use the data for the assigned_to field for both the Engagement and the Task like this?

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

$data = $request->assigned_to;
$task->tasks()->create($data);

Engagement Model

class Engagement extends Model
{
    protected $fillable =
    [
        'client_id',
        'return_type',
        'year',
        'assigned_to',
        'status',
    ];
    public function client() 
    {
        return $this->belongsTo('App\Client');
    }

    public function tasks()
    {
        return $this->belongsToMany('App\Task', 'engagement_task', 'engagement_id', 'task_id');
    }

    public function questions()
    {
        return $this->hasMany('App\Question');
    }

}

Task model

class Task extends Model
{
    protected $fillable = ['user_id'];

    public function user()
    {
        return $this->belongsTo('App\User');
    }

    public function engagements()
    {
        return $this->belongsToMany('App\Engagement', 'engagement_task', 'task_id', 'engagement_id');
    }
}

Upvotes: 0

Views: 257

Answers (1)

Thijs
Thijs

Reputation: 1012

You can do $task = Task::create(['user_id' => $request->get('assigned_to')]).

Next up you need to attach the task to the engagement, $engagement->tasks()->attach($task->id);

By the way, you can clean up your controller a bit by validating the data in a form request.

Next you can create your engagement, by $engagement = Engagement::create($engagementRequest->validated())

Upvotes: 1

Related Questions