[英]Call to a member function roles() on array (Laravel 5.3)
I'm basically new to Laravel. 我基本上是Laravel的新手。 I keep getting this "Call to member function roles() on array" error.
我不断收到此“在数组上调用成员函数role()”错误。
I'm trying to add a user's role into my database which has many to many relation. 我试图将用户角色添加到我的数据库中,该数据库具有多对多关系。 I've looked for an answer for this everywhere and the results said I just needed to add a return statement in my function.
我到处都在寻找答案,结果说我只需要在函数中添加return语句。 But I already have a return statement and it still doesn't work.
但是我已经有一个return语句,但是它仍然不起作用。
So here are my codes. 所以这是我的代码。
User.php model 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 model Role.php模型
<?php
class Role extends Model
{
public function users(){
return $this->belongsToMany('App\User','role_users', 'role_id', 'user_id');
}
}
Lines from my AccountController.php where the saving to database happens 从我的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 I actually have made it work the first time. PS我实际上是第一次使用它。 I just renamed my old model UserType.php into Role.php , updated my migrations, seeders, controllers, models, etc. and then it stopped working properly.
我只是将我的旧模型UserType.php重命名为Role.php ,更新了我的迁移,种子,控制器,模型等,然后它停止了正常工作。
Can someone help me point out what am I doing wrong? 有人可以帮我指出我在做什么错吗?
You need to first make the User, this can be done through the following: 您需要首先创建用户,这可以通过以下步骤完成:
$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);
Hope this works! 希望这有效!
Looks like you're using a pivot table for the roles? 看起来您正在使用数据透视表作为角色? In that case you want a
belongsToMany()
relationship on your User class instead of the hasMany()
在这种情况下,您希望在User类上拥有一个
belongsToMany()
关系,而不是hasMany()
public function roles(){
return $this->belongsToMany('App\Role', 'role_users', 'user_id', 'role_id');
}
See Laravel Docs Here 在这里查看Laravel文档
Then you need to create an instance of the App\\User
to be able to access that relationship. 然后,您需要创建
App\\User
的实例才能访问该关系。 Your code is calling a function on the array of ['user_email' => $email, 'password' => $password]
. 您的代码正在
['user_email' => $email, 'password' => $password]
数组上调用一个函数。 You have to create the user first or retrieve one but it needs to be that user model. 您必须先创建用户或检索一个用户,但是它必须是该用户模型。
// 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.