dongyu5104 2017-02-28 15:10
浏览 125
已采纳

Laravel:Connection.php第319行中的PDOException: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 Database structure

  • 写回答

1条回答 默认 最新

  • dousi4472 2017-02-28 15:39
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况