anon34905's avatar

Get UserID from database and compare with logged-in user

Hi,

i want to compare two userid's (fetched from the session and database)

Table "host":

id
name
user_id
....

Table "users":

id
name
....

Now in my controller i want to execute a simple SELECT-Query like:

$host = DB::table('hosts')->select('user_id')->where('id', $id)->get();

and compare the returned user_id with the current logged in user

$id = Auth::id();

Simple if-statement should do the trick, but it does not work. Any idea? :)

0 likes
10 replies
jlrdw's avatar

Put from session $s, from auth $a, and just compare the two. Oh the one from database is bcrypted. Why are you doing this? And how are you storing the session?

anon34905's avatar

The session is cookie-based(?). I´ve just simply done a "php artisan make:auth" and started creating my controller,views etc.

I want to check, if the user is the Owner of the Host. If not, he gets an 403. (or gets redirected to /home) If he's the owner, the results from the database will be displayed.

Snapey's avatar

for starters, this $id = Auth::id(); sets $id, and does not compare

You can add the userID to the query and then just check if you get a result;

$host = DB::table('hosts')->where('id', $id)->where('user_id',Auth::id())->first();
anon34905's avatar

Hi Snapey,

thanks for your response! :) Appreciate your help.

i tried your code, but it will throw an error.

The Response content must be a string or object implementing __toString(), "object" given.

If i do

return $host->id;

OR

return $host->hostname;

i get some output. Any idea?

Snapey's avatar

You did not mention what happened AFTER checking the user...

Show your full code or describe the issue.

m7vm7v's avatar

You can try:

$host = DB::table('hosts')->where('id', $id)->first();

if($host->user_id == auth()->user()->id){
    //They are matching
}

//No match

I will recommend you to make a Model for the hosts

So then the logic will stay there:

static function theLoggedUserIsAuthenticated(Host $host){

    return auth()->user()->id == $host->user_id;
}

then in your controller:

if(Host::theLoggedUserIsAuthenticated($id)){
    //They are matching
}

//No match
anon34905's avatar

Hi everybody,

@Snapey Simple task. I want to check, if the ID of the logged in user is the same for the requested host (user_id)

If yes, i want to display the data, if not, the user will be redirected to /hosts.

If i implement this code from above, i get a "The Response content must be a string or object implementing __toString(), "object" given."

My HostsController.php:

    /**
     * Show specific Host.
     *
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {

        $host = DB::table('hosts')->where('id', $id)->first();
          if($host->user_id == auth()->user()->id){
            return $host;
         }

        return redirect()->action('HostsController@index');
    }

If i edit my code like this, i get a correct response (Hostname, ID etc. as JSON)


 /**
     * Show specific Host.
     *
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {

       $host = Host::findOrFail($id);
       return $host;

Any idea?

Snapey's avatar
Snapey
Best Answer
Level 122
public function show($id)
{

    $host = Host::where('user_id',Auth::id())->where('id',$id)->first();
      if($host){
        return $host;
     }

    return redirect()->action('HostsController@index');
}
anon34905's avatar

@Snapey Laravel 5.4

With your code it works like a charme! :) Why do your code work, but my don't?

Please or to participate in this conversation.