0

I am new in Laravel.

This is my laravel controller:

public function store()
    {
        $validator = Validator::make($data = Input::all(), City::$rules);
        if ($validator->fails())
        {
            return Redirect::back()->withErrors($validator)->withInput();

        }
        $image_temp=Input::file('image');
        $name = Input::file('image')->getClientOriginalName();
        $data['image']='';
        if(Image::make($image_temp->getRealPath())->save('public/up/city/'.$name)){
            $data['image']='up/city/'.$name;
        }
        City::create($data);
        return Redirect::route('admin.cities.index');
    }

and This is my model:

class City extends \Eloquent {
   protected $primaryKey='city_id';

    // Add your validation rules here
    public static $rules = [
         'title'     => 'required',
         'image'     => 'mimes:jpeg',
         'parent_id' => 'required',
         'name'      => 'required',
         'english_name'=>'unique:cities,english_name|required'

    ];
    // Don't forget to fill this array
    protected $fillable = ['name', 'parent_id', 'english_name','population','phone_prefix','image'];
}

And I have a form I use {{ $errors->first('inputName','<p class="error">:message</p>') }} bellow my form inputs, when I send form without filling inputs I get error under each form input. But when I fill out all form inputs and then submit the Laarvel validation return fail (I mean mass assignment not working and not registering, and redirects back to create page.) what is the problem?

14
  • 2
    Looks to me like you forgot title in the $fillable array. Commented Jan 15, 2015 at 18:31
  • 1
    City::$rules looks wonky to me... Should it be City::rules or City->rules? Commented Jan 15, 2015 at 18:32
  • @TimLewis It is the right syntax for accessing static properties. Commented Jan 15, 2015 at 18:34
  • @lukasgeiter Oh static, right. It's still ugly :P Commented Jan 15, 2015 at 18:34
  • 1
    @Asker Haha no, your syntax better than a lot I see on SO. The syntax on accessing static variables, as per my first comment. Commented Jan 15, 2015 at 18:53

1 Answer 1

1

Almost always the reason for a mass assignment error is a missing attribute in the $fillable array. In your case it is title.

protected $fillable = ['title', 'name', 'parent_id', 'english_name','population','phone_prefix','image'];
                          ^

Edit

Apparently the problem was actually that the title in the $rules array, which should have been name...

Sign up to request clarification or add additional context in comments.

3 Comments

Oh you are wrong here, the problem is from my $rules here. I mean the title in $rules array should be name. And anyway your answer is true because both of them should be same.Thanks
Accepted + up vote + A big thanks + Give me your Email :) (Kidding)
can you please answer to this stackoverflow.com/questions/27985327/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.