I am trying to ask gender of the user in registration form through dropdown list but I am unable to get that value in controller.
Here is my code to register.blade.php
<!-- Gender -->
<div class="form-group">
<label for="gender" class="col-md-4 control-label">Gender</label>
<div class="col-md-6">
<select class="form-control" required="required">
<option value="male">Male</option>
<option value="female">Female</option>
</select>
</div>
</div>
Code of register controller
protected function validator(array $data)
{
return Validator::make($data, [
'username' => 'required|string|max:255',
'fullname' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
'dob' => 'required|date',
'gender' => 'required|string',
// 'gender' => 'in:male,female'
]);
}
/**
* Create a new user instance after a valid registration.
*
* #param array $data
* #return \App\User
*/
protected function create(array $data)
{
dd($data);
return User::create([
'username' => $data['username'],
'fullname' => $data['fullname'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'dob' => $data['dob'],
'gender' => $data['gender'],
]);
}
user model contains
protected $fillable = [
'username', 'email', 'password','fullname','dob','gender'
];
and the output shown when line 'gender' => 'required|string', is commented in validator function of register controller. Nothing happens when this line is uncommented
Write attribute name="gender" in the selectelement.
Without name attribute the data is not passed via GET/POST request.
Name for field was missed.
<select class="form-control" name="gender" required="required">
<option value="male">Male</option>
<option value="female">Female</option>
</select>
Related
i dont understand why, i do it in laravel 8 and it work but not in laravel 9
Store function
public function store(Request $request)
{
$ValidatedData = $request->validate([
'title' => 'required|max:255',
'slug' => 'required|unique:menus',
'categories_id' => 'required',
'deskripsi' => 'required',
'is_order' => 'required',
'most' => 'required',
'price' => 'required',
'image' => 'image|file|max:1024'
]);
if($request->file('image')){
$validatedData['image'] = $request->image->store('menus');
}
$ValidatedData['users_id'] = Auth::user()->id;
$ValidatedData['excerpt'] = Str::limit(strip_tags($request->deskripsi), 100);
Menus::create($ValidatedData);
return Redirect('/dashboard/menus')->with('success', 'Tugas Baru Telah Ditambahkan!');
}
Form
<div class="mb-3">
<label for="image" class="form-label">Size Max. 1mb</label>
<input class="form-control #error('image') is-invalid #enderror" type="file"
name="image" id="image">
#error('image')
<div class="invalid-feedback">
{{ $message }}
</div>
#enderror
</div>
.env
FILESYSTEM_DISK=public
After Create the storage location is change
I am new in laravel livewire and having a hard time doing a validation array of object.
In the application, there is an item section and the user can add item/s.
Now all the fields in the item section are required. Once the user submitted form, it did not show the error message in the item section
View
#foreach($items as $i => $item)
<tr>
<td>
<!-- <select class="form-select #error('item_selected_service.{{ $i }}') is-invalid #enderror" type="text" id="service-id" placeholder="Enter member" wire:model.defer="member_id" autofocus> -->
<select class="form-select #error('selected_service_id') is-invalid #enderror" type="text" name="selected_service_id" id="service-id" placeholder="Select Services" wire:model.defer="selected_service_id" autofocus>
<option value="">Please Select</option>
#foreach ($fees as $fee)
<option value="{{ $fee->id }}">{{ $fee->code }} - {{ $fee->name }}</option>
#endforeach
</select>
#error('selected_service_id') <div class="invalid-feedback">{{ $message }}</div> #enderror
</td>
</tr>
#endforeach
Controller
public $members;
public $fees;
public $member_id;
public $date_delivered;
public $terms;
public $date_due;
public $items = [];
public function render()
{
return view('livewire.fees.billings.billing-create');
}
public function mount()
{
$this->members = Member::where('company_id', Auth::user()->company_id)->where('status_id',22)->get();
$this->fees = Fee::where('company_id', Auth::user()->company_id)->where('status_id',20)->get();
$items = new \stdClass();
$items->selected_service_id = '';
$items->quantity = 0;
$items->amount = 0;
array_push($this->items, $items);
}
public function store()
{
$this->validate([
'member_id' => 'required',
'date_delivered' => 'required',
'terms' => 'required|numeric',
'date_due' => 'required',
'items.selected_service_id' => 'required',
]);
}
Question: How to display error message in array of object?
add like this
$this->validate([
'member_id' => 'required',
'date_delivered' => 'required',
'terms' => 'required|numeric',
'date_due' => 'required',
'items.selected_service_id' => 'required',
'member_id.required' => 'Member id is required' //Your Error message
]);
I have problem with my code somewhere that make my data $request just didn't passed to my database table (?), I'm not sure what the problem is but every time I try to submit it just redirect back to my create blade view.But when I debug it using dd($request->all()); it have everything it need.
My table have 5 columns, id, book_id, member_id, user_id, borrow_date, return_date
My Model
protected $table = "borrow";
protected $guarded = [];
public $timestamps = false;
// Relationship Book
public function book()
{
return $this->belongsTo('App\Book');
}
// Relationship Member
public function member()
{
return $this->belongsTo('App\Member');
}
My Create Controller
public function create()
{
$book= Book::all();
$member= Member::all();
return view('borrow.create', compact('book', 'member'));
}
public function store(Request $request)
{
$this->validate($request,[
'book_id' => 'required',
'member_id' => 'required',
'user_id' => 'required',
'borrow_date' => 'required',
'return_date' => 'required',
'status' => 'required'
]);
Borrow::create([
'book_id' => $request->book_id,
'member_id' => $request->member_id,
'user_id' => Auth::user()->id,
'borrow_date' => $request->borrow_date,
'return_date' => $request->return_date,
'status' => 'borrowed',
]); return redirect('/borrow');
}
My Create View
<form action="/borrow" method="POST">
#csrf
<div class="form-group row">
<label class="col-sm-2 col-form-label">Book</label>
<div class="col-sm-10">
<select data-placeholder="Enter Book Data"
data-allow-clear="1" name="book_id" id="book_id">
<option></option>
#foreach($book as $value)
<option value="{{ $value->id }}">ISBN {{ $value->isbn }} -
{{ $value->title }} ({{ $value->year }})
</option>
#endforeach
</select>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Member</label>
<div class="col-sm-10">
<select data-placeholder="Enter Member Data"
data-allow-clear="1" name="member_id" id="member_id">
<option></option>
#foreach($member as $value)
<option value="{{ $value->id }}">{{ $value->name }}
#if ($value->gender == 'man')
(M) -
#else
(W) -
#endif
{{ $value->phone }}
</option>
#endforeach
</select>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Borrow Date</label>
<div class="col-sm-10">
<input type="date" class="form-control" name="borrow_date"
id="borrow_date">
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Return Date</label>
<div class="col-sm-10">
<input type="date" class="form-control" name="return_date"
id="return_date">
</div>
</div>
<button type="submit" class="btn btn-primary">Add</button>
</form>
dd($request->all());
array:5 [▼
"_token" => "pN3PPQGpT4jmLln59tY3HBiLj27fWgf65ioIYlv0"
"book_id" => "99"
"member_id" => "99"
"borrow_date" => "2021-09-01"
"return_date" => "2021-09-30"
]
Thanks! Sorry if my English and explanation is bad
You are trying to validate a user_id and a status presents in your $request but of course it doesn't.
$this->validate($request,[
'book_id' => 'required',
'member_id' => 'required',
'user_id' => 'required',
'borrow_date' => 'required',
'return_date' => 'required',
'status' => 'required'
]);
You are using Auth::user()->id as user_id and it isn't in $request
So, just remove 'user_id' => 'required', from validation. You also don't have status in your $request so you need to remove it too. It should be like this;
$this->validate($request,[
'book_id' => 'required',
'member_id' => 'required',
'borrow_date' => 'required',
'return_date' => 'required',
]);
Use fillable in Peminjaman model
protected $fillable = [
'id', 'book_id', 'member_id', 'user_id', 'borrow_date', 'return_date'
];
try to remove user_id and status from the validation, the request doesn't have these parameters, and you are validating them as required values.
$this->validate($request,[
'book_id' => 'required',
'member_id' => 'required',
'borrow_date' => 'required',
'return_date' => 'required',
]);
When using the create() method, you are using what is called massive assignment. As per docs https://laravel.com/docs/8.x/eloquent#mass-assignment:
...before using the create method, you will need to specify either a
fillable or guarded property on your model class. These properties are
required because all Eloquent models are protected against mass
assignment vulnerabilities by default.
Saying that, you have 2 options:
1 - Keep using create() method but define fillable property in your model
protected $fillable = ['id', 'book_id', 'member_id', 'user_id', 'borrow_date', 'return_date'];
2 - Use the save() method with not need to define fillable property:
$borrow = new Borrow();
$borrow->book_id = $request->book_id;
$borrow->member_id = $request->member_id;
$borrow->user_id = Auth::user()->id;
$borrow->borrow_date = $request->borrow_date;
$borrow->return_date = $request->return_date;
$borrow->status = 'borrowed';
$borrow->save();
I am having a problem here. When a new user register I want to redirect him/her to the edit profile page... how do I change the login controller to redirect to that page?
here is my RegisterController
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use App\User;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* #var string
*/
protected $redirectTo = '/profiles/'.$user->id.'/edit';
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Get a validator for an incoming registration request.
*
* #param array $data
* #return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'type' => ['required'],
'name' => ['required', 'string', 'max:255'],
'last_name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'phone' => ['required', 'string', 'max:15', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
/**
* Create a new user instance after a valid registration.
*
* #param array $data
* #return \App\User
*/
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'phone' => $data['phone'],
'type' => $data['type'],
'password' => Hash::make($data['password']),
]);
if($data['type'] == 'Learner'){
$user->attachRole('learner');
return $user;
}
elseif($data['type'] == 'Guardian'){
$user->attachRole('guardian');
return $user;
}
elseif($data['type'] == 'Teacher'){
$user->attachRole('teacher');
return $user;
}
}
}
edit.blade.php
#extends('layouts.admin')
#section('main-content')
<!-- Page Heading -->
<h1 class="h3 mb-4 text-gray-800">{{ $user->name }} {{$user->last_name}}</h1>
<hr/>
#if (session('message'))
<div class="alert alert-success border-left-success alert-dismissible fade show" role="alert">
{{ session('message') }}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
#endif
<form method="POST" action="{{ route('profiles.update', $user->id) }}">
#csrf
#method('PATCH')
<div class="row">
<div class="col-md-9">
<div class="card shadow mb-4 px-4 pb-4">
<div class="card-header py-3 px-0">
<h6 class="m-0 font-weight-bold text-primary">User Information</h6>
</div>
#if(Auth::user()->hasRole(['superadministrator', 'administrator']))
<div class="row my-2">
<div class="col-md-2 font-weight-bold pt-2">Role:</div>
<div class="col-md-10">
<div class="form-group">
<select id="role" name="role" class="form-control #error('role') is-invalid #enderror" value="{{ old('role') }}">
<option value="">Choose user role...</option>
#foreach($roles as $role)
<option value="{{ $role->display_name }}" {{ $user->hasRole($role->name) ? 'selected' : '' }}>{{ $role->display_name }}</option>
#endforeach
</select>
#error('role')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
</div>
. . . . .
The user must come to this page after registration not to the home directory... please help
In create method change the redirect variable like this:
/**
* Create a new user instance after a valid registration.
*
* #param array $data
* #return \App\User
*/
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'phone' => $data['phone'],
'type' => $data['type'],
'password' => Hash::make($data['password']),
]);
// change redirect variable
$this->redirectTo = '/profiles/'.$user->id.'/edit';
if($data['type'] == 'Learner'){
$user->attachRole('learner');
return $user;
}
elseif($data['type'] == 'Guardian'){
$user->attachRole('guardian');
return $user;
}
elseif($data['type'] == 'Teacher'){
$user->attachRole('teacher');
return $user;
}
}
A solution :
Create a migration how add $table->boolean('profil_edited')->default(0);
Update your model to add this field on the $fillable array
Check this value :
when users login, and redirect the user to the profile page
or in a middleware
The example is based on Laravel's registration.
I have added following to register.blade.php:
<div class="form-group row">
<label for="file" class="col-md-4 col-form-label text-md-right">{{ __('Files') }}</label>
<div class="col-md-6">
<input type="file" id="files" name="files[]" multiple>
</div>
</div>
The method in RegisterController looks like this:
protected function validator(array $data)
{
$validator = Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'files.*' => ['required', 'file'],
]);
dd($validator->errors());
}
I'm trying to upload a PDF and a DOC file.:
MessageBag {#236 ▼
#messages: array:2 [▼
"files.0" => array:1 [▼
0 => "The files.0 must be a file."
]
"files.1" => array:1 [▼
0 => "The files.1 must be a file."
]
]
#format: ":message"
}
Must be a file? These are files...
just add enctype="multipart/form-data" to your form:
<form method="POST" action="{{ route('register') }}" enctype="multipart/form-data">
Try this :
protected function validator(array $data)
{
$validator = Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'files.*' => ['required', 'mimes:doc,docx,pdf,txt'],
]);
dd($validator->errors());
}