2016-12-19 8 views
1

私は基本的にLaravelの新機能です。私はこれを続ける"配列のメンバ関数roles()を呼び出す"エラー。メンバーへの呼び出しroles()on array(Laravel 5.3)

私は多くの関係を持つデータベースにユーザーの役割を追加しようとしています。私はどこでもこの答えを探しましたが、結果は私の関数にreturn文を追加する必要があると言いました。 しかし私はすでに返信文を持っていますが、それでも動作しません。

これは私のコードです。

User.phpモデル

<?php 

class User extends Model implements Authenticatable 
{ 

    use \Illuminate\Auth\Authenticatable; 

    protected $primaryKey = 'user_id';  
    protected $fillable = [ 
     'user_fname', 'user_mname', 'user_lname', 'user_ptitle', 'user_sex', 'user_civilstatus', 'user_nationality', 'user_bday', 'user_bplace', 'user_address', 'user_mobile', 'user_landline' 
    ]; 

    protected $hidden = [ 
     'password', 'remember_token', 
    ]; 

    public function login(){ 
     return $this->hasOne('App\Login', 'user_id'); 
    } 

    public function registercoop(){ 
     return $this->hasOne('App\CoopDetails', 'coop_id'); 
    } 

    public function listcomaker(){ 
     return $this->hasMany('App\LoanCoMaker', 'user_id'); 
    } 

    public function roles(){ 
     return $this->hasMany('App\Role', 'role_users', 'user_id', 'role_id'); 
    } 


} 



Role.phpモデル

<?php 

class Role extends Model 
{ 

    public function users(){ 
     return $this->belongsToMany('App\User','role_users', 'role_id', 'user_id'); 
    } 

} 


データベースへの保存は、私は実際にそれが最初の時間を仕事作った

 $user=[ 
     'user_email' => $email, 
     'password' => $password 
    ]; 

    $count = DB::table('users') 
      ->where('created_at', '>=', 'CURRENT_DATE') 
      ->count(); //Count entries for that day. 


    $year = Carbon::now()->year; 
    $month = Carbon::now()->month; 


    if ($count<100 && $count>10){ 
     $count="0".$count; 
     $user_id = $year.$month.$count; 
    } 
    else if ($count<10){ 
     $count="00".$count; 
     $user_id = $year.$month.$count; 
    } 
    else{ 
     $user_id = $year.$month.$count; 
    } 

    $user->roles()->attach($user_superadmin); 



P.Sをたまたま私のAccountController.phpから
行。私は古いモデルのUserType.phpRole.phpに変更し、マイグレーション、シーダー、コントローラー、モデルなどを更新し、正常に動作しなくなりました。

私が間違っていることを指摘してくれる人がいますか?最初にユーザーを作成する必要があり

+0

Role.phpとUser.php全体を貼り付けることはできますか? –

+0

'attach'はidの代わりにobjectの代わりにidの配列を取る! –

+0

@RobinDirksen写真を添付し​​ました。見てください。 – Eggnog654

答えて

3

が、これは以下を介して行うことができます。

$user_data = [ 
    'user_email' => $email, 
    'password' => $password 
]; 

$user = new User(); 
$user->email = $user_data['user_email']; 
$user->password = encrypt($user_data['password']); 

$user->save(); //save the user to the database 
//$user now contains the user_id (if its successfully created) 

$count = DB::table('users') 
     ->where('created_at', '>=', 'CURRENT_DATE') 
     ->count(); //Count entries for that day. 


$year = Carbon::now()->year; 
$month = Carbon::now()->month; 


if ($count<100 && $count>10){ 
    $count="0".$count; 
    $user_id = $year.$month.$count; 
} 
else if ($count<10){ 
    $count="00".$count; 
    $user_id = $year.$month.$count; 
} 
else{ 
    $user_id = $year.$month.$count; 
} 

$user->roles()->attach($user_superadmin); 

は、この作品を願って!

+0

奇妙な、私はすでにその行を持っています。代わりに新しい変数を作りました。 :) – Eggnog654

+0

この時点で何が問題になっていますか? @ Eggnog654 –

+0

これで、 'roles()'関数が実行されているときに値を持たない 'user_id'に問題があります。 – Eggnog654

2

ロールにピボットテーブルを使用しているようですか?その場合、あなたの代わりにhasMany()

public function roles(){ 
    return $this->belongsToMany('App\Role', 'role_users', 'user_id', 'role_id'); 
} 

Laravel Docs Here

を参照してくださいあなたのUserクラスにbelongsToMany()関係を望ん次に、あなたはその関係にアクセスできるようにするにはApp\Userのインスタンスを作成する必要があります。あなたのコードは['user_email' => $email, 'password' => $password]の配列の関数を呼び出しています。最初にユーザーを作成するか取得する必要がありますが、そのユーザー・モデルである必要があります。

// Create User 
$user = new User(); 
$user->user_email = $email; 
$user->password = encrypt($password); 
$user->save(); 
// OR retrieve user 
$user = User::where('your_column', $your_column_value)->get(); 

$count = User::where('created_at', '>=', 'CURRENT_DATE')->count(); //Count entries for that day. 

$year = Carbon::now()->year; 
$month = Carbon::now()->month; 


if ($count<100 && $count>10){ 
    $count="0".$count; 
    $user_id = $year.$month.$count; 
} 
else if ($count<10){ 
    $count="00".$count; 
    $user_id = $year.$month.$count; 
} 
else{ 
    $user_id = $year.$month.$count; 
} 

$user->roles()->attach($user_superadmin); 
+0

私はその行を 'belongsToMany'に変更しました。私はそれを動作させましたが、 '$ roleuser-> roles() - > attach($ user_superadmin);'が実行されると、私の 'user_id'は** null **です。 '$ role-> attach($ user_superadmin);'は$ user-> roles() - > attach($ user_superadmin);の編集版です ' – Eggnog654

+0

あなたはそれをユーザーに保存する必要がありますまず。 '$ user-> user_id = $ user_id; $ user-> save(); ' –

+0

忍耐してくれてありがとうが、今は本当に失われている。私は 'user_id'を既に保存しました: ' DB :: table( 'users') - >ここで( "user_email"、$ data ['email']) - > update(['user_id' => $ user_id]); ' これは保存されていますが、エラーは解決しません。 – Eggnog654

関連する問題