SQLSTATE[42S02]: Base table or view not found: 114

2019-06-24 13:45发布

问题:

I am getting this error in Laravel 5:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'intern.users' doesn't exist (SQL: select * from users where username = admin limit 1)

config/database.php

        'mysql' => [
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => 'intern',
    'username'  => 'root',
    'password'  => '',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
    'strict'    => false,
],

The function admin is called for admin page, and database table is mentioned in it as admin because there are many tables in it.

    public function admin(Request $request){

           if($request->isMethod('get')){
           return \View::make('student.admin');
        } else
    {


        $check=0;
        $check=\DB::table('admin')->get();
        $username = Input::get('username');
        $password = Input::get('password');



     $data=array(
     'username'=>$request->get('username'),
     'password'=>$request->get('password')
    );

    if(\Auth::attempt($data))
    {
        return redirect::intended('student/index');
    }
    else
    {
        return redirect('student/admin');
    }


        }   



       }    

Form is here:

  <div id="pageContent"><br />
<div align="left" style="margin-left:24px;">
  <h2>Please Log In To Manage</h2>
  {!! Form::open(array('url' => '/admin')) !!}
  <input type="hidden" name="_token" value="{{ csrf_token() }}">


    User Name:<br />
      <input name="username" type="text" id="username" size="40" />
    <br /><br />
    Password:<br />
   <input name="password" type="password" id="password" size="40" />
   <br />
   <br />
   <br />

     <input type="submit" name="button" id="button" value="Log In" />


 {!! Form::close() !!}

回答1:

First you should hash and create the User details to make the coloumn ready for authentication.

Here i have given the steps to achieve it.

Step 1 : Get the Input

$UserData = Input::all();

Step 2 : Create the entry - Inserting into User Table

User::create($UserData);

Note :

You should have these following coloumns in your users table

  1. email,
  2. password
  3. created_at
  4. updated_at

Additional Setup :

Have this line in your User.php (Model)

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

Here's my tiny login code for you which would simple enough for you

Have a try over this if you wish

$email = $this->request->input('email');
$password = $this->request->input('password');
if (Auth::attempt(['email' => $email, 'password' => $password])) #If the Credentials are Right
{
 return redirect::intended('student/index'); #Your Success Page
}
else
{
 return redirect('student/admin'); #Your Failure Page
}

Recommendation :

I would also recommend to validate the user input before creating

Additional Note :

If you see your table and if you password is something like encrypted and it means that you're done ;)