mstdmstd
4 months ago

Validation checking on unique raise error with empty table name

Posted 4 months ago by mstdmstd

In laravel 6 app I have a request on store, which gets rules from the model app/Http/Requests/EventRequest.php defined:

    <?php
    
    namespace App\Http\Requests;
    
    use Illuminate\Foundation\Http\FormRequest;
    use App\Event;
    class EventRequest extends FormRequest
    {
        public function authorize()
        {
            return true;
        }
    
        public function rules()
        {
            $request= Request();
            return Event::getValidationRulesArray( $request->get('id') );
        }
    }

and I got error :

    LSTATE[42S02]: Base table or view not found: 1146 Table 'CTasksRestAPI.tsk_' doesn't exist (SQL: select count(*) as aggregate from `tsk_` where `id` = 6)",
        "exception": "Illuminate\Database\QueryException",
        "file": "/mnt/_work_sdb8/wwwroot/lar/CTasksRestAPI/vendor/laravel/framework/src/Illuminate/Database/Connection.php",
        "line": 669,
        "trace": [

and where

CTasksRestAPI - my db name
tsk_ - prefix of db

And my model :

    <?php
    
    namespace App;
    
    ...    
    class Event extends Model
    {
    
        protected $table = 'events';
        protected $primaryKey = 'id';
        public $timestamps = false;
        private static $eventAccessLabelValueArray = Array('P' => 'Private', 'U' => 'Public');
    
    
        protected $fillable = [
            'name', 'access', 'at_time', 'duration', 'task_id', 'description', 'description'
        ];
    
    
        ...    
    
        public static function getEventAccessValueArray($key_return= true, $statusLimitArray=[]) : array
        {
            $resArray = [];
            foreach (self::$eventAccessLabelValueArray as $key => $value) {
                if ( !empty($statusLimitArray) ) {
                    if ( !in_array($key, $statusLimitArray) ) continue;
                }
                if ($key_return) {
                    $resArray = Arr::add([$resArray], 'code', $key, 'label', $value);
    
                } else {
                    $resArray[$key] = $value;
                }
            }
            return $resArray;
        }
    
    
        public static function getValidationRulesArray( $event_id ) : array
        {
            \Log::info('with(new Event)->getTable() ::');
            \Log::info(json_encode(  with(new Event)->getTable()  ));// I THIS VALUE IN LOG FILE AND SEE VALUE : "events"
            $validationRulesArray = [
                'name' => [
                    'required',
                    'string',
                    'max:255',
                    Rule::unique(with(new Event)->getTable())->ignore($event_id),
                ],
                'access'           => 'required|in:'.MyFuncsClass::getValueLabelKeys(  Event::getEventAccessValueArray(false) ),
                'at_time'          => 'required',
                'duration'         => 'required|integer|min:1|max:6000',
                'task_id'          => 'nullable|exists:'.( with(new Task)->table ).',id',
                'description'      => 'required',
            ];
    
            return $validationRulesArray;
        }
    
    }

I suppose that checking on unique could raise error, but why as I have $table field of a model filled and this 'events' must be used in select statement:

  protected $table = 'events';

?

Thanks!

Please sign in or create an account to participate in this conversation.