@Mikaelo use polymorphic relationship
Multiple Images in article - gallery
Hello guys,
I am trying to find best way to deal with images in articles. Lets say I have simple CMS, which allows me to create an article, and then upload/assign multiple images to this article. So basically create a gallery.
Now I know how to work with one image/per one article, but dont know how to work with multiple images. It all starts with database.
Right now I have two tables: articles and images. Do I need to create a third one and then somehow connect everything via Eloquent relationships?
Because the problem is, that the images are not exclusive just for one article, but may be used in more articles. (So it is a different situation compared to article comments for example)
@hassanjamal it isn't polymorphic in this case, as images only belong to articles and articles have many images (articles can share images, so to speak). Polymorphic relationships are for such situations when a model can belong to more than one other model. Take a comment for example. If you think of Facebook, you can comment on a photo, a video, or a status update etc. Rather than creating three separate comment classes, you have one Comment class which is polymorphic to Photo, Video, and Status. Although this is a crude example, it should go some way to explain its use.
@Mikaelo in your case a simple Many To Many relationship will do the trick.
Article model
public function images() {
return $this->belongsToMany('App\Image'); // or whatever your namespace is
}
Image model
public function articles() {
return $this->belongsToMany('App\Article'); // or whatever your namespace is
}
article_image table
Set up a pivot table, you can do this manually or use @JeffreyWay's Laravel 5 Extended Generators Package.
You'd need to use make:migration:pivot Article Image and this will set it up for you. Or, if you'd want to, you can do it manually by creating a new migration. It should look like this:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateArticleImageTable extends Migration {
public function up()
{
Schema::create('article_image', function(Blueprint $table)
{
$table->increments('id');
$table->integer('article_id')->unsigned()->index();
$table->foreign('article_id')->references('id')->on('articles')->onDelete('cascade');
$table->integer('image_id')->unsigned()->index();
$table->foreign('image_id')->references('id')->on('images')->onDelete('cascade');
$table->timestamps();
});
}
public function down()
{
Schema::drop('article_image');
}
}
This will create a simple table:
| id | article_id | image_id |
|----|------------|----------|
| 1 | 1 | 2 |
| 2 | 2 | 1 |
| 3 | 2 | 2 |
| 4 | 3 | 1 |
| 5 | 3 | 3 |
Article(1) has one Image(1)
Article(2) has two Images(1,2)
Article(3) has two Images(1,3)
ArticlesController controller
In your ArticlesController or whatever it is called you'd need to do something like the following. I'm sure this will make sense to you now.
<?php namespace App\Http\Controllers;
use App\Article;
class ArticlesController extends Controller
{
public function index()
{
$articles = Article::with('images')->get();
return view('articles.index', compact('articles')); // whatever your view is called
}
public function show($id)
{
$article = Article::find($id);
$article->load('images');
return view('articles.show', compact('article')); // whatever your view is called
}
}
I hope this has cleared things up for you, if you have any questions or would like me to explain things further, just shout.
Please or to participate in this conversation.