Feelsonix's avatar

Update other table with relationships

HI,

I'm french sorry for my english. I made a projet with Laravel, brefore i store my image url in my post table with a column url. Now i have make a new table named image, this table have a id and url column. I have make the relatinship in my two Models.

But with the controller i dont know how update the new url of the image i've upload in my image table.

This is my code:

https://pastebin.com/ms1yRCwb

How can i pass the url and the id of my post in my table Image ?

Thank you. Thomas.

0 likes
11 replies
BezhanSalleh's avatar

@feelsonix is the relationaship between your Post and Image model 1-1 or 1-m ? and it would help if you could post your models here so we can help you better.

BezhanSalleh's avatar

@feelsonix i don't know what you are doing here, your model relationships are in contradiction with the code you have in your controller...

from your controller when you delete an image its gonna create problems for other posts base on the relationship you have defined there... so either your relations are wrong or the code in your controller...

so let me know what are you trying to accomplish here... then i might be able to help you...

Feelsonix's avatar

@bezhansalleh Ok, my bad.

I have not modified my controller anymore this the controller for my previous method for storing the data. In the past i stored the url of the image in my table post.

Now i have create a Imageup Model, and i want to modify my update method for having my post ID and my post image url in the new table called image.

I have no make anymore modification on my controller because i dont know how doing this.

BezhanSalleh's avatar

@feelsonix just answer me this: in your scenario 1 post has many images right? post your models and controller here so if i get busy and won't be able to answer it on time some else might be able to help you. you might get a solution that way faster.

just start a new line with 3 ticks and paste your code then end it with three ticks

BezhanSalleh's avatar

@feelsonix let me see your migrations for post and image model i got 15mins i will send you the solution so you could just copy/paste and run it...

Feelsonix's avatar

@bezhansalleh

For the modification of my post table i have make no migration i add a image_id you can see: https://i.imgur.com/nDz6Tqa.png

And on my Image table i have make this: https://i.imgur.com/TG53daG.png

On my admin i can access to the image: https://i.imgur.com/lZQ5JqI.png

The problem for me its how make the in my update method on my controller.

Sorry if i not respect the conventions for migration, but this is for make a Proof of Concept.

Thomas.

BezhanSalleh's avatar
Level 25

@feelsonix PostController.php -- update method

    public function update(Request $request, $id)
    {

        $this->validate($request, array(
                'title' => 'required|max:255',
                'slug'  => 'required',
                'category_id' => 'required|integer',
                'body'  => 'required'
        ));

        // Validate the data
        $post = Post::find($id);
        $categories = Category::pluck('name','id');


        $post->title = $request->input('title');
        $post->slug = $request->input('slug');
        $post->category_id = $request->input('category_id');
        $post->user_id = Auth::id();
        $post->body = $request->input('body');

        if ($request->hasFile('featured_img')) {
            // Ajoute la nouvelle photo
            $image = $request->file('featured_img');
            $filename = time() . '.' . $image->getClientOriginalExtension();
            $location = public_path('img/' . $filename);
            Image::make($image)->fit(1200, 500)->save($location);

            $oldFilename = $post->postImage->url;

            $post->postImage()->createOrUpdate([
                'post_id' => $post->id
            ],[
                'url' => $filename
            ]);
            // Efface l'ancienne photo
            Storage::delete($oldFilename);

        }

        $post->save();

        Session::flash('success', "L'article à été correctement mis à jour.");

        return redirect()->route('posts.index');
    }

PostImage.php -- model

class PostImage extends Model
{
    protected $table = 'image';

    protected $fillable = ['url','post_id'];

    public function post()
    {
        return $this->belongsTo(Post::class);
    }
}

Post.php -- model

class Post extends Model
{
    protected $fillable = ['title','description','order'];

    public function postImage()
    {
        return $this->hasOne(PostImage::class);
    }
}

adjust the namespace for the models and migrations and you would be good to go... but you need some extra work to delete the image from storage and if save fails you could run the code for update inside a transaction. anyways good luck.

BezhanSalleh's avatar

@feelsonix you are most welcome ... mark it as answered if it helped so others can ignore it and if others want it they could refer to it.

Please or to participate in this conversation.