Laravel SQLSTATE[42S22]:未找到列:在where子句中找到1054未知列“名称”。(SQL:select*from`roles`where`name`=用户限制1)

Laravel SQLSTATE[42S22]:未找到列:在where子句中找到1054未知列“名称”。(SQL:select*from`roles`where`name`=用户限制1),laravel,phpmyadmin,Laravel,Phpmyadmin,我通过填写所有字段创建一个新用户,然后当我提交用户的所有新数据时,会在数据库中用正确的数据注册,但我不会重定向到索引页;我得到这个错误: SQLSTATE[42S22]:未找到列:1054“where子句”中的未知列“name”SQL:select*from roles where name=user limit 1 这是我的用户模型: <?php namespace App; use Illuminate\Notifications\Notifiable; use Illuminat

我通过填写所有字段创建一个新用户,然后当我提交用户的所有新数据时,会在数据库中用正确的数据注册,但我不会重定向到索引页;我得到这个错误:

SQLSTATE[42S22]:未找到列:1054“where子句”中的未知列“name”SQL:select*from roles where name=user limit 1

这是我的用户模型:

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;



    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email','user_type', 'password'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

SQLSTATE[42S22]:未找到列:1054“where子句”中的未知列“名称”SQL:select*from roles where name=user limit 1

您的角色表没有名称列

试试下面的内容,看看你得到了什么。打开修补程序外壳:

php artisan tinker
如果下面的代码返回相同的错误。检查您的迁移。这意味着缺少“名称”列

\DB::table('roles')->where('name', 1)->get();

正如我在下面看到的关于用户模型的代码

似乎您已经在users表中添加了name列

您还应该按照以下步骤在roles表中添加name列

应采取的步骤:

1.运行以下命令: php artisan make:迁移更改表角色添加名称列 //这将创建一个新的迁移文件

public function up()
{
    Schema::table('roles', function (Blueprint $table) {
        $table->string('name')
        ->after('id'); //optional
    });
}


public function down()
{
    Schema::table('roles', function (Blueprint $table) {
        $table->dropColumn('name');
    });
}
2.在新迁移文件上添加以下方法

public function up()
{
    Schema::table('roles', function (Blueprint $table) {
        $table->string('name')
        ->after('id'); //optional
    });
}


public function down()
{
    Schema::table('roles', function (Blueprint $table) {
        $table->dropColumn('name');
    });
}
3.最后运行命令:php artisan migrate


我希望这能解决您的问题

您的名字在users表下而不是role表下,同时您的查询正在查找列名为的role表?共享您的索引视图代码,看起来您的数据取错了感谢您的兄弟,按照您的步骤,问题resolved@OussamaAmmyDriss不客气,我很高兴你的问题解决了,请把我的答案加起来
php artisan tinker
\DB::table('roles')->where('name', 1)->get();
protected $fillable = [
    'name', 'email','user_type', 'password'
];
public function up()
{
    Schema::table('roles', function (Blueprint $table) {
        $table->string('name')
        ->after('id'); //optional
    });
}


public function down()
{
    Schema::table('roles', function (Blueprint $table) {
        $table->dropColumn('name');
    });
}