if (Auth::user()->id==Post::find($id)->user->id)
{
//allow edit
}
else
{
//throw exception/error/etc...
}
Thanks for your help TorchSK, i really appreciate it.
It's throwing an error when using the code below, am I missing something here?
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'posts.post_id' in 'where clause' (SQL: select * from `posts` where `posts`.`post_id` = 17)
public function edit($id)
{
if (Auth::user()->id == Post::find($id)->user->id)
{
return View::make('admin.listings.listings-edit', compact('post'));
}
else
{
return Redirect::back();
}
}
Hmmm... paste the model please
And try if simple
Post::find(1)
is working
So far ive been using the more generic "post" name, but actually the real name is "listing" in my controllers/models (just to clarify).
Doing a simple Listing::find(1) works fine. But it seems that the relationship is not being fetched properly.
Here is my Listing model:
public function listings() {
return $this->belongsTo('Category', 'category_id');
}
public function users() {
return $this->belongsTo('User', 'user_id');
}
public function user() {
return $this->hasMany('Listing');
}
Managed to fix it, below is the working code:
if (Auth::user()->id == Listing::find($id)->user->id)
{
$listing = Listing::find($id);
return View::make('admin.listings.listings-edit', compact('listing', 'categories'));
}
else
{
return Redirect::route('userlistings');
}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community