1

i have two table

posts

id|post_title|post_content

post_images

id|images|post_id

Controller

public function AddPost(Request $request)
    {
        Post::create($request->all());
        // PostImage::create();
        return Redirect::to('Post');
    }

Also i have added Relation

class Post extends Model
{
protected $table = 'posts';

    public function images()
    {
        return $this->hasMany('App\PostImage');
    }
}


class PostImage extends Model
{
    public function post()
    {
        return $this->belongsTo('App\Post');
    }
}

I have one form where i adding post title ,post content and selecting multiple images. My question is how I can store post images along with post id in post_images table?

1 Answer 1

1

In you controller AddPost function try (using Form model binding)

    $post = new Post($request->all());
    PostImage::post()->images()->save($post);

Or you can also do like this I think

public function AddPost(Post $post, Request $request)
{
    $input = Input::all();
    $input['post_id'] = $post->id;
    PostImage::create( $input );
    return Redirect::to('Post');
}
Sign up to request clarification or add additional context in comments.

1 Comment

if you are interested..found a simple tutorial which you can do for practice. flynsarmy.com/2015/02/…

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.