Hey,
I'm trying to build a query where I want only courses that belong to a user_id are displayed. Here's what I have currently:
Courses Controller
public function index()
{
$courses = DB::table('courses')->where('user_id', '1')->first();
return View::make('courses.index')->withCourses($courses);
}
However, that returns:
Trying to get property of non-object (View: /Applications/MAMP/htdocs/elasticbrain/app/views/courses/index.blade.php)
Any ideas what I might be doing wrong? Ideally, I'd like to use the logged in user ($user = Auth::user()->id;) as a way to dynamically set the user whose courses are shown.
Thanks, Marc
Can you show us your view? That is where your error is occurring.
Assumption being made: With courses being plural, you should probably be using get instead of first to get all of the courses and not just the first one.
$courses = DB::table('courses')->where('user_id', '1')->get();
If you want to use Auth::user()->id you can use a before filter on your route to make sure the user is logged in.
Hey,
Actually, yeh, it was the get problem. Nice one.
I already have the filter on to make sure a user can't see the page until they're logged in. However, how would I pass the logged in user's user_id to the query to replace:
->where('user_id', '1')
So basically
$course = DB:table('courses')->***Logged in User***->get();
Thanks for your help! Marc
@themarcthomas try
$courses = DB::table('courses')->where('user_id', Auth::user()->id )->get();
assuming your using laravel's auth system
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community