Laravel 5.2でこのコードを使用してUserettingsと共にユーザーを熱心にロードしようとしています:App\User::with('usersettings')->get()
、理由を理解しているようだ。これは与えられたエラーです。Laravelで1対1のリレーションをeager-loadできない
BadMethodCallException with message 'Call to undefined method Illuminate\Database\Query\Builder::usersettings()'
私はthe laravel docsを読んでいると私はLaracastsをたくさん見てきたし、これは前に働いていたので、私は、私は本当に小さな、おそらく何かを明らかに欠けているのアイデアを得ます。
User.php
<?php
namespace App;
/* User with fields: id, email */
class User extends Illuminate\Database\Eloquent\Model {
protected $table = 'users';
public function usersettings() {
return $this->hasOne("App\Usersettings", "userid");
}
}
Usersettings.php
<?php
namespace App;
/* Settings with fields: id, userid, textcolor */
class Usersettings extends Illuminate\Database\Eloquent\Model {
protected $table = 'usersettings';
public function user() {
return $this->belongsTo('App\User', 'userid');
}
}
//編集:私はすでにSを小文字に試してみました。このタイプミスは、それをSOにコピーすることに悩まされていたかもしれませんが、エラーはそこにありました。
//編集:
<?php
namespace App;
use App\UserSettings;
class User extends Illuminate\Database\Eloquent\Model {
protected $table = 'users';
public function settings() {
return $this->hasOne(UserSettings::class, "userid");
}
}
私はphp artisan tinker
>>>App\User::with('settings')->get()
を実行すると、予想通り、それは動作しますが、私はphp artisan tinker
>>>App\User::with('usersettings')->get()
を実行したときに
<?php
namespace App;
use App\UserSettings;
class User extends Illuminate\Database\Eloquent\Model {
protected $table = 'users';
public function usersettings() {
return $this->hasOne(UserSettings::class, "userid");
}
}
の下BadMethodCallException with message 'Call to undefined method Illuminate\Database\Query\Builder::usersettings()'
を与えます。私は
多分設定モデルのクラス名にタイプミスがあります。 'Usersettings'の代わりに' UserSettings'を試してみてください( 's 'を大文字にしてください)。また、 'with()'、 'with(UserSettings)'の中で名前を大文字にしてみるべきです。これが解決策だとは思わないでください。 –
'' App \ Usersettings ''を 'Usersettings :: class'に、' 'App \ User'''を' User :: class'にリネームしよう。 – KmasterYC