[英]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
Connection.php 319行中的PDOException:SQLSTATE [42S02]:未找到基表或视图:1146表'app_db.user_id'不存在
Without validation my update works on the user_details table. 未经验证,我的更新将在user_details表上运行。
UserDetailsController.php 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 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);
}
}
Your issue lies with your validation of the user email 您的问题在于您对用户电子邮件的验证
unique:user_id
-> unique:user_details, user_id
should be the proper rule format unique:user_id
unique:user_details, user_id
> unique:user_details, user_id
应该是正确的规则格式
full rule will read: 'email' => 'email|unique:user_details,user_id|required'
完整规则如下:
'email' => 'email|unique:user_details,user_id|required'
Your original validation rule is trying to query the user_id
table, which doesn't exist. 您的原始验证规则正在尝试查询不存在的
user_id
表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.