ユーザーテーブルのレコードの別のテーブルにレコードを取得しようとしています。このため私はラーベル関係を使用しています。私は1対1の関係を使用しています。Laravel:1対1の関係は機能していませんか?
私は正確に何をしたいですか?私が取得したいsrp_user_statistics
:ID表1名前:ユーザー
表2主キー:user_idを
表2名
私は2つのテーブル、
表1主キーを持っていますテーブル内の唯一のレコードであるsrp_user_statistics
srp_user_statistics
カラムuser_id
等価テーブルusers
カラムid
MySQLのクエリのように:
SELECT * FROM `srp_user_statistics` WHERE `user_id` = '" . Auth::user()->id . "'
私は、可能な最善の方法で説明しようとしていますし、うまくいけば私は以前、人々は私が何をしようとしています何を理解し、その与えられていないしているので、間違った答え。
このような状況で、私はsrp_user_statistics.user_id = users.id値
私はlaravel 5.4を使用srp_user_statisticsのレコードを取得したいので、私は、TABLE2.user_id = TABLE1.id TABLE2にレコードを取得したいですここでは私のコードを使用しています。
私はusers
テーブルに対してPlayer.phpクラスを使用します。
namespace App\Database\Frontend\User;
use Eloquent;
class Roleplay extends Eloquent
{
protected $primaryKey = 'user_id';
protected $table = 'srp_user_statistics';
public $timestamps = true;
protected $fillable = [];
public function user()
{
return $this->belongsTo('App\Database\Frontend\User\Player', 'user_id', 'id');
}
public function government_role()
{
return $this->belongsTo('App\Database\Frontend\Roleplay\GovernmentRole', 'government_id');
}
}
srp_user_statistics
テーブル用Roleplay.phpクラスを使用
namespace App\Database\Frontend\User; use Hash; use Eloquent; use \Illuminate\Auth\Authenticatable; use \Illuminate\Contracts\Auth\Authenticatable as Authentication; class Player extends Eloquent implements Authentication { use Authenticatable; protected $primaryKey = 'id'; protected $table = 'users'; public $timestamps = false; protected $fillable = []; public function setPasswordAttribute($value) { $this->attributes['password'] = Hash::make($value); } public function setUsernameAttribute($value) { return $this->attributes['username'] = $value; } public function roleplay() { return $this->hasOne('App\Database\Frontend\User\Roleplay', 'user_id'); } }
それは働いていますか?
私はこのコードは完璧に動作しますので、レコードがデータベース側に存在しているかなり確信している私は{{ Auth::user()->roleplay->my_column }}
をすることによって、テーブルsrp_user_statistics
の列をエコーしようとしましたが、それは
ErrorException in 9d107b63496020763dd451d002bc2c1a2b2459c0.php line 3:
Trying to get property of non-object (View: C:\cms_r\resources\views\frontend\home.blade.php)
私にこのエラーを送っ:
Roleplay::find(Auth::user()->id)
誰でも私にこれを手伝ってもらえますか?
[Laravel hasOneの関係空]の可能な重複(http://stackoverflow.com/questions/43037302/laravel-hasone-relationship-empty) – 4castle
あなたが二度基本的に同じ質問をしてきたようだが、ここで詳細を追加しました。詳細を追加するには、元の質問を編集してください。 – 4castle
この 'Roleplay :: find(Auth :: user() - id)'はあなたに 'Auth :: user()'に属する 'roleplay'を見つけられません。 'Auth :: user() 'と同じ' id'を持つ 'roleplay'が見つかりました。 – fubar