Home > Net >  Laravel Get Use id with session
Laravel Get Use id with session

Time:11-30

Im trying to get the id of the logged in user. When the user is clicking on the button his id need to get send to the data base, But I get an error: 1048 Column 'user_id' cannot be null.

This is my controller function:

    public function LendBook(Request $request)
    {
        $user_id = $request->session()->get('User_id');
        $book_id = $request->book_id;


        $q = new lend;
        $q->user_id = $user_id;
        $q->book_id = $book_id;

        $q->save();

        return 'test';
}
}

CodePudding user response:

here with this you get current user's id

use Illuminate\Support\Facades\Auth;

    $user = Auth::user();
    $user_id = $user->id;

CodePudding user response:

public function LendBook(Request $request)
{
    $user_id = auth()->id;
    $book_id = $request->book_id;

    $q = new Lend; // naming class should be Upper case "Lend"
    $q->user_id = $user_id;
    $q->book_id = $book_id;
    $q->save();

    return 'test';
}

naming class should be Uppercase look the basic php standard (PSR) enter link description here

  • Related