namespace App\Http\Controllers; use App\Models\Product; use Illuminate\Http\Request; use Illuminate\Support\Facades\Validator; class ProductController extends Controller { public function store(Request $request) { $this->validate($request,[ 'productName' =>'required|max:255', 'quantity' =>'required', 'weight' =>'required', 'boxes' =>'required', 'MRP' =>'required', 'costprice' =>'required', 'image' =>'required|image|mimes:png,jpeg,jpg,gif,svg|max:2048', 'productDescription' =>'required', ]); $imageName = time().'.'.$request->image->extension(); Product::create([ 'productName' => $request->productName, 'quantity' => $request->quantity, 'weight' => $request->weight, 'boxes' => $request->boxes, 'MRP' => $request->MRP, 'costprice' =>$request->costprice, 'productDescription' =>$request->productDescription, 'image' =>$request->image->move(public_path('images'),$imageName), 'seller_id' => $request->user('seller')->id, 'category_id' => $request->category ]); return back()->with('success','Product stocked Successfully...'); } }
can't get the problem solved with this code here is my blade file whenever i am submitting form it is not inserting data in database which is a problem and i have checked everthing including model , migration etc. but not getting any error but still data is not getting submit
<div class='w-75 justify-end'> <form action="{{ route('products') }}" class='w-100 bg-white p-6 rounded-lg mt-1' method='post'> @csrf @if(session()->has('success')) <div class='alert alert-success'> <ul> <li>{{ session()->get('success')}}</li> </ul> </div> @endif <fieldset class='w-100 inline-flex bg-grey scheduler-border'> <legend class='text-secondary font-bold border-bottom w-50 text-left ml-4 px-10 py-1'>Add Product </legend> <div class='flex w-100'> <div class='control-group flex flex-col ml-3 mb-4 w-25 p-3'> <label for="image" class='sr-only'>Image</label> <input type="file" name='image' id='image' class='form-control bg-gray-100 border-2 w-75 h-25 p-2 rounded-lg mb-4'> <label for="productName" class='sr-only'>Product Name</label> <input type="text" name='productName' id='productName' placeholder= 'product Name' class='bg-gray-100 border-2 w-75 p-2 h-10 rounded-lg mb-8 @error('productName') ? border border-danger : '' @enderror' value='{{ old('productName') }}'> <label for="category" class='sr-only'>Category</label> <select type="dropdown" name='category' id='category' class='bg-gray-100 text-secondary border-2 w-75 h-10 rounded-lg mb-2'> <option selected>select category</option> @foreach($categoryname as $data) <option value="{{ $data->id }}">{{ $data->categoryName }}</option> @endforeach </select> <div class='form-check w-100 ml-0 m-2 p-2'> <i>Status:</i> <input class='form-check-input mt-2 ml-3' type="checkbox" name='flexCheckChecked' id='flexCheckChecked' class='bg-gray-100 border-2 w-25 p-2 rounded-lg mb-4' checked> <label class='form-check-label ml-8' for="flexCheckChecked">active</label> </div> </div> <div class='control-group mb-4 p-3 w-50'> <label for="product description" class='sr-only'>Product description</label> <textarea name='productDescription' id='productDescription' placeholder='Add product description' class='bg-gray-100 border-2 w-75 h-25 p-2 rounded-lg mb-2 @error('productDescription') ? border border-danger : '' @enderror' value='{{ old('productDescription') }}' cols="40" rows="10"></textarea> <div class='flex mt-2'> <label for="Unit price" class='sr-only'>Unit price</label> <input type="number" name='costprice' min=1 id='costprice' placeholder='Unit price' class='bg-gray-100 border-2 text-sm w-25 p-2 rounded-lg mb-4 @error('costprice') ? border border-danger : '' @enderror' value='{{ old('costprice') }}'> <small class='text-secondary p-2 font-italic'>(Rs.)</small> <label for="weight" class='sr-only'>weight</label> <input type="number" name='weight' min=1 id='weight' placeholder= 'Weight' class='bg-gray-100 border-2 w-25 text-sm p-2 rounded-lg mb-4 ml-4 @error('weight') ? border border-danger : '' @enderror' value='{{ old('weight') }}'> <small class='text-secondary p-2 font-italic'>(g)</small> </div> <div class='flex'> <label for="quantity" class='sr-only'>Quantity</label> <input type="number" name='quantity' min=1 id='quantity' placeholder= 'Quantity' class='bg-gray-100 border-2 text-sm w-25 p-2 rounded-lg mb-4 @error('quantity') ? border border-danger : '' @enderror' value='{{ old('quantity') }}'> <small class='text-secondary p-2 font-italic'>(per box)</small> <label for="boxes" class='sr-only'>Boxes</label> <input type="number" name='boxes' min=1 id='boxes' placeholder= 'Boxes' class='bg-gray-100 border-2 w-25 p-2 rounded-lg mb-4 @error('boxes') ? border border-danger : '' @enderror' value='{{ old('boxes') }}'> <small class='text-secondary p-2 text-sm font-italic'>(cartoon)</small> </div> <div class='mb-4 flex justify-start'> <input type="number" name='MRP' min=1 id='MRP' placeholder= 'MRP' class='bg-gray-100 border-2 w-25 text-sm p-2 rounded-lg mb-4 @error('MRP') ? border border-danger : '' @enderror' value='{{ old('MRP') }}'> <small class='text-secondary p-2 font-italic mr-4'>(Rs.)</small> <button type="submit" class='w-25 h-10 bg-blue-500 text-white text-center font- medium py-1 rounded-lg'><b>+</b> Add Product</button> </div> </div> </div> </fieldset> </form> <hr class='bg-grey-500'> </div>
I want my code to insert data in my database but it is getting back and not providing error or success if i check my database there is no data please resolve issue
Asked
Active
Viewed 188 times
0

Ritik Rai
- 11
- 3
1 Answers
0
You can use it like this:
$request->validate($rules);
or
$request->validate([
'productName' => 'required|max:255'
]);
Then it returns the errors.
$test = $productName->validate([
'productName' => 'required|max:255'
]);
and you need to check if there are no errors and then you can do what ever you want.
In your case you need to do it like this:
$validator = Validator::make($request->all(), [
'productName' => 'required|max:255'
]);
if ($validator->fails()) {
return back()->with('failed','There is error');
} else {
return back()->with('success','Product stocked Successfully...');
}

Kamran Khalid
- 260
- 1
- 7
-
Thanks ur answer...got me the solution✊ – Ritik Rai Dec 30 '21 at 13:44
-
@RitikRai please close this question, Click on Accept as answer option – Kamran Khalid Dec 30 '21 at 13:58