mercredi 9 septembre 2020

Laravel inserted data is not being saved into database

Hey so I have a form with an image file, I'm tying to save employee data into database. The form is in a modal box. When I press submit I get directed in an empty tab with the data I have inputed in Json format. What might have I possibly done wrong? I don't get any error...

Controller

public function insertEmployee(Request $request)
   {
      $users = new User;

      $users->name = $request->input('name');
      $users->email = $request->input('email');
      $users->password = $request->input('password');
      $users->department = $request->input('department');
      $users->salary = $request->input('salary');

      if($request->hasFile('image'))
      {
         $file = $request->file('image');
         $extension = $file->getClientOrginalExtension(); //getting image extension
         $filename = time() . '.' . $extension;
         $file->move('images/', $filename);
         $users->image = $filename;
      } else {
         return $request;
         $users->image = '';
      }

      $users->save();

      return redirect('admin/addEmployee')->with('users',$users);
   }

Html

 <div class="modal fade" id="modal-default">
        <div class="modal-dialog">
          <div class="modal-content">
            <div class="modal-header">
              <h4 class="modal-title">Add New Employee</h4>
              <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
              </button>
            </div>
            <div class="modal-body">
              <form role="form" action="/insertEmployee" method="POST">
                  
                  
                <div class="card-body">
                  <div class="form-group">
                    <label for="exampleInputText">Name</label>
                    <input type="text" class="form-control" id="exampleInputText" name="name" value="">
                  </div>
                  <div class="form-group">
                    <label for="exampleInputPassword">Password</label>
                    <input type="password" class="form-control" id="exampleInputPassword" name="password" value="">
                  </div>
                  <div class="form-group">
                    <label for="exampleInputEmail1">Email</label>
                    <input type="email" name="email" class="form-control" id="exampleInputEmail1" value="">
                  </div>
                  <div class="form-group">
                    <label for="exampleInputText">Salary</label>
                    <input type="text" class="form-control" id="exampleInputText" name="salary" value="">
                  </div>
                  <div class="form-group">
                    <label for="exampleInputText">Department</label>
                    <input type="text" class="form-control" id="exampleInputText" name="department" value="">
                  </div>
                  <div class="form-group">
                    <label for="exampleInputFile">File input</label>
                    <div class="input-group">
                      <div class="custom-file">
                        <input type="file" name="image" class="custom-file-input" id="exampleInputFile">
                        <label class="custom-file-label" for="exampleInputFile">Choose file</label>
                      </div>
                    </div>
                  </div>
                </div>
                </div>
                <div class="modal-footer justify-content-between">
                  <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                  <button type="submit" class="btn btn-primary">Save changes</button>
                </div>
              </form>
          </div>
          <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
      </div>
      <!-- /.modal -->

Routes

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');
Route::get('/logout', 'HomeController@logout');

Route::get('admin/home', 'HomeController@adminHome')->name('admin.home')->middleware('is_admin');

Route::get('admin/addEmployee', 'Admin\DashboardController@addEmployee')->name('admin.addEmployee')->middleware('is_admin');
Route::get('/editEmployee{id}','Admin\DashboardController@editEmployee');
Route::put('/updateEmployee/{id}','Admin\DashboardController@updateEmployee');
Route::delete('/deleteEmployee/{id}','Admin\DashboardController@deleteEmployee');

Route::post('/insertEmployee' , 'Admin\DashboardController@insertEmployee')->name('insertEmployee');

Model

protected $table = 'users';
    protected $fillable = [
        'name', 'email', 'password','is_admin','department','salary','image',
    ];


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire