【问题标题】:SQLSTATE[42S22]: Column not found: 1054 Unknown column 'name' in 'where clause' (SQL: select * from `roles` where `name` = user limit 1)SQLSTATE [42S22]:未找到列:1054 'where 子句'中的未知列 'name'(SQL:select * from `roles` where `name` = user limit 1)
【发布时间】:2019-09-12 02:47:03
【问题描述】:

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

SQLSTATE[42S22]:未找到列:1054 'where 子句'中的未知列 'name'(SQL:select * from roles where name = 用户限制 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',
    ];
}

这是我的榜样:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Roles extends Model
{
    //


     protected $fillable = [
        'libelle'
    ];
}

这是我的用户控制器:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;


use Auth;
use App\User;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

public function __construct()
{
    $this->middleware('auth');
}

    public function getRowAttributes()
    {
        return view('manage_users.index');
    }



    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */

    //ouvrir le formulaire
    public function create()
    {
        return view('manage_users/create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */


    //save data 
    public function store(Request $request)
    {
        $this->validate($request,[

              'name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255',
            'user_type' => 'required',
            'password' => 'required|string|min:6|confirmed'
        ]);

        $user=new User([
              'name' => $request->get('name'),
             'email' => $request->get('email'),
             'user_type'  => $request->get('user_type'),
             'password' => bcrypt($request->get('password'))

        ]);
        $user->save();
        return redirect('manage_users/index')->with('success','Data Added');

    }

    /**

这是我的角色控制器:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Roles;

class RolesController extends Controller
{
    //
public function __construct()
{
    $this->middleware('auth');
}
      public function index()
    {
        $roles = Roles::all();

        return view('manage_users.create', ['roles' => $roles]);
    }


}

这是我在 web.php 中的路线


Route::get('manage_users/index', 'UserController@getRowAttributes')->name('index');


//Route::get('manage_users/column_search', 'UserController@getColumnSearch')->name('column_search');

//Route::get('manage_users/index','UserController@index');

//Route::resource('manage_users','UserController');

Route::get('manage_users/create','UserController@create');
Route::get('manage_users/create','RolesController@index');
Route::post('manage_users/create','UserController@store');
//Route::get('manage_users/index','UserController@index');

SQLSTATE[42S22]:未找到列:1054 'where 子句'中的未知列 'name'(SQL:select * from roles where name = 用户限制 1)

【问题讨论】:

  • 你的名字在用户表而不是角色表下,同时你的查询正在寻找角色表的列名?
  • 分享您的索引视图代码,看起来您获取的数据有误

标签: laravel phpmyadmin


【解决方案1】:

我可以在User 模型上看到下面的代码

protected $fillable = [
    'name', 'email','user_type', 'password'
];

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

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

要遵循的步骤:

1.运行命令: php artisan make:migration alter_table_roles_add_name_column //这将创建一个新的迁移文件

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

希望能解决你的问题

【讨论】:

  • 谢谢大哥按照你的步骤,问题解决了
  • @OussamaAmmyDriss 不客气,很高兴您的问题得到解决,请为我的答案投票
【解决方案2】:

您的roles 表没有name 列。

尝试关注,看看你会得到什么。打开 tinker shell:

php artisan tinker

如果下面的代码返回相同的错误。检查您的迁移。这意味着缺少name 列。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-03
    • 1970-01-01
    • 2020-03-01
    • 2020-06-08
    • 1970-01-01
    • 2021-03-29
    • 1970-01-01
    • 2023-04-07
    相关资源
    最近更新 更多