my 3 inputs address, number and name_of_firm is showing null value but i fill the value in form.
This is my form
<form id="myForm" method="post" action="{{ route('dealer.store') }}">
#csrf
<div class="user-box">
<input type="text" name="name" required="">
<label>Name</label>
#error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="user-box">
<input type="text" name="name_of_firm" required="">
<label>Name Of Firm</label>
#error('name_of_firm')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="user-box">
<input type="text" name="number" required="">
<label>Number</label>
#error('number')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="user-box">
<input type="text" name="address" required="">
<label>Address</label>
#error('address')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="user-box">
<input type="email" name="email" required="">
<label>Email</label>
#error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="user-box">
<input type="password" name="password" required="">
<label>Password</label>
#error('password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<button class="a" id="a" value="submit" name="submit">Submit</button>
</form>
This is my controller. the name of my controller is DealerController from where i storing data in users table
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'email' => 'required',
'password' => 'required',
'name_of_firm' => 'required',
'address' => 'required',
'number' => 'required',
]);
$user = User::create([
'name' => $request->input('name'),
'email' => $request->input('email'),
'password' => Hash::make($request->input('password')),
'name_of_firm' => $request->input('name_of_firm'),
'address' => $request->input('address'),
'number' => $request->input('number'),
]);
return redirect()->route('dealer.index')->withSuccess('done');
}
this is my User model. there are only 3 field that are fillable.
protected $fillable = [
'name',
'email',
'password',
];
This is pic of database.
enter image description here
That's because of Laravel mass-assignment and you should add every field you want to fill with input in $fillable array.
change your fillable array like below:
protected $fillable = [
'name',
'email',
'password',
'name_of_firm',
'address',
'number'
];
Replace your $fillable to this
protected $guarded = [
'id',
];
It'll resolve the issue
If you are using $fillable try to add the other fields also, but if you don't want to add other fields try to use $guarded:
$fillable
protected $fillabel = [
'name',
'email',
'password',
'name_of_firm',
'address',
'number'
];
$guarded
protected $guarded = [];
Related
I want to be able to see, for example, only a certain group that is in that category among the courses that I offer on the site.
Anyone can not see the course of another group
In this way, when registering, users choose the category they are in and see the information about their group in their profile.
Should I use polymorphic?
User table
Course table
Classification table
And another table called categorizable which is polymorphic how many to how many?
please guide me
database
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('slug');
$table->integer('parent')->default(0);
$table->foreignId('category_id')->nullable()->constrained('categories')->onDelete('cascade');
$table->timestamps();
});
Schema::create('categorizables' , function(Blueprint $table) {
$table->unsignedBigInteger('category_id');
$table->unsignedBigInteger('categorizable_id');
$table->string('categorizable_type');
});
model category
public function courses()
{
return $this->morphedByMany(Course::class , 'categorizable');
}
public function users()
{
return $this->morphedByMany(User::class , 'categorizable');
}
model user and course
public function categories()
{
return $this->morphToMany(Category::class, 'categorizable');
}
form blade
<form class="g-3" method="POST" action="{{ route('register') }}">
#csrf
<div class="col-8 m-auto">
<input type="text" required class="form-control #error('name') is-invalid #enderror" id="name" name="name">
#error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="col-8 m-auto">
<input type="text" required class="form-control #error('national_code') is-invalid #enderror" id="national_code" name="national_code">
#error('national_code')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="col-8 m-auto">
<select id="inputState" class="form-select" name="category_id">
<option selected class="d-none">select category</option>
#foreach(\App\Models\Category::all() as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
</select>
</div>
<div class="col-8 m-auto">
<input type="password" required class="form-control #error('password') is-invalid #enderror" id="password" name="password">
#error('password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="col-8 m-auto">
<input type="password" required class="form-control #error('password_confirmation') is-invalid #enderror" id="password_confirmation" name="password_confirmation">
#error('password_confirmation')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<button type="submit">register</button>
</form>
controller
$data = $request->validate([
'name' => ['required', 'string', 'max:255'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
'national_code' => ['required' , New Nationalcode],
// 'g-recaptcha-response' =>['required' , New Recaptcha],
'category_id' => 'required',
]);
// $user = User::create($data);
$user = User::create([
'name' => $data['name'],
'password' => Hash::make($data['password']),
'national_code' => $data['national_code'],
// 'category_id' => $data['category_id'],
]);
$user->categories()->sync($data['category_id']);
I'm trying to create a form to insert a user but the function doesn't work the second time, only the first. I got *rror after submitting it a second time.
302 found
UserController
class UserController extends Controller
{
// User
public function edit()
{
return view(
'pages.user.edit',
[
'user' => Auth::user(),
'title' => ''
]
);
}
public function create()
{
return view('pages.user.createUS', [
'levels' => Level::get(),
'perusahaans' => Perusahaan::get(),
'satkers' => Satker::get(),
]);
}
/**
* Create a new user instance after a valid registration.
*
* #param array $data
* #return \App\Models\User
*/
public function store(Request $request)
{
// dd($validatedData);
$this->validate(request(), [
'name' => 'required',
'level_id' => 'required',
'satker_id' => 'required',
'username' => ['required', 'string', 'max:255', 'unique:users'],
'phonenumber' => ['required', 'unique:users'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
]);
$errors = $this->errors();
if ($this->fails()) {
return redirect('users/create')->withErrors($errors)->withInput()->with('err', true);
}
// dd($validatedData, $request);
$user = new User([
'name' => request('name'),
'username' => request('username'),
'email' => request('email'),
'phonenumber' => request('phonenumber'),
'level_id' => request('level_id'),
'satker_id' => request('satker_id'),
'remember_token' => Str::random(10)
]);
$user->password = md5(request('password'));
$user->save();
return redirect()->route('users.create')->with('success', 'User Berhasil Dibuat!');
}
/**
* Indicate that the model's email address should be unverified.
*
* #return static
*/
public function unverified()
{
return $this->state(function (array $attributes) {
return [
'email_verified_at' => null,
];
});
}
public function index()
{
$users = User::latest()->get();
$perusahaans = Perusahaan::get();
$satkers = Satker::latest()->get();
return view(
'pages.user.index',
[
'users' => $users,
'perusahaans' => $perusahaans,
'satkers' => $satkers,
'levels' => Level::get()
]
);
}
public function action(Request $request)
{
$pid = $request->post('pid');
$satkers = Satker::where('perusahaan_id', $pid)->latest()->get();
$html = '<option value="">Pilih Satuan Kerja</option>';
foreach ($satkers as $satker) {
$html .= '<option value="' . $satker->id . '">' . $satker->satker . '</option>';
}
echo $html;
// return response()->json(['html' => $html]);
}
// Setting Profile
public function show(User $user)
{
$details = DetailTransaksi::get();
$transaksis = Transaksi::latest()->get();
$tagihans = Tagihan::latest()->get();
$satkers = Satker::latest()->get();
// dd($tagihans->where('user_id', $user->id));
return view('pages.user.detail', [
'user' => $user,
'transaksis' => $transaksis,
'details' => $details,
'tagihans' => $tagihans,
]);
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\Models\User $user
* #return \Illuminate\Http\Response
*/
public function update(Request $request)
{
$user = Auth::user();
// $file_name = $request->image->getClientOriginalName();
$rules = [
'name' => 'required',
'username' => 'required',
'satker_id' => 'required',
'email' => 'required',
'phonenumber' => 'required',
'level_id' => 'required',
];
if (
$request->email != $user->email
) {
$rules['email'] = 'required|unique:users';
}
$validatedData = $request->validate($rules);
$validatedData['id'] = auth()->user()->id;
User::where('id', $user->id)
->update($validatedData);
return redirect('/user')->with('success', 'Profile has been Updated');
}
}
Blade
<section id="produk-form-create" class="">
<h4 class="fw-bold py-3 mb-4">
<span class="text-muted fw-light">User /</span> Tambah Data User
</h4>
<div class="col-xxl">
<div class="card mb-4">
<div class="card-header d-flex align-items-center justify-content-between">
<h5 class="mb-0">Form Tambah Produk</h5>
</div>
<div class="card-body">
<form action="/users/store" method="POST" enctype="multipart/form-data">
#csrf
<div class="row">
<div class="col-md-6 mb-3 text-start">
<label for="name" class="form-label">Nama User</label>
<input id="name" type="name" name="name" value="{{ old('name') }}"
class="form-control #error('name') is-invalid #enderror" required>
#error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="col-md-6 mb-3 text-start">
<label for="username" class="form-label">Username</label>
<input id="username" type="text" name="username" value="{{ old('username') }}"
class="form-control #error('username') is-invalid #enderror" required>
#error('username')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="col-md-6 mb-3 text-start">
<label for="email" class="form-label">Email</label>
<input id="email" name="email" value="{{ old('email') }}" type="text"
class="form-control #error('email') is-invalid #enderror" required>
#error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="col-md-6 mb-3 text-start">
<label for="leveluser" class="form-label">Level User</label>
<select class="form-select #error('leveluser') is-invalid #enderror" id="leveluser"
name="level_id" required>
#foreach ($levels as $level)
<option value="{{ $level->id }}">{{ $level->level }}</option>
#endforeach
</select>
#error('level_id')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="col-md-6 mb-3 text-start">
<label class="form-label" for="phonenumber">Phone Number</label>
<div class="input-group input-group-merge">
<span class="input-group-text">ID (+62)</span>
<input type="text" id="phonenumber" name="phonenumber" class="form-control"
placeholder="" value="{{ old('phonenumber') }}" required>
#error('phonenumber')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="col-md-6 mb-3 text-start">
<label for="perusahaan" class="form-label">Perusahaan</label>
<select class="form-select #error('perusahaan_id') is-invalid #enderror" id="perusahaan_id"
name="perusahaan_id" required>
#foreach ($perusahaans as $perusahaan)
<option value="{{ $perusahaan->id }}">{{ $perusahaan->nama_perusahaan }}
</option>
#endforeach
</select>
</div>
<input type="hidden" name="password" id="password" value="12345678">
<div class="col-md-6 mb-3 text-start">
<label for="password" class="form-label">Password</label>
<p>Password default untuk user adalah
<mark><strong
class="text-warning">"12345678"</strong></mark>
mohon arahkan kepada
user untuk mengganti password demi keamanan dan kenyamanan bersama.
</p>
</div>
<div class="col-md-6 mb-3 text-start">
<label for="satker_id" class="form-label">Satuan Kerja</label>
<select id="satker_id" name="satker_id"
class="form-select #error('satker') is-invalid #enderror" required>
#foreach ($satkers->where('perusahaan_id', 1) as $satker)
<option value="{{ $satker->id }}">
{{ $satker->satker }}
</option>
#endforeach
</select>
#error('satker_id')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="row">
<div class="col-sm-4 ">
<button type="submit" class="btn btn-primary"><i class="fw-icons bx bx-send"></i>
Send
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</section>
Routes
Route::get('users', [UserController::class, 'index'])->name('users')->middleware('userlevel');
Route::get('users/{user}/edit', [UserController::class, 'edit'])->name('users.edit')->middleware('userlevel');
Route::get('users/create', [UserController::class, 'create'])->name('users.create')->middleware('userlevel');
Route::post('/users/store', [UserController::class, 'store'])->name('users.store')->middleware('userlevel');
Route::delete('users/{user}/destroy', [UserController::class, 'destroy'])->name('users.destroy')->middleware('userlevel');
Route::put('users/{user}/update', [UserController::class, 'update'])->name('users.destroy')->middleware('userlevel');
Route::post('users/action', [UserController::class, 'action']);
Success in the first-time post but get 302 found in the second time. Please help me to solve this problem.
When I'm submitting the image, it shows the following error:
Call to a member function getClientOriginalExtension() on null
I do not know, where the mistake is.
This is my controller named ProductController:
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'brand' => 'required',
'detail' => 'required',
'size' => 'required',
'type' => 'required',
'price' => 'required',
'image' => 'required',
]);
$image = $request->file('image');
$new_name = rand().'.'.$image->getClientOriginalExtension();
$image->move(public_path('images'), $new_name);
$form_data = array(
'image' => $new_name,
'name' => $request->input('name'),
'size' => $request->input('size'),
'type' => $request->input('type'),
'price' => $request->input('price'),
'detail' => $request->input('detail'),
'brand' => $request->input('brand'),
);
Product::create($form_data);
return redirect()->route('product.index')->withSuccess('Done');
}
The error occours on this line: $new_name = rand().'.'.$image->getClientOriginalExtension();
This is my form from where I am submitting the image:
<form id="myForm" method="post" action="{{ route('product.store') }}">
#csrf
<div class="user-box">
<input type="text" name="name" required="">
<label>Name</label>
#error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="user-box">
<input type="text" name="brand" required="">
<label>Brand</label>
#error('brand')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="user-box">
<input type="text" name="price" required="">
<label>Price</label>
#error('price')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="user-box" style=" border-bottom: 1px solid white">
<span style="font-weight: bold; color: white; font-size: 16px; margin-bottom: 30px ">Size</span>
<select name="size" id="size" style="width: 40px; font-size: 16px; margin-bottom: 20px ">
<option value="50 ML">50 ML</option>
<option value="100 ML">100 ML</option>
<option value="200 ML">200 ML</option>
<option value="500 ML">500 ML</option>
<option value="1 L">1 L</option>
<option value="4 L">4 L</option>
<option value="10 L">10 L</option>
<option value="20 L">20 L</option>
</select>
#error('size')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="user-box">
<input type="text" name="type" required="">
<label>Type</label>
#error('type')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="user-box">
<input type="text" name="detail" required="">
<label>Detail</label>
#error('detail')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="user-box">
Image<input type="file" name="image" required="">
<label>Image</label>
#error('image')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<button class="a" id="a" value="submit" name="submit">Submit</button>
</form>
your server is not receiving the file that you are uploading,
Try adding the enctype='multipart/form-data' to the form in blade file.
<form id="myForm" method="post" action="{{ route('product.store') }}" enctype='multipart/form-data'>
Add attribute enctype="multipart/form-data" to your form tag like below:
<form id="myForm" method="post" action="{{ route('product.store') }}" enctype="multipart/form-data">
Route [staff] not defined.
This is my web.php (route).
Route::get('/staff', function () {
return view('staff');
});
Route::resource('/staff', StaffController::class);
This is my controller. The index, create page is in same page.
public function index()
{
$staffs = Staff::all();
return view('staff', compact('staffs'));
}
public function create()
{
return view('staff');
}
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'gender' => 'required',
'salary' => 'required',
]);
$staff = new Staff();
$staff->name = $request->name;
$staff->gender = $request->gender;
$staff->salary = $request->salary;
$staff->save();
return redirect()->route('staff')->withSuccess('Done');
}
And this is my staff.blade.php where after click on submit error occuring (route not defined).
<form id="myForm" method="post" action="{{ route('staff.store') }}">
#csrf
<div class="user-box">
<input type="text" name="name" required="">
<label>Name</label>
#error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="user-box">
<input type="text" name="gender" required="">
<label>Gender</label>
#error('gender')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="user-box">
<input type="text" name="salary" required="">
<label>Salary</label>
#error('salary')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<button class="a" id="a" value="submit" name="submit">Submit</button>
</form>
There are no any name route called staff, it would be staff.index :
return redirect()->route('staff.index')->withSuccess('Done');
See Resource Route Name
1st remove your get route all is covered by resource
Route::resource('/staff', StaffController::class);
this code generate route names like
Route Name
staff.index
staff.create
staff.store
staff.show
staff.edit
staff.update
staff.destroy
so in your code you need to do this
return redirect()->route('staff.index')->withSuccess('Done');
ref link https://laravel.com/docs/8.x/controllers#actions-handled-by-resource-controller
I want to edit form update only address, email and password. How to change password? The old password is important.
edit.blade.php
<form method="POST" action="{{ route('update') }}">
#csrf
{{ method_field('PATCH') }}
<div class="form-group row">
<label for="email" class="col-md-1 col-form-label text-md-right">{{ __('Email') }}</label>
<div class="col-md-5">
<input id="email" type="text" class="form-control #error('email') is-invalid #enderror" name="email" value="{{ old('email') ? : user()->email }}" required autocomplete="email" autofocus>
#error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="password" class="col-md-1 col-form-label text-md-right">{{ __('Password') }}</label>
<div class="col-md-5">
<input id="password" type="text" class="form-control #error('password') is-invalid #enderror" name="password" value="{{ old('password') }}" required autocomplete="password" autofocus>
#error('password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="address" class="col-md-1 col-form-label text-md-right">{{ __('Address') }}</label>
<div class="col-md-5">
<textarea id="address" type="text" class="form-control #error('address') is-invalid #enderror" name="address" required autocomplete="address" autofocus>{{ old('address') ? : user()->address }}</textarea>
#error('address')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-1">
<button type="submit" class="btn btn-block btn-primary">
{{ __('Register') }}
</button>
</div>
</div>
</form>
Route
Route::post('update', 'Auth\RegisterController#sqlupdate')->name('update');
RegisterController
public function sqlupdate(Request $request)
{
Auth::user()->update([
'address' => $request['address'],
'email' => $request['email'],
]);
$hashedPassword = auth()->user()->password;
if (Hash::check($request->oldpassword, $hashedPassword)){
$user = User::findOrFail(Auth::id());
$user->password = Hash::make($request->password);
}
return redirect()->back();
}
Just read the below code carefully :-
/**
* Admin My profile : Password update.
*
* #param Request $request
* #param $id
* #return \Illuminate\Http\Response
*/
public function updatePassword(Request $request,$id = 0)
{
$validate = Validator::make($request->all(),[
'old_password' => 'required',
'password' => 'required|confirmed|min:8',
'password_confirmation' => 'required|min:8',
]);
$getUserData = Admin::where('id',$id)->first();
if($getUserData === null) {
return redirect()->back()->with([
'status' => 'warning',
'title' => 'Warning!!',
'message' => 'Invalid Admin ID.'
]);
}
$validate->after(function ($validate) use ($request,$getUserData,$id) {
if(!Hash::check($request->get('old_password'),$getUserData->password)){
$validate->errors()->add('old_password', 'Wrong old password');
}
});
if($validate->fails()){
return redirect()->back()->withErrors($validate)->withInput();
}
try{
$getUserData->update([
'password' => Hash::make($request->get('password'))
]);
return redirect()->back()->with([
'status' => 'success',
'title' => 'Success!!',
'message' => 'Admin password updated successfully.'
]);
}catch (Exception $e){
return redirect()->back()->with([
'status' => 'error',
'title' => 'Error!!',
'message' => $e->getMessage()
]);
}
}
With the above method you'll get the idea of how we update password, this is from one of my project i've created three field for that here is the screenshot of view :-
I hope this will help
Further more update here is the small snippet for update method
specially
$getOldPassword = User::where('id',$id)->first();
if($request->get('password') === null){
$password = $getOldPassword->password;
}else{
$password = Hash::make($request->get('password'));
}