Search code examples
phplaravelcontroller

Laravel Model get all where $id = Auth::user()->id


How to List all rows from a DB where $id matches the logged user id. I'm using default Auth from Laravel.

At the moment i can list them all with this method in my controller:

public function index(){

    $invoices = Invoice::all();
    return view('index', compact('invoices'));
}

But i just want the ones that are from this user which is logged in:

Something like

$invoices = Invoice::where('id', '=', Auth::user()->id);

Solution

  • Your code Seems almost right. I would do:

    $invoice = Invoice::where('id', Auth::user()->id)->get();

    So basically use the get in order to fetch a collection. And maybe I would separate the user id in a varaible in case that you change the authentication in the future ;)