Insert Form Data Into Database using Laravel

In this post we learn how to Insert Form Data Into Database using Laravel. Insert data in database using model, how to insert data in database using model in laravel, insert form data into database using laravel 8.

Insert Form Data Into Database using Laravel

Route/web.php

<?php

  use Illuminate\Support\Facades\Route;
  use App\Http\Controllers\UserController;


  Route::get('/', function () {
   return view('crud.form');
  })->name("user");

 Route::post("save_user",[UserController::class,"save_user"]);

Create view file : crud/form.blade.php

<!DOCTYPE html>
<html lang="en">
 <head>
  <title>Insert Form Data Into Database using Laravel </title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
 </head>
 <body>

  <div class="container">
   <h2 class="text-center">Insert Form Data Into Database using Laravel</h2>
   <form action="save_user" method="post" enctype='multipart/form-data'>
    @csrf
    <div class="form-group">
     <label for="email">Email:</label>
     <input type="email" class="form-control" id="email" placeholder="Enter email" name="email">
   </div>

  <div class="form-group">
   <label for="pwd">Password:</label>
   <input type="password" class="form-control" id="pwd" placeholder="Enter password" name="pwd">
 </div>

<div class="form-group">
  <label for="country">Country:</label>
  <select name="country" id="country" class="form-control">
   <option value="india">India</option>
   <option value="srilanka">SriLanka</option>
   <option value="usa">USA</option>
 </select>
</div>

<div class="form-group">
  <label>Gender : </label>
  <label for="male">Male</label>
  <input type="radio" name="gender" value="male">
  <label for="female">Female</label>
  <input type="radio" name="gender" value="female">
</div>

<div class="form-group">
  <label><strong>Hobbies :</strong></label>
  <label><input type="checkbox" name="hobbies[]" value="Reading"> Reading</label>
  <label><input type="checkbox" name="hobbies[]" value="Writing"> Writing</label>
  <label><input type="checkbox" name="hobbies[]" value="Developing"> Developing</label>
  <label><input type="checkbox" name="hobbies[]" value="Music"> Music</label>
  <label><input type="checkbox" name="hobbies[]" value="Dance"> Dance</label>
</div> 


<div class="form-group">
  <label for="photo">Photo:</label>
  <input type="file" class="form-control" id="photo" name="photo">
</div>

<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>

</body>
</html>

Create model : User.php

<?php

  namespace App;

  use Illuminate\Database\Eloquent\Model;

  class User extends Model
  {

  }

Create controller : UserController.php

<?php

  namespace App\Http\Controllers;

  use Illuminate\Http\Request;
  use Illuminate\Support\Facades\DB;
  use App\User;

  class UserController extends Controller
  {

   function save_user(Request $request){
    $img = time()."_".$request->photo->getClientOriginalName();
    $hobbies = implode(',', $request->hobbies);
    $request->photo->move(public_path('uploads'), $img);

    $user = new User;
    $user->email = $request->email;
    $user->password = $request->pwd;
    $user->country = $request->country;
    $user->gender = $request->gender;
    $user->hobbies = $hobbies;
    $user->photo = $img;

    if($user->save()){
     return redirect()->route('user');
    }
  }
}

 

Read Also :  Laravel 8 Send Email Example