//form.blade.php
<form method="post" action="{{ route('submitForm') }}">
{!! csrf_field() !!}
<input type="text" name="name"/>
<input type="text" name="last_name"/>
<input type="text" name="email"/>
<input type="text" name="phone"/>
<input type="text" name="address"/>
</form>
<?php
//app/Http/controllers/YourController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
//user is your eloquent model
class YourController extends Controller
{
//show user form
public function showForm()
{
return view('form');
}
//post url for submit form
public function submitForm(Request $request){
$this->validate($request, [
'name' => 'required|min:2|max:30',
'lastname' => 'required|min:2|max:30',
'email' => 'required',
'phone' => 'required',
'address' => 'required'
]);
$user = new User();
$user->name = $request->name;
$user->lastname = $request->lastname;
$user->email = $request->email;
$user->phone = $request->phone;
$user->address = $request->address;
try{
$user->save();
return redirect()->route('showAllusers')->with('success', "User was successfully created..!");
}
catch(Exception $e){
return redirect()->back()->with('error', "Could not save the user!");
}
return redirect()->back()->with('error', "Error Occured, please try again!");
}
// after form submit, redirect to this page where all users will be showcased
public function showAllusers()
{
$users = User::all();
return view('allusers', compact('users'));
}
}
?>
//allusers.blade.php
@if($users->count > 0)
<table>
<thead>
<tr>
<th> id</th>
<th> name</th>
<th> last name </th>
<th> email </th>
<th> phone</th>
<th> adddress </th>
</tr>
</thead>
<tbody>
@foreach($users as $user)
<tr>
<td> {{$user->id}} </td>
<td> {{$user->name}} </td>
<td> {{$user->last_name}} </td>
<td> {{$user->email}} </td>
<td> {{$user->phone}} </td>
<td> {{$user->address}} </td>
</tr>
@endforeach
</tbody>
</table>
@else
<p> No users found..</p>
@endif
//routes.php
<?php
Route::get('/showForm', 'YourController@index')->name('showForm');
Route::post('/submitForm', 'YourController@index')->name('submitForm');
Route::get('/showAllusers', 'YourController@index')->name('showAllusers');
?>