我不认为这是一个典型的非对象错误,但希望比我有更多知识的人能弄清楚它。
我有以下控制器:
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'));
}
}
}然后,我从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
}有人能帮上忙吗?谢谢
发布于 2017-01-27 21:44:00
这是因为您的父构造函数MangopayController没有被称为implicitly (这是PHP的默认行为)。这意味着$mangopay不会被注入,因此它将是null。
这意味着在expression $this->mangopay->Users->Create($newMangopayUser);中,您将访问null上的->Users。
所以它不是$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)) {
...
}
...
}https://stackoverflow.com/questions/41894460
复制相似问题