Laravel request validation image required in create but not required in update - php

ProductsRequest.php code:
public function rules()
{
return [
'name' => 'required
|min:'.trans('validation_standards.names.min').'
|max:'.trans('validation_standards.names.max').'
|unique:products,name,'.$this -> product_id,
'barcode' => 'size:'.trans('validation_standards.barcode.size').'
|unique:products,barcode,'.$this -> product_id,
'category_id' => 'required
|exists:categories,id',
'seasons_id' => 'required
|exists:seasons,id',
// REQUIRED IMAGE ONLY IN STORE
'image' => 'required
|image|mimes:'.trans('validation_standards.images.extensions').'
|max:'.trans('validation_standards.images.file_size'),
'description' => 'nullable
|min:'.trans('validation_standards.descriptions.min').'
|max:'.trans('validation_standards.descriptions.max'),
];
}
These rules apply for both store and update methods.
Problem is:
I want the image to be required only on store and not required in update, since user can just update the product basic info without choosing a new image for the product every time he update the product.
What I have tried:
I have tried to create two different ProductsRequest one for store and other for update but I know that this achievement is not the best achievement because my code must be DRY.

Use required_without rules
If primary key and element with name is id exist in your array
'image' => 'required_without:id`
If primary key and element with name is product_id exist in your array
'image' => 'required_without:product_id`
You can get more detail from laravel validation

You can do this in your ProductsRequest file;
public function rules()
{
if(request()->isMethod('put')) // could be patch as well
{
// Update rules here - Don't require image here
return [
'name' => 'required
|min:'.trans('validation_standards.names.min').'
|max:'.trans('validation_standards.names.max').'
|unique:products,name,'.$this->product_id,
'barcode' => 'size:'.trans('validation_standards.barcode.size').'
|unique:products,barcode,'.$this->product_id,
'category_id' => 'required|exists:categories,id',
'seasons_id' => 'required|exists:seasons,id',
// REQUIRED IMAGE ONLY IN STORE
'image' => 'required|image|mimes:'.
trans('validation_standards.images.extensions').'
|max:'.trans('validation_standards.images.file_size'),
'description' => 'nullable
|min:'.trans('validation_standards.descriptions.min').'
|max:'.trans('validation_standards.descriptions.max'),
];
}else{
// store rules here - require image here
return [
'name' => 'required
|min:'.trans('validation_standards.names.min').'
|max:'.trans('validation_standards.names.max').'
|unique:products,name,'.$this->product_id,
'barcode' => 'size:'.trans('validation_standards.barcode.size').'
|unique:products,barcode,'.$this->product_id,
'category_id' => 'required|exists:categories,id',
'seasons_id' => 'required|exists:seasons,id',
// REQUIRED IMAGE ONLY IN STORE
'image' => 'image|mimes:'.
trans('validation_standards.images.extensions').'
|max:'.trans('validation_standards.images.file_size'),
'description' => 'nullable
|min:'.trans('validation_standards.descriptions.min').'
|max:'.trans('validation_standards.descriptions.max'),
];
}
}
}

public function rules()
{
$image = request()->isMethod('put') ? 'nullable|mimes:jpeg,jpg,png,gif,svg|max:8000' : 'required|mimes:jpeg,jpg,png,gif,svg|max:8000';
return [
'image' => $image,
];
}

If your route is something like this one (which should be)
Route::post('products/{id}/update', 'ProductController#updateProduct')->name('products.update');
And you will call this route as
route('products.update', $product->id);
$product->id will be available in your form request and you can validate your form request for both create and update like this.
public function rules() {
return [
// All other rules
'image' => $this->id == null ? 'required|image|mimes:'.
trans('validation_standards.images.extensions').'
|max:'.trans('validation_standards.images.file_size') :
'image|mimes:'.trans('validation_standards.images.extensions').'
|max:'.trans('validation_standards.images.file_size')
];
}

Just this few lines can solve your problems...
You have to check there image have or not, like this.
Rules in a private or protected function
private function validateRequest($request)
{
//This is for Update without required image, this will check that In DB image have or not
$product = Product::find($request->product_id);
$rules = [];
if ($product) :
if ($product->product_image == null):
$rules['product_image'] = 'required|image|max:1999';
endif;
//This is for regular validation
else :
$rules = [
'category_id' => 'required|integer|not_in:-- Select Category --',
'product_image' => 'required|image|max:1999',
];
endif;
return $rules;
}

