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']);
}
}
@vipin93 If the user is not logged in you will get this error.
@usman but user is logged in!!
I have filter
@vipin93 how did you define the relation between user and image?
Image model
public function user()
{
return $this->belongsTo('User');
}
}
and user model
public function images()
{
return $this->hasMany('Image', 'user_id')->latest();
}
Ok, then you cannot call attach on hasMany type of relations. It is only available for belongsToMany. Try using save instead.
@vipin93 you can also use the associate method from the image side of the relation as well.
@vipin93 something like:
$image->user()->associate($request->user());
$image->save();
Usman.
@usman
I'm using now L4.2
but when i try like
$img->user_id = Auth::user()->id;
Its working
@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 read my replies above you will get the idea.
@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']);
}
}
@vipin93 something like:
$image->user()->associate($request->user());
$image->save();
@usman i did not get your reply
$image->user()->associate($request->user());
$image->save();
where i use it?
@vipin93
query your image / create you image
$image = App\Image::find(1);
edit image
$image->this = that;
....
associate the image to the current logged in user:
$image->user()->associate($request->user());
save the image:
$image->save();
:)
@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.
@usman my above edit controller where i have to put your code??
plz help me out!!
thanks
@usman no i want to edit image only who belongs to that image
I dont want to edit user!
@vipin93 I am having trouble understanding you... :( would you state it for once clearly, what you are after?
@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 i have mentioned my edit controller at page-1
@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.
@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
@usman its showing error
No query results for model [Image].
@vipin93 I have updated my above reply use that code...
@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!!
Call to undefined method Illuminate\Database\Query\Builder::file()
Can you help me?
Please sign in or create an account to participate in this conversation.