I am submitting a form thorugh ajax, form values are saving in the database and errors are showing without page reload, which is a good thing. As I am a beginner so i cannot make sense of this error. This error is not disturbing or impacting the flow of the application in any way. Thanks.
**Here's the error's image: **
**Here's the code of the controller: **
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Products;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Validator;
class ProductController extends Controller
{
//Search Products
public function search(Request $request)
{
$products = Products::where('name','like', '%'.$request->search.'%')
->orWhere('id','like', '%'.$request->search.'%')->get();
$output = "";
foreach($products as $products)
{
$output.=
'<tr >
<td>'.$products->name.'</td>
<td>'.$products->s_description.'</td>
<td>'.$products->l_description.'</td>
<td ><img src='.asset('images')."/".$products->image_src.'></td>
<td>'.$products->category.'</td>
<td>'.$products->quantity.'</td>
<td>'.$products->price.'</td>
<td>'.'
<form action='.route('delete_product', $products->id).' method="POST" id="deleteBtn">
z
<input type="hidden" name="_method" value="delete">
<button type="submit">'.'Delete</button>
</form>
'.'
</td>
</tr>
';
}
return response($output);
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function viewProducts()
{
$p_details = Products::all();
return view('admin.products.view_products', compact('p_details'));
}
public function productVerify(Request $request)
{
$val = $request->validate
(
[
'name' => 'required',
's_description' => 'required',
'l_description' => 'required',
'image_src' => 'required|mimes:jpg,png,jpeg',
'category' => 'required',
'quantity' => 'required|integer|not_in:0|regex:^[1-9][0-9] ^',
'price' => 'required|integer|not_in:0|regex:^[1-9][0-9] ^',
],
[
'required' => 'The :attribute field is required',
'mimes' => 'Image should be a JPG, JPEG, or PNG',
'integer' => 'The :attribute field should be an integer.',
]
);
if ($val)
{
return response()->json(['errors'=>($val)->errors()->all()]);
}
else
{
// return redirect()->to('view_products')->with('success','Product added successfully');
return response()->json(['errors'=>'Product added successfully, head to view products to inspect it. Thanks!']);
}
}
//Uploading Images
public function validImg(Request $request)
{
if ($request->hasFile('image_src'))
{
$filename = $request->file('image_src');
$filename->getClientOriginalName();
$filename = time().$filename->getClientOriginalName();
$destinationPath = base_path("/public/images");
$request->file('image_src')->move($destinationPath, $filename);
$data['image_src'] = $filename;
}
return $data['image_src'];
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('admin.products.add_products');
}
//Creating and Adding Products
public function createProduct(Request $request)
{
//Product Validation
$validation = $this->productVerify($request);
$data = $request->all();
$image_src = $this->validImg($request);
Products::create
([
'name' => $data['name'],
's_description' => $data['s_description'],
'l_description' => $data['l_description'],
'category' => $data['category'],
'quantity' => $data['quantity'],
'price' => $data['price'],
'image_src' => $image_src
]);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$product_edit = Products::findOrFail($id);
return view('admin.products.edit_products', compact('product_edit'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//Product Validation
$validatedData = $this->productValidation($request);
$this->validImg($request);
Products::whereId($id)->update($validatedData);
return redirect('view_products')->with('success', 'Product details successfully updated');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$products = Products::findOrFail($id);
$destinationPath = base_path("/public/images").'/'.$products->image_src;
if(File::exists($destinationPath))
{
File::delete($destinationPath); //for deleting only file try this
$products->delete(); //for deleting record and file try both
}
// $products->delete();
return redirect('view_products')->with('error', 'Product successfully deleted');
}
}
CodePudding user response:
The validate()
method on the $request
object returns an array (see API docs) containing the data that passed validation. If validation fails, a JSON response (which includes validation errors) is generated and returned to the client that made the request.
The code statement you're highlighting in your question is informing you that the variable $val
is an array
, however, you're attempting to access some data within $val
using the object operator (->
) as though it were an object.
If that code statement were to actually be executed, you'd see an exception message because of this (there is no errors()
method on the $val
array).
CodePudding user response:
try this
return response()->json(['errors'=>$val->errors()->all()]);