我如何在我的自定义类和提供程序中获取身份验证用户或任何会话?

问题描述 投票:0回答:1

我必须获得用户选择的公司,但无法在我的类和提供程序启动功能中获取用户数据。

用户可以拥有多个公司,因此用户必须选择一家公司进行某些操作。但是正如我所说,我无法获得用户选择的公司。

喜欢这个:

 public function boot()
    {

        $user = Auth::user();

        dd( $user ); // return null;

        $bid = new Bid();
        $show = $bid->check();
        Blade::directive('bid',function() use($show){
            return "<?php if( $show ) { ?>";
        });

        Blade::directive('endbid',function(){
            return '<?php } ?>';
        });
    }

我的其他班级:

<?php

namespace App\Services\Buying\Package;

use App\Services\Buying\Package\PackageInterface;
use App\Models\Company\Company;
use Illuminate\Support\Facades\Session;
use Illuminate\Http\Request;
use App\User;

class PackageBuying extends PackageQuery implements PackageInterface
{
    private $company_id;

    public function __construct()
    {
        $user = Auth::user();

        dd( $user ); // return null

        $this->setCompanyId($this->company_id);
    }

    public function setSession( Request $request )
    {
        $request->session()->get('selected-company');
    }

    public function company()
    {
        return $this->company_id;
    }

    public function package()
    {
        if ( $this->check() ) {
            return $this->getPackage($this->company())->first();
        }

        return [];
    }

    public function features()
    {
        return (object)json_decode( $this->package()->features );
    }

    public function importantFeatures()
    {
        return (object)json_decode( $this->package()->important_features );
    }

    public function active()
    {
        return (bool) $this->getPackage()->firstOrFail()->active;
    }
}

实际上,如果我在提供程序启动功能中获得了用户数据,则可以将数据发送给我的班级。

您能帮我吗?

提前感谢。

php laravel user-controls provider construct
1个回答
0
投票

像这样将构造函数内部的代码放入calMethod函数中>

public function callAction( $method, $parameters ) {
        $user = Auth::user();
        dd( $user ); // return null
        $this->setCompanyId($this->company_id);
        return parent::callAction( $method, $parameters );
    }
© www.soinside.com 2019 - 2024. All rights reserved.