First you will have to have a route for each action, let's say that in the path create
is where we create our student and the path edit
where we update the data of our students.
Route::get('student/create', 'StudentController@create');
Route::get('student/update', 'StudentController@edit');
Now we go to the controller, we create the two methods for the routes and in each method we will return a different view, but share a subview that will be the form.
class StudentController extends Controller
{
public function create()
{
return view('student.create');
}
public function edit()
{
return view('student.update');
}
}
Now let's create our views. We create three the vita create
the view edit
and one more that we will call form
.
// Esta seria la vista 'create', la cual tendria una formulario
// al metodo 'store' donde guardariamos nuestro usuario.
<form method="POST" action="{{ action('StudentController@store') }}">
{{ csrf_field() }}
@include('student.form')
</form>
// Esta seria la vista 'edit', la cual tendria una formulario
// al metodo 'update' donde actualizariamos nuestro usuario.
<form method="POST" action="{{ action('StudentController@update', $student) }}">
{{ csrf_field() }}
{{ method_field('put') }}
@include('student.form')
</form>
Finally, we created the form
view where we used the form to create and update our users.
<input name="name" value="{{ $student->name or old('name') }}" type="text">
<input name="date_birth" value="{{ $student->date_birth or old('date_birth') }}" type="date">
The trick is to combine the logic or
with the method old
, Ex: {{ $student->name or old('name') }}
. This way, we get that when there is a user, that is, when we update it, it will show us its non-name and if there is no user it will show the data of the request, that if it does not exist it will return null.