调用数组上的成员函数role()(Laravel 5.3) [英] Call to a member function roles() on array (Laravel 5.3)

查看:119
本文介绍了调用数组上的成员函数role()(Laravel 5.3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上是Laravel的新手.我不断收到此在数组上调用成员函数role()" 错误.

I'm basically new to Laravel. I keep getting this "Call to member function roles() on array" error.

我试图将用户角色添加到具有多对多关系的数据库中.我到处都在寻找答案,结果说我只需要在函数中添加return语句. 但是我已经有一个return语句,但是它仍然不起作用.

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. But I already have a return statement and it still doesn't work.

这是我的代码.

User.php模型

So here are my codes.

User.php model

<?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中保存到数据库的行



Lines from my AccountController.php where the saving to database happens

        $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 ,更新了我的迁移,播种机,控制器,模型等,然后它停止了正常工作.

P.S I actually have made it work the first time. I just renamed my old model UserType.php into Role.php, updated my migrations, seeders, controllers, models, etc. and then it stopped working properly.

有人可以帮我指出我在做什么错吗?

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

希望这行得通!

这篇关于调用数组上的成员函数role()(Laravel 5.3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