lundi 26 octobre 2015

Laravel 5 Controller Does Not Returns Validator Errors

I have a Product model and ProductController in Laravel 5.

But Validator errors doesn't return.

I couldnt understant why happens like this.

Here is Model Class:

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $table = "products";


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

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

}

Here is Our Controller Class:

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();
    }


}

How could be solve this issue ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire