vipin93's avatar

Image updating problem L4.2

i try to make user can edit there uploaded image but i want to edit image which belongs to only his/her. In my case user able to edit other image my controller

public function edit($id)
    {
        $image = Image::findOrFail($id);
 
        return View::make('images.edit', compact('image'))->with('title', 'edit image');
    }
help me!! 
thanks!
0 likes
4 replies
bobbybouwmann's avatar

You select based on the image and not on the user! Do you have a relation setup between the user and the image? How did you link the user to the image? You can do the inverse look up for your edit function ;)

vipin93's avatar

@blackbird my relationship is

User model
public function images()
    {
        return $this->hasMany('Image');
    }
My image model
public function user()
    {
        return $this->belongsTo('User');
    }
bobbybouwmann's avatar
Level 88

You should connect the pictures to the user with sync, attach or detach here is an example:

public function create() {
    $user = Auth::user();
    $file = Input::file('image');
    $picture = new Image;
           
    $extension = $file->getClientOriginalExtension();
    $sha1 = sha1($file->getClientOriginalName());
    $filename=date('Y-m-d-h-i-s').".".$sha1.".".$extension;
    $path = public_path('img/users/' . $filename);
    $picture->path='img/users/' . $filename;
    $picture->save();

    // Add the connection between the image and the user
    $user->images()->attach($picture->id);
}

public function edit($id) {
    $user = Auth::user();
    $picture = Image::findOrFail($id);
    $file = Input::file('image');

        if ($file)
    {
        // Remove the connection between the current image and the user
        $user->images()->detach($picture->id);

        // Do the same process as above for getting the image ready
        // Then again use attach to connect the image and the user
    }
}
vipin93's avatar

@blackbird my store method is

public function store()
    {
         $input = Input::all();

         $this->uploadImageForm->validate($input);

         $img = new Image;

         $img->title          = Input::get('title');
         $img->description    = Input::get('description');
         $img->status         = Input::get('status');
         $img->user_id        = Auth::user()->id;

          if (Input::hasFile('file'))
        {

        $file       = Input::file('file');

        $name       = time() . '-' . $file->getClientOriginalName();

        $file       = $file->move(public_path(). '\images', $name);

        $image      = Imag::make($file->getRealPath())->resize('320','240')->save($file);

        $img->file  = $name;
        } 

         if ($img->save()) 
         {
            return Redirect::back()->with(['global' => 'Your image file has been uploaded successfully.', 'type' => 'success']);
         }else
         {
            return Redirect::back()->with(['global'=> 'Your upload could not be succeeded.' , 'type' => 'danger']);
         }
    }

whats the problem with this

Please or to participate in this conversation.