PHP code example of shahal-hexnine / laravel-request-context

1. Go to this page and download the library: Download shahal-hexnine/laravel-request-context library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

shahal-hexnine / laravel-request-context example snippets


$currentTeam = Request::context()->get('currentTeam');

Request::context()->put('currentTeam', Request::user()->teams->first());

$currentTeam = Request::context()->get('currentTeam');


Request::context()->put('currentTeamTaskCount', Request::user()->teams->first()->tasks->count());

$count = Request::context()->get('currentTeamTaskCount');
// int(10)


Request::context()->put('teamOwner', Request::user()->teams->first()->owner->name);

$owner = Request::context()->get('teamOwner');
// string(14) "Martha Stewart"


Request::context()->put('teamProperties', Request::user()->teams->first()->properties);

$properties = Request::context()->get('teamProperties');
// array(1) {
//   'foo' =>
//   string(3) "bar"
// }


Request::context()->put([
    'currentTeamTaskCount' => Request::user()->teams->first()->tasks->count(),
    'teamOwner' => Request::user()->teams->first()->owner->name
]);

$count = Request::context()->get('currentTeamTaskCount');
// int(10)

$owner = Request::context()->get('teamOwner');
// string(14) "Martha Stewart"

// store it
Context::put('currentTeam', Request::user()->teams->first());

// get it
$currentTeam = Context::get('currentTeam');

// store it
context('currentTeam', request()->user()->teams->first());

// get it
$currentTeam = context('currentTeam');



namespace App\Http\Middleware;

use Closure;

class StoreTeamRequestContext
{
    public function handle($request, Closure $next)
    {
        $request->context()->put('currentTeam', $request->user()->teams()->first());
        return $next($request);
    }
}