Php Laravel 5控制器不返回验证程序错误

Php Laravel 5控制器不返回验证程序错误,php,laravel-5,Php,Laravel 5,我在Laravel 5中有一个产品模型和产品控制器 但是验证器错误不会返回 我无法理解为什么会发生这样的事情 以下是已发布的表单字段: <form class="form-horizontal" action="/imalatci/products/addproduct" enctype="multipart/form-data" method="post"> {!! csrf_field() !!}

我在Laravel 5中有一个
产品
模型和
产品控制器

但是验证器错误不会返回

我无法理解为什么会发生这样的事情

以下是已发布的表单字段:

<form class="form-horizontal"  action="/imalatci/products/addproduct" enctype="multipart/form-data" method="post">
                                    {!! csrf_field() !!}
                                    <input type="hidden" name="imalatci_id" value="{{ Auth::user()->id }}"/>
                                    <div class="form-group">
                                        <label class="col-md-2 control-label">Ürün Başlığı</label>
                                        <div class="col-md-8">
                                            <input type="text" name="title" class="form-control" placeholder="Ürün başlığını giriniz.">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-md-2 control-label">Ürün Açıklaması</label>
                                        <div class="col-md-8">
                                            <textarea class="form-control"  rows="5" name="description" placeholder="Ürün hakkında detaylı bir açıklama yapınız." ></textarea>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-md-2 control-label">Ürünün Fiyatı</label>
                                        <div class="col-md-8">
                                            <input type="text" name="price" class="form-control" placeholder="Ürününü fiyatını  belirleyiniz (TL Olarak).">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-md-2 control-label">Ürünün Stok Durumu</label>
                                        <div class="col-md-8">
                                            <select class="form-control" name="avaliability">
                                                <option value="1">Stokta Var</option>
                                                <option value="0">Stokta Yok</option>
                                            </select>
                                        </div>
                                    </div>

                                    <div class="form-group">
                                        <label class="col-md-2 control-label">Ürün Kategorisi</label>
                                        <div class="col-md-8">
                                            <select class="form-control" name="subcategory" id="subcategory">
                                                @foreach(App\Category::all() as $category)
                                                    <optgroup label="{{ $category->name }}">
                                                        @foreach(App\Category::find($category->id)->subcategories as $subcategory)
                                                            <option value="{{$subcategory->id}}">{{$subcategory->name}}</option>
                                                        @endforeach
                                                    </optgroup>
                                                @endforeach
                                            </select>
                                        </div>
                                    </div>

                                    <div class="form-group">
                                        <label class="col-md-2 control-label">Ürün Ana Resmi</label>
                                        <div class="col-md-8">
                                            <input id="product_main_img" name="image" type="file" class="file-loading" accept="image/*">
                                        </div>
                                    </div>


                                    <!--
                                    <div class="form-group">
                                        <label class="col-md-2 control-label">Ürün Diğer Resimler</label>
                                        <div class="col-md-8">
                                            <input id="products_img" name="product_images[]" type="file" multiple=true class="file-loading" accept="image/*">
                                        </div>
                                    </div>
                                    -->


                                    <div id="kv-success-modal" class="modal fade">
                                        <div class="modal-dialog">
                                            <div class="modal-content">
                                                <div class="modal-header">
                                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                                    <h4 class="modal-title">Yükleme İşlemi Başarılı</h4>
                                                </div>
                                                <div id="kv-success-box" class="modal-body">
                                                </div>
                                            </div>
                                        </div>
                                    </div>

                                    <div class="form-group">
                                        <div class="col-md-offset-2 col-md-8">
                                            <button type="submit" class="btn btn-success btn-lg">Ürünü Ekle</button>
                                        </div>
                                    </div>
                                </form>
这是我们的控制器类:

namespace App\Http\Controllers;

use App\Category;
use App\Product;
use App\ProductImage;
use App\SubCategory;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
use Intervention\Image\Facades\Image;
use Validator;

class ProductsController extends Controller
{
    public function getIndex()
    {
        $products = Product::all();

        return view('pages.imalatci.products')
            ->with('products',$products);

    }

