Hello Please I am trying to view the records of a user based on the user id I used as a foreign key in another table in laravel. Basically I want the name of the userID to show.
This is my Controller
public function index()
{
$id = User::all();
$organisedTrips = organisedTrip::where('userID', '=', $id)->get();
$organisedTrips = organisedTrip::paginate(4);
return view('welcome', compact('organisedTrips'));
}
2
Answers
User::all()
returns array of the users. Consider using pluck function and correct your code.Tip:
It can be done in better way using eloquent.
You can use auth() because this is the simplest and safest way
enter image description here
Or you can separate model and controller
enter image description here
enter image description here
I tried this before and it worked.