Laravel:Connection.php第319行中的PDOException:SQLSTATE [42S02]

时间:2017-02-28 15:10:46

标签: php laravel pdo

在数据库更新期间,我尝试验证输入,但每次都会收到此错误消息(单击“提交”按钮):

  

Connection.php第319行中的PDOException:SQLSTATE [42S02]:基表   或查看未找到:1146表' app_db.user_id'不存在

如果没有验证,我的更新适用于 user_details 表。

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

更新 Database structure

1 个答案:

答案 0 :(得分:0)

您的问题在于验证用户电子邮件

unique:user_id - &gt; unique:user_details, user_id应该是正确的规则格式

完整规则将为:'email' => 'email|unique:user_details,user_id|required'

您的原始验证规则是尝试查询不存在的user_id表。