You are currently viewing Method to insert data from a form submitted using a Blade template in Laravel

Method to insert data from a form submitted using a Blade template in Laravel

  • Post author:
  • Post category:Technology

In the Laravel Blade template, you can create a form to gather the data you want to insert into the database. Here’s an example of a form to gather data for a new user:

<form method="post" action="{{ route('users.store') }}">
    @csrf
    <div class="form-group">
        <label for="name">Name</label>
        <input type="text" class="form-control" id="name" name="name">
    </div>
    <div class="form-group">
        <label for="email">Email</label>
        <input type="email" class="form-control" id="email" name="email">
    </div>
    <div class="form-group">
        <label for="password">Password</label>
        <input type="password" class="form-control" id="password" name="password">
    </div>
    <button type="submit" class="btn btn-primary">Save</button>
</form>

In your controller, you can use the save method to insert the data from the form into the database:

use App\User;
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function store(Request $request)
    {
        $user = new User;
        $user->name = $request->input('name');
        $user->email = $request->input('email');
        $user->password = Hash::make($request->input('password'));
        $user->save();

        return redirect()->route('users.index');
    }
}