Search code examples
laravellaravel-11laravel-authenticationlaravel-breezephp-8.2

Undefined property: Illuminate\Auth\AuthManager::$id


I am building a CRUD notes app and I have this controller, I am trying to get all the notes from a user to display in the index page also I am using laravel breeze. The IDE says "Undefined method 'id'." I've try all the solutions I could find in laracast and here and nothing works, I know I am forgetting something I just cant remember what it is, it's been a while since I used laravel. If you guys can help me it would be great! thank you

 <?php

namespace App\Http\Controllers;

use App\Models\Note;
use Inertia\Inertia;
use App\Http\Resources\NoteResource;
use App\Http\Requests\StoreNoteRequest;
use App\Http\Requests\UpdateNoteRequest;

class NoteController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        //
        $notes = Note::where('user_id', auth()->id())->get();
        return Inertia::render('Notes/Index', [
            'notes' => NoteResource::collection($notes),
        ]);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
        return Inertia::render('Notes/Create');

    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(StoreNoteRequest $request)
    {
        //
        $note = Note::create([
            'user_id' => auth()->id,
            'title' => $request->title,
            'content' => $request->content,
            'is_pinned' => $request->is_pinned,
            'color' => $request->color,

        ]);

        return redirect()->route('notes.show', $note)->with('success', 'Note created successfully');

    }

    /**
     * Display the specified resource.
     */
    public function show(Note $note)
    {
        //
        return Inertia::render('Notes/Show', [
            'note' => new NoteResource($note),
        ]);
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(Note $note)
    {
        //
        return Inertia::render('Notes/Edit', [
            'note' => new NoteResource($note),
        ]);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(UpdateNoteRequest $request, Note $note)
    {
        //
        $note->update([
            'title' => $request->title,
            'content' => $request->content,
            'is_pinned' => $request->is_pinned,
            'color' => $request->color,
        ]);

        return redirect()->route('notes.show', $note)->with('success', 'Note updated successfully');

    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Note $Note)
    {
        //
        $Note->delete();
        return redirect()->route('notes.index')->with('success', 'Note deleted successfully');
    }
    public function togglePin(Note $note)
    {
        $note->update([
            'is_pinned' => !$note->is_pinned,

        ]);
        return redirect()->back()->with('success', 'Note pinned successfully');
    }
}

Solution

  • auth()->id in the index method: If the user is not authenticated, auth()->id will return null. Trying to use null as a foreign key in your query will cause problems, and in some cases, might lead to the "Undefined method 'id'" error if Laravel tries to implicitly convert it.

    public function index()
    {
        // Use the Auth facade and check if the user is authenticated
        if (Auth::check()) {
            $notes = Note::where('user_id', Auth::id())->get();
            return Inertia::render('Notes/Index', [
                'notes' => NoteResource::collection($notes),
            ]);
        } else {
            // Handle the case where the user is not authenticated.  
            // You could redirect them to the login page or display a message.
            return redirect('/login'); // Example: redirect to login
        }
    }