Support the ongoing development of Laravel.io →
posted 10 years ago
Eloquent
Last updated 2 years ago.
0
if (Auth::user()->id==Post::find($id)->user->id)
{
//allow edit
}
else
{
//throw exception/error/etc...
}
Last updated 2 years ago.
0

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();

		}

	}
Last updated 2 years ago.
0

Hmmm... paste the model please

And try if simple

Post::find(1) 

is working

Last updated 2 years ago.
0

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');
  }

Last updated 2 years ago.
0

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');

		}
Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Reached reached Joined 27 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.