Reputation: 2893
I have a Project Model and in it I have
protected $table = 'projects';
protected $guarded = [];
public function docOne()
{
return $this->hasOne('App\DocOne', 'id');
}
In the Model DocOne, I have
protected $table = 'doc_one';
protected $guarded = [];
public function project()
{
return $this->belongsTo('App\Project', 'id');
}
So it is a one to one relationship. Now I have a form which collects information to fill in the doc_one table. In the controller, I do
public function store(Request $request)
{
$input = Input::all();
DocOne::create( $input );
return Redirect::route('projects.index')->with('message', 'Document saved');
}
This fails because the database also expects the project ID. How can I get the id for the project that is creating doc_one?
Thanks
Upvotes: 1
Views: 87
Reputation: 4531
To create doc associated with the project, your route should be like this:
Route::post('projects/{id}/doc_one', 'DocOneController@store');
Then your controller's method will be:
public function store(Request $request, $id)
{
/**
* Create DocOne
*/
}
Where $id
is your project ID.
Upvotes: 1