vipin93's avatar

Call to undefined method Illuminate\Database\Query\Builder::attach()

I try to attach a image with Auth user but it give error Call to undefined method Illuminate\Database\Query\Builder::attach()

my controller is

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

         $this->uploadImageForm->validate($input);
         $user = Auth::user();
         $img = new Image;

         $img->title          = Input::get('title');
         $img->description    = Input::get('description');
         $img->status         = Input::get('status');
        

          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;

        
        
        } 
           // Add the connection between the image and the user
          $user->images()->attach($img->id);
         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']);
         }
    }
0 likes
27 replies
vipin93's avatar
Image model
public function user()
    {
        return $this->belongsTo('User');
    }
}

and user model
public function images()
    {
        return $this->hasMany('Image', 'user_id')->latest();
    }
usman's avatar

Ok, then you cannot call attach on hasMany type of relations. It is only available for belongsToMany. Try using save instead.

usman's avatar

@vipin93 you can also use the associate method from the image side of the relation as well.

usman's avatar

@vipin93 something like:

        $image->user()->associate($request->user());
        $image->save();

Usman.

vipin93's avatar

@usman I'm using now L4.2

but when i try like

$img->user_id        = Auth::user()->id;

Its working

vipin93's avatar

@usman My problem is how to edit image only edit person who belongs to that image

You have any idea like any filter or something

vipin93's avatar

@usman here is edit controller

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

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

         $img = Image::with('user')->findOrFail($id);

         $img->title          = Input::get('title');
         $img->description    = Input::get('description');
         $img->status         = Input::get('status');
       

          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->update()) 
         {
            return Redirect::back()->with(['global' => 'Your image file has been updated successfully.', 'type' => 'success']);
         }else
         {
            return Redirect::back()->with(['global'=> 'Your update could not be succeeded.' , 'type' => 'danger']);
         }
    }

usman's avatar

@vipin93 something like:

    $image->user()->associate($request->user());
    $image->save();
vipin93's avatar

@usman i did not get your reply

 $image->user()->associate($request->user());
    $image->save();

where i use it?

usman's avatar

@vipin93

  1. query your image / create you image
    $image = App\Image::find(1);
  1. edit image
    $image->this = that;
    ....
  1. associate the image to the current logged in user:
 $image->user()->associate($request->user());
  1. save the image:
$image->save();

:)

usman's avatar

@vipin93 I think I miss understood you. If you want to edit the user for the image you can do something like:

$image = App\Image::find(1);
$user = $image->user();

//edit user
$user->name = 'edit name';

//and simply save the user
$user->save();

Usman.

vipin93's avatar

@usman my above edit controller where i have to put your code??

plz help me out!! thanks

vipin93's avatar

@usman no i want to edit image only who belongs to that image

I dont want to edit user!

usman's avatar

@vipin93 I am having trouble understanding you... :( would you state it for once clearly, what you are after?

vipin93's avatar

@usman my problem is that if any one try to edit image who does not belongs to that image it redirect(any where ) like if My profile only i can edit. I hope you will understand my problem!

usman's avatar
usman
Best Answer
Level 27

@vipin93 hmmm ok, create the following method inside you User model:

    public function owns($resource)
    {   
        return $this->id == $resource->user_id;
    }

Now you can check the ownership on an image at the beginning of your edit action. It would be easier, had you been using form requests though:

public function edit($id)
{
    $image = Image::findOrFail($id);    
    $user = Auth::user();
    if(! $user->owns($image) ) //redirect back to some permission denied page
    //continue processing.
}

I hope you will understand this time :)

Usman.

anouarabsslm's avatar

@vipin93 : since you already create an instrance of you model image using $image = new Image; go ahead an try this:

$user->images()->associate($image);

$user->save();

if did not work tray to share you users and images table schema

vipin93's avatar

@usman thanks bro its working I have problem since last 7 days and i tried many way but it did not work

but You did it thanks alot!!

nguyenhoa072's avatar

Call to undefined method Illuminate\Database\Query\Builder::file()

Can you help me?

Please or to participate in this conversation.