    public function postAddproduct()
    {
        $validator = Validator::make(Input::all(),Product::$rules);

       if(!$validator->fails()){

            $product = new Product();
            $subcategory = SubCategory::find(Input::get('subcategory'));
            $product->category_id = $subcategory->category_id;
            $product->subcategory_id = Input::get('subcategory');
            $product->imalatci_id = Input::get('imalatci_id');
            $product->title = Input::get('title');
            $product->description = Input::get('description');
            $product->price = Input::get('price');
            $image = Input::file('product_main_img');
            $filename =Input::get('imalatci_id').'_'.date('Y_m_d_H_i_s').'_'.$image->getClientOriginalName();
            Image::make($image->getRealPath())->resize(250,280)->save('images/products/'.$filename);
            $product->image = 'images/products/'.$filename;
            $product->save();


            flash()->success('Ürün başarıyla eklendi.Yönetici onayından sonra ürün yayınlanacaktır.');

            return Redirect::to('/imalatci/products');

        }

        flash()->important('Hata Oluştu');

        return Redirect::to('/imalatci/products')
            ->withErrors($validator)
            ->withInput();
    }


}

如何解决此问题?

您应该在控制器中定义
规则
数组。试着这样做:

namespace App\Http\Controllers;

use App\Category;
use App\Product;
use App\ProductImage;
use App\SubCategory;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
use Intervention\Image\Facades\Image;
use Validator;
use Input;

class ProductsController extends Controller
{
    protected $rules = array(
        'category_id' => 'required|integer',
        'subcategory' => 'required|integer',
        'imalatci_id' => 'required|integer',
        'title' => 'required|min:2',
        'description' => 'required|min:10',
        'price' =>'required|numeric',
        'avaliability' => 'required|integer',
        'image' => 'required|image|mimes:jpeg,jpg,bmp,png,gif',
        'verified' => 'required|integer',
        'published' => 'required|integer'
    );
    public function getIndex()
    {
        $products = Product::all();

        return view('pages.imalatci.products')
            ->with('products',$products);

    }

    public function postAddproduct()
    {
        $validator = Validator::make(Input::all(),$this->rules);

       if(!$validator->fails()){

            $product = new Product();
            $subcategory = SubCategory::find(Input::get('subcategory'));
            $product->category_id = $subcategory->category_id;
            $product->subcategory_id = Input::get('subcategory');
            $product->imalatci_id = Input::get('imalatci_id');
            $product->title = Input::get('title');
            $product->description = Input::get('description');
            $product->price = Input::get('price');
            $image = Input::file('product_main_img');
            $filename =Input::get('imalatci_id').'_'.date('Y_m_d_H_i_s').'_'.$image->getClientOriginalName();
            Image::make($image->getRealPath())->resize(250,280)->save('images/products/'.$filename);
            $product->image = 'images/products/'.$filename;
            $product->save();


            flash()->success('Ürün başarıyla eklendi.Yönetici onayından sonra ürün yayınlanacaktır.');

            return Redirect::to('/imalatci/products');

        }

        flash()->important('Hata Oluştu');

        return Redirect::to('/imalatci/products')
            ->withInput()
            ->withErrors($validator);
    }


}
您可以通过$rules属性使用来验证模型

第一个安装包

composer require "watson/validating"
在你的模型中使用特质

namespace App;

use Illuminate\Database\Eloquent\Model;
use Watson\Validating\ValidatingTrait;

class Product extends Model
{
    use ValidatingTrait;

    protected $table = "products";


    protected $fillable = ['category_id','subcategory_id','imalatci_id','title','description','price','avaliability','image','verified','published'];

    protected $rules = array(
        'category_id' => 'required|integer',
        'subcategory' => 'required|integer',
        'imalatci_id' => 'required|integer',
        'title' => 'required|min:2',
        'description' => 'required|min:10',
        'price' =>'required|numeric',
        'avaliability' => 'required|integer',
        'image' => 'required|image|mimes:jpeg,jpg,bmp,png,gif',
        'verified' => 'required|integer',
        'published' => 'required|integer'
    );
/*
    public static $messages = array(
        'required' => ':attribute alanı boş bırakılamaz.',
        'numeric'  => ':attribute alanına sadece sayı girilebilir.',
        'min' => ':attribute alanı en az :min karakter olmalıdır.',
        'integer' => ':attribute alanı sadece sayı olabilir.',
        'image' => 'Resim formatında yükleme yapılabilir.',
        'mimes' => 'Resim olarak sadece jpeg,jpg,png,gif formatlarından birisi yüklenebilir.'
    );
*/


    public function category()
    {
        return $this->belongsTo('Category');
    }

    public function subcategory()
    {
        return $this->belongsTo('Subcategory');
    }

    public function imalatci()
    {
        return $this->belongsTo('User');
    }

    public function productImage(){
        $this->hasMany('ProductImage');
    }

}
在控制器类中,您可以编写非常简单的代码来验证规则

namespace App\Http\Controllers;

use App\Category;
use App\Product;
use App\ProductImage;
use App\SubCategory;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
use Intervention\Image\Facades\Image;

class ProductsController extends Controller
{
    public function getIndex()
    {
        $products = Product::all();

        return view('pages.imalatci.products')
            ->with('products', $products);

    }

    public function postAddproduct()
    {
        $product = new Product();
        $subcategory = SubCategory::find(Input::get('subcategory'));
        $product->category_id = $subcategory->category_id;
        $product->subcategory_id = Input::get('subcategory');
        $product->imalatci_id = Input::get('imalatci_id');
        $product->title = Input::get('title');
        $product->description = Input::get('description');
        $product->price = Input::get('price');
        $image = Input::file('product_main_img');
        $filename = Input::get('imalatci_id') . '_' . date('Y_m_d_H_i_s') . '_' . $image->getClientOriginalName();
        Image::make($image->getRealPath())->resize(250, 280)->save('images/products/' . $filename);
        $product->image = 'images/products/' . $filename;
        if (!$product->save()) {
            flash()->important('Hata Oluştu');

            return Redirect::to('/imalatci/products')
                ->withErrors($product->getErrors())
                ->withInput();
        } else {
            flash()->success('Ürün başarıyla eklendi.Yönetici onayından sonra ürün yayınlanacaktır.');

            return Redirect::to('/imalatci/products');
        }
    }

}

这对我没什么帮助。我认为规则定义在哪里并不重要。你是对的,我认为问题在于重定向::到。withInput应位于错误之前。(并使用输入)没有必要。但我还是做了。它也帮不了我。在这段代码中,你需要编写规则而不是$rules
$validator=validator::make(输入::all(),产品::规则)返回未定义的类常量“规则”
namespace App\Http\Controllers;

use App\Category;
use App\Product;
use App\ProductImage;
use App\SubCategory;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
use Intervention\Image\Facades\Image;

class ProductsController extends Controller
{
    public function getIndex()
    {
        $products = Product::all();

        return view('pages.imalatci.products')
            ->with('products', $products);

    }

    public function postAddproduct()
    {
        $product = new Product();
        $subcategory = SubCategory::find(Input::get('subcategory'));
        $product->category_id = $subcategory->category_id;
        $product->subcategory_id = Input::get('subcategory');
        $product->imalatci_id = Input::get('imalatci_id');
        $product->title = Input::get('title');
        $product->description = Input::get('description');
        $product->price = Input::get('price');
        $image = Input::file('product_main_img');
        $filename = Input::get('imalatci_id') . '_' . date('Y_m_d_H_i_s') . '_' . $image->getClientOriginalName();
        Image::make($image->getRealPath())->resize(250, 280)->save('images/products/' . $filename);
        $product->image = 'images/products/' . $filename;
        if (!$product->save()) {
            flash()->important('Hata Oluştu');

            return Redirect::to('/imalatci/products')
                ->withErrors($product->getErrors())
                ->withInput();
        } else {
            flash()->success('Ürün başarıyla eklendi.Yönetici onayından sonra ürün yayınlanacaktır.');

            return Redirect::to('/imalatci/products');
        }
    }

}