试图从另一个控制器Laravel 5.3获取非对象的属性

时间:2017-01-27 12:55:01

标签: php oop laravel-5

我不认为这是一个典型的非对象错误,但希望有比我更多知识的人能够弄明白。

我有以下控制器:

class MangopayController extends Controller
{

    /**
     * @var \MangoPay\MangoPayApi
     */
    private $mangopay;

    public function __construct(MangoPayApi $mangopay)
    {
        $this->mangopay = $mangopay;
    }

    /**
     *  Add a user to mangopay. This should be done for every new user
     *
     * @return True if successful
     */

    public function addUserToMangopay($user_id)
    {
        // get the admin user object
        $user = User::where('id', $user_id)->first();
        // set fields for mangopay
        $newMangopayUser = new UserNatural();
        $newMangopayUser->Tag = $user->role;
        $newMangopayUser->FirstName = $user->first_name;
        $newMangopayUser->LastName = $user->last_name;
        $newMangopayUser->Birthday = $user->birthday;
        $newMangopayUser->Nationality = $user->nationality;
        $newMangopayUser->CountryOfResidence = $user->country_of_residence;
        $newMangopayUser->Email = $user->email;
        // add user to mango pay, should return user object if successful
        $result = $this->mangopay->Users->Create($newMangopayUser); // <-- this line is causing issues
        if (is_object($result)) {
            $user->mangopay_id = $result->Id;
            $user->save();

            return true;
        }
    }
}

如果我使用这样的路线运行它,上面的工作正常:

Route::get('/add-user-to-mangopay/{user_id}', [ 'as' => 'user_id', 'uses' => 'MangopayController@addUserToMangopay']);

但是,如果我尝试从我的RegisterController运行它,看起来像这样:

class RegisterController extends MangopayController
{
    /**
     * Handle a registration request for the application.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function register(Request $request)
    {
        $this->validator($request->all())->validate();

        event(new Registered($user = $this->create($request->all())));

        $this->guard()->login($user);

        $result = $this->addUserToMangopay($user->id);

        if ($this->addUserToMangopay($user->id)) {
            return $this->registered($request, $user)
                ?: redirect($this->redirectPath());
        } else {
            return $this->registered($request, $user) ?: redirect($this->redirectPath()->with('alert-danger', 'There was a problem creating your account'));
        }
    }
}

然后我得到错误&#34;试图获得非对象的属性&#34;来自MangopayController系列:

    $result = $this->mangopay->Users->Create($newMangopayUser);

我尝试了各种代码迭代但没有成功。

真正令人困惑的是,我已经检查了错误指向带有dd()的$ newMangopayUser的对象;并且据我所知它是一个对象,所以它应该工作......!

DD($ newMangopayUser);

UserNatural {#227 ▼
+FirstName: "10"
+LastName: "10"
+Address: null
+Birthday: 1483228800
+Nationality: "GB"
+CountryOfResidence: "GB"
+Occupation: null
+IncomeRange: null
+ProofOfIdentity: null
+ProofOfAddress: null
+PersonType: "NATURAL"
+Email: "ten@e.co.uk"
+KYCLevel: null
+Id: null
+Tag: "customer"
+CreationDate: null
}

任何人都可以提供帮助吗?感谢

1 个答案:

答案 0 :(得分:1)

这是因为您的父构造函数MangopayController未被调用implicitly(这是PHP的默认行为)。这意味着$mangopay不会被注入,因此它将是null

这意味着您在$this->mangopay->Users->Create($newMangopayUser);表达式->Users中正在访问null

所以$newMangoPayUser实际上不是null

尝试按以下方式修改RegisterController

class RegisterController extends MangopayController
{
  ...
  public function __construct(MangoPayApi $mangopay)
  {
    // call parent constructor explicitly
    parent::__construct($mangopay);
    ...
  }
...
}

此外,在这部分代码中,您要调用addUserToMangopay()两次,而不是检查$result(这是不必要的,但不是很重要)。

 $result = $this->addUserToMangopay($user->id);

        if ($this->addUserToMangopay($user->id)) {
         ...
        }
 ...
 }