i have three columns in my database ,
i need to make a validation for these columns ,
and to make the tree of them unique.
Validator::make($request->all(),
[
'name' => 'required|min:4|string',
'color'=>'required',
'size_id'=>'required',
]);
i already tried
Validator::make($request->all(),
[
'name' => 'required|min:4|string|unique:products,name,color,size_id',
'color'=>'required|unique:products,color,name,size_id',
'size_id'=>'required|unique:products,size_id,name,color',
]);
and i tried this
$color = $request->input('color');
$size_id = $request->input('size_id');
$name = $request->input('name');
// dd($name);
return
Validator::make($request->all(),
[
'name' => 'required|min:4|string|unique:products,name,'.'NULL'.',color,'.$color.',size_id,'.$size_id,
'description'=>'required|min:4',
'quantity'=>'required|numeric',
'subcategory_id'=> 'required',
'category_id'=> 'required',
'price'=> 'required|numeric',
'color'=>'required|unique:products,color,'.'NULL'.',name,'.$name.',size_id,'.$size_id,
'size_id'=>'required|unique:products,size_id,'.'NULL'.',name,'.$name.',color,'.$color,
'cover'=>'required|image:png,jpg,jpeg',
// 'images'=>'image:png,jpg,jpeg',
'images.
*'=>'image:png,jpg,jpeg',
]);
but it return undefined offset 3 when i check if it fails or not!
thanks
$color = $request->get('color');
$size_id = $request->get('size_id');
$name = $request->get('name');
// dd($name);
return
Validator::make($request->all(),
[
'name' => 'required|min:4|string|unique:products,name,NULL,id,color,'.$color.',size_id,'.$size_id,
'description'=>'required|min:4',
'quantity'=>'required|numeric',
'subcategory_id'=> 'required',
'category_id'=> 'required',
'price'=> 'required|numeric',
'color'=>'required|unique:products,color,NULL,id,name,'.$name.',size_id,'.$size_id,
'size_id'=>'required|unique:products,size_id,NULL,id,name,'.$name.',color,'.$color,
'cover'=>'required|image:png,jpg,jpeg',
// 'images'=>'image:png,jpg,jpeg',
'images.*'=>'image:png,jpg,jpeg',
]);
the code above solved my problem
Related
I want to know how can I save two models which has one relationship in my models. I don't know how to save it but I need the ID for my patient model to assign to my scale model.
Here is my PatientController.
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required',
'birth' => 'required',
'nacionality' => 'required',
'adress' => 'required',
'phone' => 'required',
'living_method' => 'required',
'responsable' => 'required',
]);
//Create Patient
$patient = new Patient;
$scales = new Scale;
$patient->name = $request->input('name');
$scales->sd_abc_1_old = $request->input('sd_abc_1_old');
$patient->save();
return redirect('/patients')->with('success', 'Paciente creado.');
}
I try this code but get error.
DB::transaction(function() use ($patient, $scales) {
$patient = $patient->save(); //Patient Exists First
Patient::find($patient->id)->scales()->save($scales)
});
You could try something like this:
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required',
'birth' => 'required',
'nacionality' => 'required',
'adress' => 'required',
'phone' => 'required',
'living_method' => 'required',
'responsable' => 'required',
]);
//Create Patient
$patient = new Patient;
$patient->name = $request->name;
$patient->save();
$patient->scale()->create(['patient_id' => $patient->id, 'sd_abc_1_old' => $request->sd_abc_1_old]);
);
return redirect('/patients')->with('success', 'Paciente creado.');
}
If the model uses a hasOne() relation like you said then the code show work, with a bit of modification of course.
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required',
'birth' => 'required',
'nacionality' => 'required',
'adress' => 'required',
'phone' => 'required',
'living_method' => 'required',
'responsable' => 'required',
]);
//Create Patient
$patient = Patient::create([
'name' => $request->input('name'),
]);
$scale = Scale::create([
'patient_id' => $patient->id,
'sd_abc_1_old' => $request->sd_abc_1_old,
]);
return redirect('/patients')->with('success', 'Paciente creado.');
}
First create and store base model and referencing it save related model
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required',
'birth' => 'required',
'nacionality' => 'required',
'adress' => 'required',
'phone' => 'required',
'living_method' => 'required',
'responsable' => 'required',
]);
//Create Patient
$patient = new Patient;
$patient->name = $request->input('name');
$patient->save();
$scales = new Scale;
$scales->sd_abc_1_old = $request->input('sd_abc_1_old');
$patient->scales()->save($scales);
return redirect('/patients')->with('success', 'Paciente creado.');
}
I don't know how to get the query string array to validate in Laravel Validation.
here is my code
public function addUserByAdmin(Request $request){
$this->validate($request,[
'name' => 'required',
'fullname' => 'required',
'password' => 'required',
'position' => 'required',
'phone' => 'required'
]);
$uesrAction = new UserAction($this->repository);
$user = $uesrAction->addUser($request->input('phone'),$request->input('password'),$request->input('name'),$request->input('fullname'),$request->input('position'));
if($user){
return response()->json(['success' => 'success','user'=>$user]);
}
return response()->json(['success' => 'fail']);
}
but validation doesn't work.Then I tried Validation::make() method and also didn't work.
My URL be like
http://localhost:8000/admin/users/add-user?phone=abcdefg&password=erer&name=ere&fullname=rere&position=343
If you are using laravel 5.5, change your code as follows:
public function addUserByAdmin(Request $request){
$validatedData = $request->validate([
'name' => 'required',
'fullname' => 'required',
'password' => 'required',
'position' => 'required',
'phone' => 'required'
]);
// ...
}
One of the features that was added in this update is that other way to use the validate() method:
More info:
https://laravel.com/docs/5.5/validation#quick-writing-the-validation-logic
https://laravel.com/docs/5.5/releases
public function register(Request $request){
$validator = \Validator::make($request->all(), [
'first_name' => 'required|min:3',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
'password_confirmation' => 'required',
'address' => 'required',
'phone_number' => 'required',
]);
if($request->user_type == 1 ){
$validator = \Validator::make($request->all(), [
'dob' => 'required'
]);
}
if($request->user_type == 2 ){
$validator = \Validator::make($request->all(), [
'doctor_practice' => 'required'
]);
}
// then, if it fails, return the error messages in JSON format
if ($validator->fails()) {
return response()->json($validator->messages(), 401);
}
How can I set the validator based on user type? I did try does not work fine.
Add rules array by users type and put rules at last
$rules = [
'first_name' => 'required|min:3',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
'password_confirmation' => 'required',
'address' => 'required',
'phone_number' => 'required',
];
if($request->user_type == 1 ){
$rules['dob'] = 'required';
}else if($request->user_type == 2 ){
$rules['doctor_practice'] = 'required';
}
$validator = \Validator::make($request->all(), $rules);
if ($validator->fails()) {
return response()->json($validator->messages(), 401);
}
It is better and more correctly to use embedded mechanism of validation.
$rules = [
'first_name' => 'required|min:3',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
'password_confirmation' => 'required',
'address' => 'required',
'phone_number' => 'required',
'dob' => 'required_if:user_type,1',
'doctor_practice' => 'required_if:user_type,2',
];
$validator = \Validator::make($request->all(), $rules);
if ($validator->fails()) {
return response()->json($validator->messages(), 401);
}
It is described in Laravel documentation
required_if:anotherfield,value,...
The field under validation must be present and not empty if the anotherfield field is equal to any value.
I am validating and storing image using my Controller.
When I validate image form is not stored in database but when I comment validation it stores in database.
FrontController.php
public function StorePost(Request $request)
{
$formInput = $request->except('image');
$this->validate($request, [
'title' => 'required',
'name' => 'required',
'email' => 'required|email',
'contact' => 'required',
'model' => 'required',
'city' => 'required',
'description' => 'required',
'image' => 'image|mimes:png,jpg,jpeg|max:10000'
]);
//image upload
$image = $request->image;
if($image){
$imageName = $image->getClientOriginalName();
$image->move('images', $imageName);
$formInput['image'] = $imageName;
}
Post::create($formInput);
$posts = Post::all();
return view('ad.ad', compact('name', 'posts'));
}
When I create a field I use the following validation.
$this->validate($request, [
'client_name' => 'required',
'contact_name' => 'required',
'email' => 'required|unique:clients|email',
]);
Now when I try to edit this with PUT. I try the following
$this->validate($request, [
'client_name' => 'required',
'contact_name' => 'required',
'email' => 'required|unique:clients|email',
]);
$client = \App\Client::find($id);
$client->client_name = $request->client_name;
$client->contact_name = $request->contact_name;
$client->email = $request->email;
$client->slug = str_slug($request->client_name, '_');
$client->save();
The problem - if I remove the Unique attribute it can be set as the same email address as another record. If I keep it in the record will not save.
Is there a work around?
Syntax for unique validator in Laravel (here)
unique:table,column,except,idColumn
So, in your code you need to mention the id which it need to except while applying unique.
$this->validate($request, [
'client_name' => 'required',
'contact_name' => 'required',
'email' => 'required|unique:clients,client_email,clientid,'.$id.'|email',
]);
$client = \App\Client::find($id);
$client->client_name = $request->client_name;
$client->contact_name = $request->contact_name;
$client->email = $request->email;
$client->slug = str_slug($request->client_name, '_');
$client->save();
Can also refer some discussion in laracast (here)
I hope it will help you.