I have my Controller set up as such:
JavaScript
x
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppModelsBooking;
class eventController extends Controller
{
/**
* Display a listing of the resource.
*
* @return IlluminateHttpResponse
*/
public function index()
{
$events = array();
$bookings = Booking::all();
foreach($bookings as $booking) {
'id' => $booking->id,
'title' => $booking->title,
'resourceId' => $booking->resourceId,
'start' => $booking->start_date,
'end' => $booking->end_date,
];
}
return view('home', ['events' => $events]);
}
}
Here I can pass everything from my DB into my view, but how can I filter these out such that only entries from a certain user_id is displayed? This is what my table looks like: https://imgur.com/AUZhA1L.
I have attempted to use the {user} blade but am stuck.
Advertisement
Answer
Change this line:
JavaScript
$bookings = Booking::all();
to this:
JavaScript
$bookings = Booking::where('user_id', 9)->get();
Or if you want to get the logged in user, you can use Auth
.
Add this line after use AppModelsBooking;
JavaScript
use IlluminateSupportFacadesAuth;
and then:
JavaScript
$bookings = Booking::where('user_id', Auth::id())->get();