简体   繁体   中英

Laravel: PDOException in Connection.php line 319: SQLSTATE[42S02]

During database update, I try to validate my inputs, but I get this error message every time (clicked on submit button):

PDOException in Connection.php line 319: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'app_db.user_id' doesn't exist

Without validation my update works on the user_details table.

UserDetailsController.php

<?php

namespace App\Http\Controllers;

use Session;
use App\UserDetails;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;


class UserDetailsController extends Controller
{
     public function index()
    {
        $details = UserDetails::all()->where('user_id', \Auth::user()->id);
        return \View::make('pages.personal_datas')
        ->with('details', $details);
    }

     public function update()
    {

        $details = UserDetails::where('user_id', \Auth::user()->id)->first();
        $validator = UserDetails::validate(Input::all());
        if($validator->fails())
        {   
            $messages = $validator->messages();

            return redirect()->action('UserDetailsController@index')
            ->withErrors($validator);
        }
        else
        {
            $details->email = Input::get('email');
            $details->phone = Input::get('phone');         
            $details->country = Input::get('country');
            $details->city = Input::get('city');
            $details->address = Input::get('address');
            $details->save();

            Session::flash('message', 'Successfully updated!');

            return redirect()->action('UserDetailsController@index');
        }
    }
}

UserDetails.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Validator;

class UserDetails extends Model
{
    public $timestamps = false;
    protected $guarded = [];
    protected $primaryKey = 'user_id';
    protected $table = 'user_details';

    public static $rules = array(
            'email' => 'email|unique:user_id|required',
            'phone' => 'min:11|required',
            'country' => 'min:4|required',
            'city' => 'min:2|required',
            'address' => 'min:4|required',
    );

    public static function validate($data)
    {
            return Validator::make($data, static::$rules);
    }
}

UPDATE 数据库结构

Your issue lies with your validation of the user email

unique:user_id -> unique:user_details, user_id should be the proper rule format

full rule will read: 'email' => 'email|unique:user_details,user_id|required'

Your original validation rule is trying to query the user_id table, which doesn't exist.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM