0

The following function in laravel stores my form input. I can't get it to store anything other than the author id and the title. It just won't store the keywords.

Below is the function in my Postcontroller.php

public function store()
    {
        $input = Input::all();

        $rules = array(
            'title' => 'required',
            'text'  => 'required',
        );

        $validation = Validator::make($input, $rules);

        if ($validation->fails()) {
            return Redirect::back()->withErrors($validation)->withInput();
        } else {
            // create new Post instance
            $post = Post::create(array(
                'title'  => $input['title'],
                'keywords'  => $input['keywords'],
            ));

            // create Text instance w/ text body
            $text = Text::create(array('text' => $input['text']));

            // save new Text and associate w/ new post
            $post->text()->save($text);

            if (isset($input['tags'])) {
                foreach ($input['tags'] as $tagId) {
                    $tag = Tag::find($tagId);
                    $post->tags()->save($tag);
                }
            }

            // associate the post with user
            $post->author()->associate(Auth::user())->save();

            return Redirect::to('question/'.$post->id);
        }
    }

Post.php (model)

<?php

class Post extends Eloquent {

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'posts';

    /**
     * Whitelisted model properties for mass assignment.
     *
     * @var array
     */
    protected $fillable = array('title');

    /**
     * Defines a one-to-one relationship.
     *
     * @see http://laravel.com/docs/eloquent#one-to-one
     */
    public function text()
    {
        return $this->hasOne('Text');
    }

    /**
     * Defines an inverse one-to-many relationship.
     *
     * @see http://laravel.com/docs/eloquent#one-to-many
     */
    public function author()
    {
        return $this->belongsTo('User', 'author_id');
    }

    /**
     * Defines a many-to-many relationship.
     *
     * @see http://laravel.com/docs/eloquent#many-to-many
     */
    public function tags()
    {
        return $this->belongsToMany('Tag');
    }

    /**
     * Defines an inverse one-to-many relationship.
     *
     * @see http://laravel.com/docs/eloquent#one-to-many
     */
    public function category()
    {
        return $this->belongsTo('Category');
    }

    /**
     * Defines a polymorphic one-to-one relationship.
     *
     * @see http://laravel.com/docs/eloquent#polymorphic-relations
     */
    public function image()
    {
        return $this->morphOne('Image', 'imageable');
    }

    /**
     * Defines a one-to-many relationship.
     *
     * @see http://laravel.com/docs/eloquent#one-to-many
     */
    public function comments()
    {
        return $this->hasMany('Comment');
    }

}
3
  • can you post your Post model? Commented Jul 17, 2014 at 13:07
  • Ah, I see it now. I have whitelisted fields in my model which I hadn't spotted before. I added my additional field there and it works now. Thanks for pointing me in the right direction! Commented Jul 17, 2014 at 13:19
  • No problem. If my answer has helped you, please mark it as accepted. Thanks and good luck! Commented Jul 17, 2014 at 13:21

1 Answer 1

1

You are stopping the mass assignment of keywords with your model settings.

Change

protected $fillable = array('title');

to

protected $fillable = array('title', 'keywords');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.