在laravel中的db中验证并提交表单

时间:2017-11-24 14:54:46

标签: php laravel

我想在用户填写表单时将数据提交到db。但它表现得很奇怪。

这是我的控制器功能:

      namespace App\Http\Controllers\PrivatePages;
      use App\Http\Controllers\Controller;
      use App\Http\Requests\Request\CreateProductRequest;
      /*use App\Productuse Illuminate\Support\ServiceProvider;*/
      use Illuminate\Support\Facades\View;
      use Illuminate\Support\Facades\DB;
      use App\Http\FormRequests;


    public function UpdateAdminProfile(CreateProductRequest $request){
    Product::StoreProductDetails($request);
  }

这是我的模型函数,它定义了函数:

   namespace App;

   use App\Http\Requests\CreateProductRequest;
   use Illuminate\Database\Eloquent\Model;
   use App\Http\Controllers\PrivatePages;



  use  Illuminate\Http\Request;

public static function StoreProductDetails(CreateProductRequest $request)
{

        $saveproduct = new Product();
        $saveproduct->name = $request->get('name');
    $saveproduct->description = $request->get('description');
    $saveproduct->save();

}

这是我的请求文件:

<?php
     namespace App\Http\Requests\Request;
    use Illuminate\Foundation\Http\FormRequest;
    class CreateProductRequest extends FormRequest
 {
 /**
  * Determine if the user is authorized to make this request.
 *
 * @return bool
 */
public function authorize()
{
    return true;
}

/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{

    return [

        'name' => 'required',
        'description' => 'required',
        'contactinfo'=> 'required',

    ];

}



   public function messages()
   {
      return[
        'name.required' => 'Enter Name',
        'description.required' => 'Enter description',
        'contact.required' => 'Enter contact info',

    ];
    }
  }

我的表格

   @if (Session::has('success'))
   <div class="alert alert-success" role="alert" style="font-size: 18px;">
    <strong>Success: </strong>

    {{ Session::get('success') }}
 </div>
  @endif

       <form id="" method="post" class="form-horizontal" action="{{ 
      route('updateadminprofile')}}"enctype="multipart/form-data" >



      <div class="form-group">
      <label class="col-sm-4 control-label" for="userName"> Name</label>
      <div class="col-sm-5">
     <input type="text" class="form-control" id="name" name="name" 
      placeholder="name}"  value="name" />
     </div>
    </div>
   <div class="form-group">
   <label class="col-sm-4 control-label" for="userName"> Description</label>
   <div class="col-sm-5">
    <input type="text" class="form-control" id="description" 
   name="description" placeholder="Description}"  value="description" />
  </div>
  </div>
     <button  style="margin-left: 30%" type="submit" class="btn btn-primary" 
       name="signup" value="sumbmit" >Save</button>

没有给出错误

 jsut refreshes the page and no record is saving in db

为什么它会抛出错误,因为每件事看起来都很好?现在它只是刷新页面。甚至没有去表格

的行动中指定的路线

2 个答案:

答案 0 :(得分:3)

通过扩展 FormRequest 而非请求

CreateProductRequest 更改为以下内容
use Illuminate\Foundation\Http\FormRequest;

class CreateProductRequest extends FormRequest
{
public function rules()
{
    return [
        'name' => 'required',
        'description' => 'required',
        'contactinfo'=> 'required',
    ];
}

public function messages()
{
    return [
        'name.required' => 'Enter Name',
        'description.required' => 'Enter description',
        'contact.required' => 'Enter contact info',
    ];
}
}

希望这有效:)

答案 1 :(得分:1)

请试试这个:

public static function StoreProductDetails(CreateProductRequest $request)
{
    $saveproduct = new Product();
    $saveproduct->name = $request->get('name');
    $saveproduct->description = $request->get('description');
    $saveproduct->save();
}

并更改您的CreateProductRequest.php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class CreateProductRequest extends FormRequest
{
    public function rules()
    {
        return [
            'name' => 'required',
            'description' => 'required',
            'contactinfo'=> 'required',
        ];
    }

    public function messages()
    {
        return [
            'name.required' => 'Enter Name',
            'description.required' => 'Enter description',
            'contact.required' => 'Enter contact info',
        ];
    }
}

已编辑:您可以查看视图中的错误

@if ($errors->any())
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif