调用数组 (Laravel 5.3) 上的成员函数 roles()

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

我基本上是 Laravel 的新手。我不断收到此 "Call to member function roles() on array" 错误。

我正在尝试将用户的角色添加到我的具有多对多关系的数据库中。我到处都在寻找这个问题的答案,结果说我只需要在我的函数中添加一个 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);



P.S 其实我第一次就成功了。我刚刚将我的旧模型 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);

希望这有效!

您似乎在为角色使用枢轴 table?在这种情况下,您希望在您的用户 class 上建立 belongsToMany() 关系,而不是 hasMany()

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

Laravel Docs Here

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