dcdamianchase's avatar

Many to Many Relationship - Help Needed

Hi I am building my website with a many to many relationship.

I came across this thread:

https://laracasts.com/discuss/channels/laravel/multiple-images-in-article-galerry

And I am wandering how to implement this on the front end.

Ive got a many to many Article and Image models and the pivot table exactly like the one in the thread above.

I have a form to upload an image into the database, therefore giving me an Image id.

But how do I retrieve that image in the 'create article form' and connect the two when the pivot table needs both the ids of the article and the image in order to create the relationship?

Thanks for looking.

0 likes
9 replies
Jaytee's avatar

If you're an upgraded member here on Laracasts, check out the 'Build Flyer with me' series, Jeffrey explains how to upload/process/store photos.

Otherwise, use something like Intervention Image. (But I highly recommend the Series as you'll learn more about it.)

tykus's avatar

What is your intended workflow; do you intend to upload the images separately and then visit you articles/create - you could use an Image Picker plugin to display thumbnails over a normal select input;

or

do you want to upload the images along with the article; in which case you create the images and attach an array of the created image id's to the article over the `belongsToMany relationship

or

do you wish use something like dropzone to upload images using AJAX on the articles/create view? You could build an array of ids returned from the AJAX calls and handle them in the articles/store controller methods as above

dcdamianchase's avatar

Hi Tykus,

The first one,

I intend to upload and process the images separately, (thus allowing me to reuse the images for the articles instead of uploading the same image twice), the same way Wordpress does.

The image picker seems like a good solution, however I am confused with what kind of a relationship I need to use. I am currently using many to many with a pivot table and Jeffrey used a One to Many relationship in his flyer video.

tykus's avatar

You are correct to use a many-to-many; assuming the pivot table is called 'article_image' and FK's on it are 'article_id' and 'image_id':

// Article.php
public function images()
{
    return $this->belongsToMany('App\Image'); 
}


// Image.php
public function articles()
{
    return $this->belongsToMany('App\Article'); 
}

In your controller, you can create an article as normal;

$article = App\Article::create($request->all());
$article->images()->attach($request->image_ids); // image_ids will come from the multiple select image field in the form

dcdamianchase's avatar

The controller part is exactly what I needed help with. I will implement the picker into my create article form and let you know if everything works fine.

Thank you very much for your help.

1 like
tykus's avatar

Just be sure that the form passes an array to the controller name="image_ids[]" becomes $request->image_ids in your controller:

<select name="image_ids[]" multiple="multiple">
    @foreach($images as $image)
        <option data-img- src="{{$image->path}}" value="{{$image->id}}">{{ $image->caption }}</option>
    @endforeach
</select>
imoh's avatar

@tykus if i want to follow the second option of creating----------- do you want to upload the images along with the article; in which case you create the images and attach an array of the created image id's to the article over the `belongsToMany relationship-----

how do i go about this because i have created a form like this with type file and name=file[] ,i also created textarea for my article

i have also created the three tables the Image the Article and the pivot table but my problem is that i know how to store image and article separately on my database but in this case since i am creating the article and images together, how do i store both of them,

That is how will my controller(both images and articles or if i need only one controller) look like to store the method? and how will i display it in my view

Thank you very much for your help

imoh's avatar

?? no help from anyone yet??

i was just thinking if it isnt possible to use a one-to-many relationship in this case since an image has many articles is one to many

Please or to participate in this conversation.