【问题标题】:Call to a member function roles() on array (Laravel 5.3)调用数组上的成员函数角色()(Laravel 5.3)
【发布时间】:2017-05-04 20:57:31
【问题描述】:

我基本上是 Laravel 的新手。我不断收到此“调用数组上的成员函数角色()”错误。

我正在尝试将用户角色添加到具有多对多关系的数据库中。我到处寻找答案,结果表明我只需要在我的函数中添加一个 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,更新了我的迁移、播种器、控制器、模型等,然后它就停止了正常工作。

谁能帮我指出我做错了什么?

【问题讨论】:

  • 你能把整个Role.php和User.php贴上来吗?
  • attach 接受 id 或 id 数组而不是对象!
  • @RobinDirksen 我已附上照片。请看一看。
  • @Eggnog654 错误的意思正是它所说的,$user 是一个 array 你在 AccountController.php 中声明的
  • 您没有在AccountController.php 中创建new User$user 是一个数组,因此当您调用$user-&gt;roles()-&gt;attach($user_superadmin); 时会报错

标签: php laravel migration roles


【解决方案1】:

您似乎正在为角色使用数据透视表?在这种情况下,您需要在 User 类上使用 belongsToMany() 关系,而不是 hasMany()

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

Laravel Docs Here

然后您需要创建App\User 的实例才能访问该关系。您的代码正在调用['user_email' =&gt; $email, 'password' =&gt; $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);

【讨论】:

  • 我已将该行更改为belongsToMany。我让它开始工作,但是当$roleuser-&gt;roles()-&gt;attach($user_superadmin); 被执行时,不知何故我的user_idnull$roleuser-&gt;roles()-&gt;attach($user_superadmin);$user-&gt;roles()-&gt;attach($user_superadmin); 的编辑版本
  • 你需要先保存给用户。 $user-&gt;user_id = $user_id; $user-&gt;save();
  • 感谢您的耐心等待,但我现在真的迷路了。我已经保存了 user_id 已经在使用这个:DB::table('users') -&gt;where("user_email", $data['email']) -&gt;update(['user_id' =&gt; $user_id]); 它正在保存但错误仍然存​​在。
  • 我在我的新问题中找到了我的问题的答案。 stackoverflow.com/questions/41245657/…
【解决方案2】:

您需要先创建用户,这可以通过以下方式完成:

$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_id 在执行roles() 函数时没有值的问题。
猜你喜欢
  • 2017-04-08
  • 2017-03-10
  • 1970-01-01
  • 1970-01-01
  • 2017-10-03
  • 2022-01-22
  • 2017-05-22
  • 2018-06-28
  • 1970-01-01
相关资源
最近更新 更多