簡體   English   中英

調用數組上的成員函數role()(Laravel 5.3)

[英]Call to a member function roles() on array (Laravel 5.3)

我基本上是Laravel的新手。 我不斷收到此“在數組上調用成員函數role()”錯誤。

我試圖將用戶角色添加到我的數據庫中,該數據庫具有多對多關系。 我到處都在尋找答案,結果說我只需要在函數中添加return語句。 但是我已經有一個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');
    }

}



從我的AccountController.php行保存到數據庫的行

        $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);



PS我實際上是第一次使用它。 我只是將我的舊模型UserType.php重命名為Role.php ,更新了我的遷移,種子,控制器,模型等,然后它停止了正常工作。

有人可以幫我指出我在做什么錯嗎?

您需要首先創建用戶,這可以通過以下步驟完成:

$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);

希望這有效!

看起來您正在使用數據透視表作為角色? 在這種情況下,您希望在User類上擁有一個belongsToMany()關系,而不是hasMany()

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

在這里查看Laravel文檔

然后,您需要創建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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM