SQLSTATE [42S22]:找不到列:1054“ where子句”中的未知列“ name”(SQL:从*“ roles”中选择*其中“ name” =用户限制1)

时间:2019-04-22 23:30:15

标签: laravel phpmyadmin

我通过填写所有字段来创建新用户,然后在我为用户提交所有新数据时,在DB中使用正确的数据注册了该数据,但是我没有重定向到索引页;我收到此错误:< / p>

SQLSTATE [42S22]:找不到列:1054'where子句'中的未知列'name'(SQL:从roles中选择*,其中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:从roles中选择*,其中name =用户限制1)

2 个答案:

答案 0 :(得分:0)

您的roles表中没有name列。

尝试以下操作,看看会得到什么。打开修补匠壳:

php artisan tinker

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

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

答案 1 :(得分:0)

正如我在User模型上看到的以下代码

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

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

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

要关注的步骤:

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

我希望这能解决您的问题