0
votes

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
The error message seems pretty clear and unrelated to your code. What does the database schema look like?jeroen
Sorry, forgot it. updated.danielori

1 Answers

0
votes

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.