Laravel Insert Data into Table without Model

In this post we have learn Laravel Insert Data into Table without Model.  Insert data into database manually inside a laravel 8 controller.

Laravel Insert Data into Table without Model

route/web.php

<?php

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


  Route::get('/', function () {
   return view('crud.form');
  });

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

crud/form.blade.php

<!DOCTYPE html>
<html lang="en">
 <head>
  <title>Laravel Insert Data into Table without Model </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">Laravel Insert Data into Table without Model</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>

UserController.php

<?php

  namespace App\Http\Controllers;

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

  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);
      DB::insert('insert into users ( email,password,country,gender,hobbies,photo) values (?, ?,?,?,?,?)', [$request->email,$request->pwd,$request->country,$request->gender,$hobbies,$img]);
  }
}