Basic Form Handling
in routes/web.php add:
- Laravel will use get or post automatically as the code inside the function will required.
Route::get('/projects', 'ProjectsController@index');
Route::post('/projects', 'ProjectsController@store');
inside controller (at ProjectsController in this example)
- It’s return all entered information
public function store()
{
return request()->all();
// or you can return any form information with:
// return request('title');
}
inside the web page call with: ” {{ csrf_field() }} ” – this is a security helper that put hidden input like this:
<input type="hidden" name="_token" value="qPc0L8dtVwKnkagCdvaWcKgVfzLUkj3DwWFzZ7Cm">
<form method="POST" action="/projects">
{{ csrf_field() }}
<div>
<input type="text" name="title" placeholder="Project title">
</div>
<div>
<textarea name="description" placeholder="Project description"></textarea>
</div>
<div>
<button type="submit">Create Project</button>
</form>
Advance Form Handling
Create a project and store the data to Database
public function store()
{
//return request()->all();
//return request('title');
$project = new Project();
$project->title = request('title');
$project->description = request('description');
$project->save();
return redirect('/projects');
}