0

I'm a newbie in PHP, so now i need to validate model with static variable.

This is what i have

class Setting extends Model {

    protected $table = 'settings';

    public static $rules = [
        'skey' => 'required|unique:table,id,' . Route::input('settings')
    ];
}

It throws following error : syntax error, unexpected '.', expecting ']'

Ok, I understand that can not used in declaring variable.

Now, this is my question:

  • How can I done this with Illuminate\Http\Request, I dont want to create a new SettingRequest that can use easier.
  • I also dont want use in store or update method in controller. I want to use this way in both 2 method create/update.
  • In PHP, anyway to create setter or getter as C#.
trinvh
  • 1,500
  • 2
  • 11
  • 20

2 Answers2

1

You can't do it like this as Luis said.

I assumed that you're using L5. Better practise is using a Request class.

<?php 
    namespace App\Http\Requests;

    class SettingRequest extends Request
    {
        /**
         * Determine if the user is authorized to make this request.
         *
         * @return bool
         */
        public function authorize()
        {
            return true;
        }

        /**
         * Get the validation rules that apply to the request.
         *
         * @return array
         */
        public function rules()
        {
            return [
                'skey' => 'required|unique:table,id,' .$this->input('settings')
            ];
        }
    }

after that you can use the SettingRequest class as a method parameter in your controller like this:

public function update(SettingRequest $request) {}
public function create(SettingRequest $request) {}
Goktug Gumus
  • 129
  • 4
  • Hi I said in above post, assume if I dont want it, anyway to make getter or setter in php without create new Request class or use as a function ( getRules()) ? – trinvh Apr 12 '16 at 11:02
0

You can't do it in that context. Try to do it inside a method:

public static function getRules()
{
    return  [
            'skey' => 'required|unique:table,id,' . Route::input('settings')
    ];
}
Luis Montoya
  • 3,117
  • 1
  • 17
  • 26