【问题标题】:I am unable to send data to my database , getting no error我无法将数据发送到我的数据库,没有错误
【发布时间】:2021-12-30 06:53:27
【问题描述】:
 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...');  
  }
 
  }

无法使用此代码解决问题 这是我的刀片文件,每当我提交表单时,它没有在数据库中插入数据,这是一个问题,我检查了包括模型、迁移等在内的所有内容,但没有收到任何错误但仍然没有提交数据

<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>

我希望我的代码在我的数据库中插入数据,但它正在返回并且没有提供错误 或成功,如果我检查我的数据库没有数据请解决问题

【问题讨论】:

    标签: laravel-blade laravel-controller


    【解决方案1】:

    你可以这样使用它:

    $request->validate($rules);
    

    $request->validate([
        'productName' => 'required|max:255'
    ]);
    

    然后它返回错误。

    $test = $productName->validate([
               'productName' => 'required|max:255'
            ]);
    

    你需要检查是否没有错误,然后你可以做任何你想做的事情。

    在您的情况下,您需要这样做:

    $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...'); 
    }
    

    【讨论】:

    • 谢谢你的回答...给了我解决方案?✊
    • @RitikRai 请关闭此问题,点击接受作为答案选项
    猜你喜欢
    • 1970-01-01
    • 2020-07-22
    • 2019-12-12
    • 1970-01-01
    • 2017-11-21
    • 2021-08-15
    • 1970-01-01
    • 2016-09-08
    • 2018-09-25
    相关资源
    最近更新 更多