samedi 12 novembre 2016

Laravel: How can I save to foreign table from a single post?

I have a "primary" application (not laravel) and a "secondary" application (Laravel).

I am saving user data to my laravel app via api call. Everything is working great when things are 1:1. Ex: Name->name, Email->email etc.

I am not sure how to handle 1:many though. I have a students table where all of the 1:1 things are stored. I also have an activities table, where a user could have 1:many.

For example, a user could belong to band, choir, baseball.

My api call looks like this:

http://ift.tt/2g5BFcO;...

It's where I want to store the activities I am hung up. Do I send an array of activities to Laravel? Or do I have multiple activities parameters?

This is what my controller looks like:

StudentController

$data = new Student;
$data->fill($request->all());

return response()->json(['success' => $data->save()], 200);

Student

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



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire