0
votes

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

this is my role model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Roles extends Model
{
    //


     protected $fillable = [
        'libelle'
    ];
}

this is my user Controller:

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

    }

    /**

this is my role Controller:

<?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]);
    }


}

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
Your name is under users table not role table meanwhile your query is looking for role table's with column name?Lim Kean Phang
Share your index view code, it looks like your fetching wrong datalivreson ltc

2 Answers

1
votes

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

0
votes

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