Rebaz Salih
Rebaz Salih

Reputation: 39

Problem in Retrieving Data of Current user in Laravel

I have two tables in database

  1. local with (ID,comment,entry_by).
  2. tb_users with ID and rest info about users

I want to retrieve back comments of current logged in user in controller

Controller :

public function getIndex( Request $request )
{

    $logeduser= Auth::user()->id;
    $comment = DB::table('local')->select('org_id', 'comment')->where('entry_by', '=', '$logeduser')->get();

    return view('dashboard.index', $comment);
}

View:

@foreach ($comment as $comm)
    {{$comm->comment}}
@endforeach

it does not work without any error

Upvotes: 0

Views: 36

Answers (2)

Bonish Koirala
Bonish Koirala

Reputation: 651

You must edit your controller code as given below

public function getIndex( Request $request )
{

    $logeduser= Auth::user()->id;
    $comments = DB::table('local')->select('comment')->where('entry_by', $logeduser)->get();

    return view('dashboard.index', compact('comments'));
}

and your view must have

@foreach ($comments as $comm)
    {{ $comm->comment }}
@endforeach

Upvotes: 0

Josh Young
Josh Young

Reputation: 1366

Your Query is searching in 'entry_by' for the string '$logeduser'

try this

public function getIndex( Request $request )
{

    $logeduser= Auth::user()->id;
    $comment = DB::table('local')->select('org_id', 'comment')->where('entry_by', '=', $logeduser)->get();

    return view('dashboard.index', $comment);
}

remove the quotes around your variable

Upvotes: 1

Related Questions