Related

Save multi-level array using Laravel Model

I have problem to save this data using Laravel-7 model
this is my data
$supplier = [
'name' => 'Supplier 1',
'pic' => [
[
'name' => 'PIC 1',
'phone_number' => [
['number' => '111111'],
['number' => '123456']
]
],
[
'name' => 'PIC 2',
'phone_number' => [
['number' => '222222']
]
]
]
];
And this is my models
Supplier.php
// Supplier.php
public function supplier_pic()
{
return $this->hasMany('SupplierPIC');
}
and the other models
// SupplierPIC.php
public function supplier()
{
return $this->belongsTo('Supplier');
}
public function pic_phone_number()
{
return $this->hasMany('SupplierPICPhoneNumber');
}
// SupplierPICPhoneNumber.php
public function supplier_pic()
{
return $this->belongsTo('SupplierPIC');
}
How to save those data on controller ?
Thank you
You just need to break it down into it's constituent objects.
In your case, it is one Supplier object with two SupplierPIC objects, each of which has a SupplierPICPhoneNumber
Create Supplier
$supplier = Supplier::firstOrCreate([
'name' => 'Supplier 1'
]);
Create Supplier PIC(s)
collect($data['pics'])->each(function ($pic) use ($supplier) {
// Create the PIC
$x = SupplierPIC::create([
'name' => $pic['name']
]);
// Attach it to the supplier
$supplier->supplier_pic()->save($x);
// Attach phone numbers
collect($pic['phone_number'])->each(function ($number) use ($x) {
// Create the PIC Phone number
$y = SupplierPICPhoneNumber::create([
'number' => $pic['number']
]);
// Attach the number to the PIC
$x->pic_phone_number()->save($y);
});
});
Suggestions
The naming of your relationships doesn't follow best practice which is a little confusing. Try naming things that are use a hasMany type relationship with a plural (i.e. pic_phone_numbers rather than pic_phone_number)
Do you need an entire model for SupplierPICPhoneNumber? A json column may be better suited.

Update only the value of one field in controller in Laravel 6

