skip to Main Content

I create a new user by fill in all the fields, then when i submit all the new data for user are registred with the correct data in DB but i’m not redirect to index page;I am getting this error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘name’ in ‘where clause’ (SQL: select * from roles where name = user limit 1)

this is my user model:

<?php

namespace App;

use IlluminateNotificationsNotifiable;
use IlluminateFoundationAuthUser 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',
    ];
}

this is my role model:

<?php

namespace App;

use IlluminateDatabaseEloquentModel;

class Roles extends Model
{
    //


     protected $fillable = [
        'libelle'
    ];
}

this is my user Controller:

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;
use IlluminateSupportFacadesHash;


use Auth;
use AppUser;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return IlluminateHttpResponse
     */

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

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



    /**
     * Show the form for creating a new resource.
     *
     * @return IlluminateHttpResponse
     */

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  IlluminateHttpRequest  $request
     * @return IlluminateHttpResponse
     */


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

    }

    /**

this is my role Controller:

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;

use AppRoles;

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

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


}

this is my route in 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]: Column not found: 1054 Unknown column ‘name’ in ‘where clause’ (SQL: select * from roles where name = user limit 1)

2

Answers


  1. Your roles table doesn’t have name column.

    Try following and see what you get. Open tinker shell:

    php artisan tinker
    

    If below code returns same error. Check your migrations. It means name column is missing.

    DB::table('roles')->where('name', 1)->get();
    
    Login or Signup to reply.
  2. As I can see below code on User model

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

    It seems you’ve added the name column in users table,

    you should add name column on roles table as well by following below steps

    STEPS TO FOLLOW:

    1.Run the command :
    php artisan make:migration alter_table_roles_add_name_column
    //This will create a new migration file

    2.Add the below methods on the new migration file

    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.Finally run the command : php artisan migrate

    I hope this will solve your problem

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search