Hy there! Im trying to create a user for my project.
User.php - model
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var string[]
*/
protected $fillable = [
'name','email','password','address','mobile_number', 'department_id', 'designation', 'role_id', 'img', 'start_from'
];
/**
* The attributes that should be hidden for serialization.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
UserController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
use App\Models\Department;
use App\Models\Role;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$departments = Department::get()->all();
$roles = Role::get()->all();
return view('admin.user.create', compact('departments','roles'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request,[
'firstname'=>'required',
'lastname'=>'required',
'email'=> 'required | string | email | max:255 | unique:users',
'password'=>'required|string',
'department_id'=>'required',
'role_id'=>'required',
'img'=>'required|mimes:jpeg,jpg,png',
'start_from'=>'required',
'designation'=>'required',
]);
$data = $request->all();
if($request->hasFile('img')){
$img = $request ->img->hashName();
$request->img->move(public_path('profile'),$img);
}else{
$img = 'avatar2.png';
}
$data['name'] = $request->firstname.' '.$request->lastname;
$data['img']=$img;
$data['password'] = bcrypt($request->password);
User::create($data);
return redirect()->back()->with('message','User-ul a fost creat!');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
create_users_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->string('address')->nullable();
$table->string('mobile_number')->nullable();
$table->integer('department_id');
$table->integer('role_id');
$table->string('designation');
$table->date('start_from');
$table->string('img');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
create.blade.php
@extends('admin.layouts.master')
@section('content')
<div class="container mt-5">
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item active" aria-current="page">Register employee
</li>
</ol>
</nav>
@if(Session::has('message'))
<div class="alert alert-success">
{{Session::get('message')}}
</div>
@endif
<form action="{{route('users.store')}}" method="post" enctype="multipart/form-data">@csrf
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">General Information</div>
<div class="card-body">
<div class="form-group">
<label>First name</label>
<input type="text" name="firstname" class="form-control" required="">
</div>
<div class="form-group">
<label>Last name</label>
<input type="text" name="lastname" class="form-control" required="">
</div>
<div class="form-group">
<label>Address</label>
<input type="text" name="address" class="form-control">
</div>
<div class="form-group">
<label>Mobile number </label>
<input type="number" name="mobile_number" class="form-control">
</div>
<div class="form-group">
<label>Department</label>
<select class="form-control" name="department_id" required="">
@foreach($departments as $department)
<option value="{{$department->id}}">{{$department->name}}</option>
@endforeach
</select>
</div>
<div class="form-group">
<label>Designation</label>
<input type="text" name="designation" class="form-control" required="">
</div>
<div class="form-group">
<label>Start date</label>
<input type="date" name="start_from" class="form-control" placeholder="dd-mm-yyyy" required="">
</div>
<div class="form-group">
<label>Image</label>
<input type="file" name="image" class="form-control" accept="image/*" required="">
</div>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card">
<div class="card-header">Login Information</div>
<div class="card-body">
<div class="form-group">
<label>Email </label>
<input type="email" name="email" class="form-control" required="">
</div>
<div class="form-group">
<label>Password</label>
<input type="password" name="password" class="form-control" required="">
</div>
<div class="form-group">
<label>Role</label>
<select class="form-control" name="role_id" required="">
@foreach($roles as $role)
<option value="{{$role->id}}">{{$role->name}}</option>
@endforeach
</select>
</div>
</div>
</div>
<br>
<div class="form-group">
<button class="btn btn-primary " type="submit">Submit</button>
</div>
</div>
</div>
</form>
</div>
@endsection
web.php
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::post('departments','Departmentcontroller@store');
Route::resource('departments', 'Departmentcontroller');
Route::view('hrmanagement-app','admin.create');
Route::post('roles','RoleController@store');
Route::resource('roles', 'RoleController');
Route::post('users','UserController@store');
Route::resource('users', 'UserController');
I don't know where is the problem, the other forms are working. Only this one just it's refrash the page and nothing happening -> no post to SQL DB and no erorrs shows. Please help!
UPDATE
I try the dd() function and the same thing, it refresh the page.
CodePudding user response:
In your form, you call a route with the name users.store
, here :
<form action="{{route('users.store')}}" method="post" enctype="multipart/form-data">
while in your web.php
file no route with this name. So try to change your route :
Route::post('users', 'UserController@store');
by :
Route::post('users', 'UserController@store')->name('users.store');
Normally it should work !
CodePudding user response:
It returns back because you are missing some validation