CodeIgniter Laravel PHP Example HTML Javascript jQuery MORE Videos New

How to insert data using Laravel, Ajax



UserDataController.php (controller)

namespace App\Http\Controllers;

use App\UserData;
use Illuminate\Http\Request;

class UserDataController extends Controller
public function create()
        return view('userData.create');

public function store(Request $request)
            'name' => 'required',
            'email' => 'required',
        return json_encode(array(

UserData.php (Model)

namespace App;

use Illuminate\Database\Eloquent\Model;
class UserData extends Model
    protected $table = 'users_info';
    protected $fillable = [
        'type','name', 'email','phone','city'


<div class="row">
    <div class="col-lg-12 margin-tb">
        <div class="pull-left">
            <h2>Add New User</h2>
        <div class="pull-right">
            <a class="btn btn-primary" href="{{ route('userData.index') }}"> Back</a>
@if ($errors->any())
    <div class="alert alert-danger">
        <strong>Whoops!</strong> There were some problems with your input.<br><br>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>

<div class="form-group">
        <input type="hidden" name="_token" id="csrf" value="{{Session::token()}}">
    <label for="email">Name:</label>
    <input type="text" class="form-control" id="name" placeholder="Enter Name" name="name">
  <div class="form-group">
    <label for="email">Email:</label>
    <input type="email" class="form-control" id="email" placeholder="Enter Email" name="email">
  <div class="form-group">
    <label for="email">Phone:</label>
    <input type="text" class="form-control" id="phone" placeholder="Enter Phone" name="phone">
  <div class="form-group">
    <label for="email">City:</label>
    <input type="text" class="form-control" id="city" placeholder="Enter City" name="city">
  <button type="submit" class="btn btn-primary" id="butsave">Submit</button>
$(document).ready(function() {
    $('#butsave').on('click', function() {
      var name = $('#name').val();
      var email = $('#email').val();
      var phone = $('#phone').val();
      var city = $('#city').val();
      var password = $('#password').val();
      if(name!="" && email!="" && phone!="" && city!=""){
        /*  $("#butsave").attr("disabled", "disabled"); */
              url: "/userData",
              type: "POST",
              data: {
                  _token: $("#csrf").val(),
                  type: 1,
                  name: name,
                  email: email,
                  phone: phone,
                  city: city
              cache: false,
              success: function(dataResult){
                  var dataResult = JSON.parse(dataResult);
                    window.location = "/userData";				
                  else if(dataResult.statusCode==201){
                     alert("Error occured !");
          alert('Please fill all the field !');


<!DOCTYPE html>
<html lang="{{ app()->getLocale() }}">
    <!-- Fonts -->
    <link href=",600" rel="stylesheet" type="text/css">
    <link rel="stylesheet" href="">
    <script src=""></script>
    <script src=""></script>
    <!-- Style -->
        .input-box { position: relative; }
        .unit { position: absolute; display: block; left: 5px; top: 10px; z-index: 9; }
        .fixedTxtInput { display: block; border: 1px solid #d7d6d6; background: #fff; padding: 10px 10px 10px 30px; width: 100%; }

<div class="container">