Mikaelo's avatar

Getting ID of uploaded file

Hello guys, I am working on a simple file uploading and experiencing a small problem.

I have function addNewArticle which handles getting some data from the form and then inserting them into database via Eloquent. Now there is a field image_id which I need to fill with the ID of uploaded file/image. But I dont know how to get this ID? (When saving the image name and url into database, ID is set to autoincrement)


public function addNewArticle() 
    {
        $data = array (
        'title'=> Input::get('title'),
        'content'=> Input::get('content'),
        'image_id'=> how to get the ID of uploaded file???
        );
Article::insertArticle($data);

Btw my form for handling upload looks something like this in my view:


{{ Form::open(array('action' => 'AdminController@addNewArticle','files' => true)) }}
blablabla

{{ Form::file('image') }}

Thank you
0 likes
8 replies
Mikaelo's avatar

Ah, that looks interesting. Now how do I use it in my case? Because I need to get this ID ideally in controller ( in my models I am using Eloquent btw, not a query builder)

Kryptonit3's avatar

usually the image record would have the foreign key to the article id.

bestmomo's avatar

When you insert your image you must get the id. I dont know how you organize your code. I suppose you have a function somewhere to insert this image. Just return the id to set it as parameter for article insert. All Query builder methods can be used on Eloquent models, so you can use inserGetId, but you also have the id in object returned by save or create method on model.

Mikaelo's avatar

Thank you guys for your time. Well it looks like this.

//Controller


public function addNewArticle() 
    {
      //getting article article
    $data = array (
    'title'=> Input::get('title'),
    'content'=> Input::get('content'),
    'image_id'=> I NEED TO GET THIS SOMEHOW :)

    );

    //getting the name of uploaded image
    $name = Input::file('image')->getClientOriginalName();

    //move the uploaded image
    Input::file('image')->move('images', $name.rand(4,10).'.jpg');

    //getting image data from article
    $imagedata = array (
    'name'=> $name,
    'url'=> $name
    );

    
    //saving image data into Image eloquent model
    Image::insertImage($imagedata);
    
    //saving article data into Article eloquent model
    Article::insertArticle($data)

    return Redirect::to('admin')->with('message','Article was added successfully');
}

Article model


class Article extends Eloquent {
   protected $fillable = array('title','content');

public $timestamps = true;

public function image()
{
    return $this->belongsTo('Image');
}

// ziskame data z databaze
public static function getData()
{
    $articles = Article::orderBy('id','DESC')->get();

    return $articles;
}

// inserting a new article into database
public static function insertArticle($data)
{
    $article = new Article;
    $article->fill($data);
    $article->save();
}

// Image Model


class Image extends Eloquent {
    protected $fillable = array('url','name');

    public $timestamps = true;


public function article()
{
    return $this->hasMany('Article');
}

    public static function insertImage($imagedata)
{

    inserting the image into database
    $image = new Image;
    $image->fill($imagedata);
    $image->save();
}

bestmomo's avatar
Level 52

@Mikaelo In your controller :

public function addNewArticle() 
    {

    //getting the name of uploaded image
    $name = Input::file('image')->getClientOriginalName();

    //move the uploaded image
    Input::file('image')->move('images', $name.rand(4,10).'.jpg');

    //getting image data from article
    $imagedata = array (
    'name'=> $name,
    'url'=> $name
    );
    
    //saving image data into Image eloquent model
    $image_id = Image::insertImage($imagedata);

    //getting article article
    $data = array (
        'title'=> Input::get('title'),
        'content'=> Input::get('content'),
        'image_id'=> $image_id)
    );

    //saving article data into Article eloquent model
    Article::insertArticle($data)

    return Redirect::to('admin')->with('message','Article was added successfully');
}

And in image model :

class Image extends Eloquent {
    protected $fillable = array('url','name');

    public $timestamps = true;


public function article()
{
    return $this->hasMany('Article');
}

    public static function insertImage($imagedata)
{

    $image = new Image;
    $image->fill($imagedata);
    $image->save();
    return $image->id;
}
Mikaelo's avatar

bestmomo: I just figured it out by myself, but thank you ;)

Please or to participate in this conversation.