My project requires that the admin has to approve the item uploaded by the user in order for this item to be seen in the website as in sort of validation or some kind of keeping everything under control.
so in the Item table, I have a field called (status) and has a default value = (denied).
$table->enum('status',['available','denied'])->default('denied');
The admin sees all items and I want a button next to each item called approve if the admin clicks it the status change from denied to approved, how can I create a function in the controller that changes only the status field? one besides the default edit and update function that is already working in my controller.
public function edit($itemid)
{
$where = array('itemid' => $itemid);
$data['item_info'] = Item::where($where)->first();
return view('items.edititem', $data);
}
public function update(Request $request, $itemid)
{
$request->validate([
'name' => 'required',
'description' => 'required',
'state' => 'required',
'price' => 'required',
'charityfee' => 'required',
'status' => 'required',
'category' => 'required',
]);
$update = [
'name' => $request->name, 'description' => $request->description,
'state' => $request->state, 'price' => $request->price,
'charityfee' => $request->charityfee, 'status' => $request->status,
'category' => $request->category, 'itemphoto' => $request->itemphoto
];
Item::where('itemid', $itemid)->update($update);
return Redirect::to('profile')
->with('success', 'Great! item updated successfully');
}
Although I tried this following code, an error appeared that the function I have called is not defined:(
public function editstatus($itemid)
{
Item::where('itemid', $itemid)->update(array('itemid' => 'available'));
}
function in controller
<td>approve</td>
the code in the view
Your problem is on editstatus function.
you want to update status => 'available' but you code update itemid => 'available'. that why it error.
you code should change to below:
public function editstatus($itemid)
{
Item::where('itemid', $itemid)->update(array('status' => 'available'));
}
I'm pretty sure your $where variable is wrong, You want to compare a variable with a field just get rid of the '=>'. You are basically doing an assigment operation and not a comparison.
You can have it this way
$data['item_info'] = Item::where($itemid, 'item_id')->first();

Custom validation using laravel 5.8

Good day,
I am trying to update brand data using Laravel 5.8 and I made a custom validation function to validate the name of the brand but my problem is when I attempt an update, the validation fails and I get a message saying Opps name Is Exist Before
I need to update this validation function to link the brand id with the brand name to perform the update without showing the validation error.
Thanks in advance.
Here is my code:
public function update(Request $request, $id)
{
//prepare data for validation
request()->validate([
'name' => [
'required',
'min:2', // validate english name is exist before
function ($attribute, $value, $fail) {
$englishname=Brand::where(['name'=>$value,'deleted'=>1 ])->first();
if(false !=$englishname) {
$fail('Opps '.$attribute.' Is Exist Before.');
}
},
],
'keywords' => 'required|min:2',
'ar_name' => [
'required',
'min:2',// validate english name is exist before
function ($attribute, $value, $fail) {
$arname=Brand::where(['ar_name'=>$value,'deleted'=>1])->first();
if(false !=$arname) {
$fail('Opps '.$attribute.' Is Exist Before.');
}
},
],
'ar_keywords' => 'nullable',
'status' => 'required|integer',
],[],[
"name"=>"Brand Name",
'keywords' => 'Brand KeyWords',
'ar_name' => 'اسم الماركة',
'ar_keywords' => 'الكلمات الدليلية',
]);
// start pass data to model
$branddata=array(
'name' =>$request->name,
'keywords' =>$request->keywords,
'ar_name' =>$request->ar_name,
'ar_keywords' =>$request->ar_keywords,
'last_updated_by'=>auth()->user()->id,
'status' =>$request->status,
);
//start update data
$updateddata=Brand::where(['id'=>$id,'deleted'=>1])->update($branddata);
if (false !==Brand::create($updateddata))
{
return redirect(route("brand.edit"))->with("messageSuccess","Brand Updated Successfully");
}else{
return redirect(route("brand.edit"))->with("messageSuccess","Brand Updated Successfully");
}
}
You can use the unique validation rule from Laravel with extended attributes passed for the validation checks.
If you want to validate that the name attribute must be unique (but allows for the same model instance), you can do something like follow:
'name' => "required|string|email|unique:<table name for the Brand::class>,name,{$id}"
This validation rule will check for uniqueness of the provided name for every rows except the one with $id as its primary key.
If your $id variable is not a primary key, you can specify the column name for the variable as follows:
'name' => "required|string|email|unique:<table name for the Brand::class>,name,{$id},<column name of the id>"

Unable to obtain instance of HTTP request in Laravel 5.8

I am having trouble retrieving form values using the Illuminate\Http\Request class.
I have an endpoint that creates a product and another that updates it set up like this
Route::post('products', 'ProductController#store');
Route::put('products/{product}', 'ProductController#update');
The store and update methods in the ProductController are
public function store(Request $request)
{
// validate inputs
$validator = Validator::make($request->all(), [
'name' => 'required',
'category' => 'required',
'status' => 'required',
'price' => 'required',
'image' => 'required|image|mimes:jpeg',
'interest' => 'required'
]);
// return 401 response if validation fails
if ($validator->fails()) {
return response()->json([$validator->errors()], 401);
}
// create & store the product
if ($product = Product::create([
'name' => $request->name,
'category' => $request->category,
'status' => $request->status,
'price' => $request->price,
'interest' => $request->interest,
])) {
// store the product image
$file = $request->file('image');
$destinationPath = "public/images/products";
$filename = 'pramopro_' . $product->name . '_' . $product->id . '.' . $file->extension();
Storage::putFileAs($destinationPath, $file, $filename);
ProductImage::create([
'product_id' => $product->id,
'name' => $filename
]);
}
// return new product
return new ProductResource($product);
}
public function update(Request $request, Product $product)
{
// dd($request);
// validate inputs
$validator = Validator::make($request->all(), [
'name' => 'required',
'category' => 'required',
'status' => 'required',
'price' => 'required',
'image' => 'required|image|mimes:jpeg',
'interest' => 'required'
]);
// return 401 response if validation fails
if ($validator->fails()) {
return response()->json([$validator->errors()], 401);
}
// update this product
if ($product) {
$product->update($request->all());
return new ProductResource($product);
} else {
return response()->json(["error" => "Not found"], 404);
}
}
Now when I test the endpoints in postman, I can successfully create a new product record. But if I try to update a product, I get a 401 unauthorized error even though all the required fields are filled. dd($request)returns null but dd($product) returns the product as expected.
Maybe I have been looking at it so hard so I have missed something. What am I doing wrong?
This is the reasons I think why the $request return null
The store and update request is same name. Two input name or select name or textarea name can't be the same name in the same web page. If its same it will always get the first one that is the reason why it is returning null because the first one is empty.
The name you're calling is incorrect
Hope it helps!

Unique key violation upon edit

I am trying to update a blog post but I am getting unique key error from database part then I went without using model and directly accessing ORM but then again no success.
This is my routes spesific to edit
Route::get('/getedit/{slug}', array('as' => 'getedit', 'uses' => 'AdminController#getEdit'))->before('auth');
Route::post('/postedit', array('as' => 'postedit', 'uses' => 'AdminController#postEdit'))->before('auth');
Controller
public function getEdit($slug)
{
$article = Post::where('slug', '=' , $slug)
->firstOrFail();
return View::make('admin.edit', array(
'title' => $article->title,
'mainarticle' => $article->article,
'slug' => $article->slug,
'category' => $article->category
));
}
// Updates articles to database
public function postEdit()
{
$rules = [
'title' => 'required',
'article' => 'required',
'slug' => 'required|unique:posts,slug,9',
'category' => 'required'
];
$input = Input::all();
$validator = Validator::make($input, $rules);
if ($validator->fails()) {
return Redirect::route('getedit')
->withErrors($validator);
// withInput not defined
}
else
{
$slug = $input['slug'];
/*$affectedRows = Post::where('slug', '=', $slug)->update([
'title' => $input['title'],
'article' => $input['article'],
'slug' => $input['slug'],
'category' => $input['category']
]);*/
/*$affectedRows = Post::where('slug', '=', $slug)->firstOrFail();
$affectedRows->title = $input['title'];
$affectedRows->article = $input['article'];
$affectedRows->slug = $input['slug'];
$affectedRows->category = $input['category'];
$affectedRows->save();*/
$post = DB::table('posts')->where('slug', '=', $slug)->update([
'title' => $input['title'],
'article' => $input['article'],
'slug' => $input['slug'],
'category' => $input['category']
]);
if ($post) {
return Redirect::route('dashboard')
->with('flash_message','Article Successfully Inserted');
}
else
{
return Redirect::route('dashboard')
->with('flash_message','Error updating data');
}
}
}
My model is just creating object of database (I am accidentally following fat controller and thin model approach as I am just trying the framework).
I have tried using Post::find(1)->update($data); method but that is returning unique violation and my current approach is just executing else statement which is triggered upon update failure.
Note: I am new to Laravel and trying this for the first time.
When you update a post, you'd rather send a POST (or better PATCH/PUT- http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html) request to given resource.
That said, you would include edited row key in the url, and change your method to something like this:
// route
Route::post('/postedit/{id}', array('as' => 'postedit', 'uses' => 'AdminController#postEdit'))
->before('auth');
// controller
public function postEdit($id)
{
// if no posts with $id found, throws exception - catch it and eg. show 404
$post = Post::findOrFail($id);
$rules = [
'title' => 'required',
'article' => 'required',
'slug' => 'required|unique:posts,slug,'.$id, // to ignore this row in unique check
'category' => 'required'
];
// validate
$post->fill($input)->save(); // fill() in order to use mass-assignement check
// alternatively you can just update:
// $post->update($input);
// but then make sure $input has only elements corresponding to the table columns
Additionally, read about route grouping, so you don't need to add before('auth') to those routes separately.
You should check your database table indexes. You should make sure that only slug has unique index.
I see that you are checking unique for slug but you hardcoded 9 in the rule:
'slug' => 'required|unique:posts,slug,9',
It should be:
'slug' => 'required|unique:posts,slug,'.$id,
where $id id of post you try to edit.
You should include such id in your form as hidden element and not search records with slug that you have because it seems you can edit your slug and you may edit the wrong record or edit nothing.

Categories