dcdamianchase's avatar

How to assign an image to an article

Hi, I am trying to find an easy way of assigning an image to the article.

I am able of successfully creating an article and uploading the data onto the database as well as uploading an image and moving it into the public folder.

However I am stuck as I still cannot figure out how to assign the image onto the article after researching it.

Heres the code:

Article Model

class Article extends Model {

// Protecting from the mass assignment...
protected $fillable = [

    'title',
    'type',
    'body',
    'published_at'

];

// A single article is owned by a user...
public function user()
{
    return $this->belongsTo('App\User');
}

// A single article can have one image...
public function image()
{
    return $this->hasOne('App\Image');
}

Article Controller

// Store the article... public function store(ArticleRequest $request) {

    $article = new Article($request->all());

    // Assign the user id to the created article...
    Auth::user()->articles()->save($article);

Article Migration

public function up() { Schema::create('articles', function (Blueprint $table) { $table->increments('id');

        $table->integer('user_id')->unsigned();
        $table->integer('image_id')->unsigned();

        $table->string('title');
        $table->string('type');
        $table->text('body');
        $table->timestamps();
        $table->timestamp('published_at');

        $table->foreign('user_id')
              ->references('id')
              ->on('users');



    });
}

Image model

class Image extends Model {

// Protecting from the mass assignment...
 protected $table = 'images';
    protected $fillable = [
    'title',
    'description',
    'image'
];

// An image blongs to a single article
public function article()
{
    return $this->belongsTo('App\Article');
}

Image Controller

public function upload() { return view('/admin/media/imageupload'); }

public function store(Request $request)
{
    $image = new Image();
    $this->validate($request, [
        'title' => 'required',
        'image' => 'required'
    ]);
    $image->title = $request->title;
    $image->description = $request->description;
    
    if($request->hasFile('image')) {
        $file = Input::file('image');
        $name = time(). '-' .$file->getClientOriginalName();
        $image->filePath = $name;

        $file->move(public_path().'/uploads/', $name);
    }
    $image->save();
}

Image Migration

public function up() { Schema::create('images', function (Blueprint $table) {

        $table->increments('id');
        $table->string('title');
        $table->text('description');
        $table->string('filePath');
        $table->timestamps();
        
    });
}
0 likes
2 replies
rdelorier's avatar

You have a few options:

1: send the article id along with the image form, probably not possible if the image uploads before the article does 2: return the image id from the controller and add it to the article form, then pass it along when you create the article 3: upload the image along with the article and create everything together

coder's avatar
coder
Best Answer
Level 2

if you want to do using relationship the way currently you are doing then it should be same as you select tags at the time of article creation(since you are not using tags). so the approach is simple. when you create your article give an option to select images which are stored in the database( use j query plugin for that). or other way is don't create a separate table for images if a article has one image then store it along with article.

hope that is helpful

1 like

Please or to participate in this conversation.