Ok, there is a difference between creating and updating articles, this is the recommended workflow:
FIrst we start with the routes
// Create an article (return the view to create an article)
Route::get('article/create', ['as' => 'article.create', 'uses' => 'ArticlesController@create']);
// Store an article (Save the article to the database)
Route::post('article/store', ['as' => 'article.store', 'uses' => 'ArticlesController@store']);
// Edit an article (return the view to edit an article)
Route::get('article/{$id}/edit', ['as' => 'article.edit', 'uses' => 'ArticlesController@edit']);
// Update an article (Update the article to the database)
Route::patch('article/{$id}', ['as' => 'article.update', 'uses' => 'ArticlesController@update']);
Now that we have the routes we create the matching functions in the controller
public function create()
{
// We only return the view here since we don't have any data for the article
return view('articles.create');
}
public function store(Request $request)
{
$article = Article::create($request->all());
// Now that we created an article we can redirect the user to either
// the index or the single view for articles.
}
public function edit($id)
{
// As you can see we pass in the $id in the function, this is the id in the url
// and the id of the Article.
$article = Article::findOrFail($id);
// Now we do have an article so we can pass that to the view
return view('articles.edit', compact('article'));
}
public function update($id, Request $request)
{
$article = Article::findOrFail($id);
$article->update($request->all());
// Redirect the user to the index of the single article view.
}
So the controller is finished, as you can see only the edit view uses an article variable. Next up the views
- views/articles/create.blade.php
// We open need to pass in the route, url or action for the form, no data needed here
{!! Form::open(['route' => 'article.create']) !!}
@include('articles.form')
{!! Form::close(); !!}
- views/articles/edit.blade.php *
// In this view the $article variable is available and we can now use it here
// The form fields will be filled because we use the model function
{!! Form::model($article, ['route' => ['article.edit', $article->id]]) !!}
@include('articles.form')
{!! Form::close(); !!}
I hope this helps ;)