dimanche 31 mars 2019

Laravel 5.8: Get Session id from DataSet

I made a table which lists all the sessions of a specific user. I want to be able to delete a session from a button I created which should point to the delete route (/sessions/ID_STRING).

Table

@foreach($sessions as $session){
   <tr>
      <td><i data-sessionId=''></td>
   </tr>
@endforeach

The rest is done via JS, however, this doesn't work. After minutes of debugging, I found out that $session->id returns 0 instead of the real id. I set a breakpoint right after the line where I'm getting my sessions and took a look at the array.

$sessions = Session::whereUserId($user->id)->get();

Inside my debugging window I tried a few things and the result surprised me:

$sessions[0]->getAttributes()["id"]; //returns correct id
$sessions[0]->id; //returns 0
$sessions; //searching for "attributes" shows the correct id too

Why does it return 0 when accessing it via the magic method and how can I fix this? The table is rendered via JS so I can't use my workaround with ->getAttributes()

Sessions.php

class Session extends Model
{
    public $timestamps = false;

    protected $hidden = [];
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire