0

I am building a Slim Framework 4 Api Application, with Eloquent.

public/index.php

$capsule = new \Illuminate\Database\Capsule\Manager;
$capsule->addConnection($dbconfig);
$capsule->setAsGlobal();
$capsule->bootEloquent();

$app->add(new RequestUser()); // for calling middleware that adds user

Middlware

 class RequestUser
  {

public function __invoke(Request $request, RequestHandler $handler): Response
{
     ....................
    $user = User::where('emailid', $email)->first();
    $request = $request->withAttribute('user', $user);
    $request = $request->withAttribute('token', (string) $exploded_authorization[1]);
    return $handler->handle($request);
  }
  }

Controller

use Psr\Http\Message\ServerRequestInterface as Request;

public function create(Request $request, Response $response, $args)
    {
$user = $request->getAttribute('user'); // gives me the request user information

    }

Model

<?php

namespace App\Models\TableModel;

use Illuminate\Database\Eloquent\Model;
use DateTime;
use Psr\Http\Message\ServerRequestInterface as Request;


class Details extends Model
{
    protected $table = 'my_table';

    protected $primaryKey = 'Id';

    public $timestamps = false;

    protected $fillable = [];

    protected $hidden = [];

    protected $casts = [

    ];

    protected $appends = ['can_edit', 'can_delete'];


    public function getCanEditAttribute(){
        $now = date('Y-m-d H:i:s');
        return $this->Start_Date >= $now;
    }

    public function getCanDeleteAttribute(){
        //request contains the user information from the middleware? How to access here
        return 
    }


}

I want to access the $request in my Model so that i could get the user information, who is trying to access on CanDelete.

Alaksandar Jesus Gene
  • 6,523
  • 12
  • 52
  • 83

1 Answers1

0

You got the wrong kind of middleware, it needs to be a routing middleware and binded to route or route group directly, not $app->add

Shuyi
  • 906
  • 2
  • 13
  • 28