php - Having trouble implementing a multiple image upload system in Laravel -
php - Having trouble implementing a multiple image upload system in Laravel -
in web application, people can create posts. alonng these posts, may upload multiple images. this, have posts table , image table. posts hasmany images, , image belongs post. therefore, images table has image_path column , post_id column. problem getting post_id. because cant post id before post has been uploaded, can't set post image belongs to. anyway, here code:
public function create(){ $post = new post; $post->title=input::get('title'); $post->body=input::get('body'); $post->category=input::get('category'); $post->save(); $images = input::file('images'); foreach($images $image) { $destinationpath = 'public/uploads/'; $filename = $image->getclientoriginalname(); $image->move($destinationpath, $filename); $id=auth::id(); $file = new image(); $file->image_path = 'uploads/' . $image->getclientoriginalname(); $file->description = ''; $file->post_id=input::get('post_id'); $file->save(); }
what best way go fixing problem?
once save post
using $post->save()
may id using:
// ... $post->save(); /... $file->post_id = $post->id;
you may seek instead:
// ... $post->save(); $destinationpath = 'uploads/'; $images = input::file('images'); foreach($images $image) { $filename = $image->getclientoriginalname(); $image->move($destinationpath, $filename); $file = new image(); $file->image_path = 'uploads/' . $image->getclientoriginalname(); $file->description = ''; $post->images->save($file); }
php mysql file-upload laravel laravel-4
Comments
Post a Comment