Ad

Laravel Using Form Request Get Argument 1 Passed Error, Must Be Of The Type Array, Object Given

- 1 answer

i'm trying to use Form Request for register users in my application but i get this error:

Argument 1 passed to Illuminate\Database\Eloquent\Model::create() 
must be of the type array, object given, 
called in /var/www/app/app/Http/Controllers/RegisterNewUsersPgController.php 
on line 28 and defined

My Route:

Route::post('registerRealUserAccount'  ,'RegisterNewUsersPgCo[email protected]');

Controller:

class RegisterNewUsersPgController extends Controller
{
    /*
     * RegisterRealUserAccount
     *
     * @param StoreRegisterRealUserAccount $request
     */
    public function registerRealUserAccount(StoreRegisterRealUserAccount $request)
    {
        AccountRealUsers::create($request);
    }
}

StoreRegisterRealUserAccount class:

class StoreRegisterRealUserAccount 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 [
        ];
    }
}

AccountRealUsers Model:

class AccountRealUsers extends Model
{
    protected $table = 'account_reaL_users';
    protected $fillable = [
                            'acceptor_farsi_name',
                            'acceptor_english_name',
                            'display_name',
                            'username',
                            'password',
                            'service_type',
    ];

    protected $guarded = ['users'];

    public function setPasswordAttribute($value)
    {
        $this->attributes['password'] = Hash::make($value);
    }
}
Ad

Answer

The create() function requires an array as a parameter, not an object ($request). The $request->all() method should return an array of all your request parameters.

class RegisterNewUsersPgController extends Controller
{
    /*
     * RegisterRealUserAccount
     *
     * @param StoreRegisterRealUserAccount $request
     */
    public function registerRealUserAccount(StoreRegisterRealUserAccount $request)
    {
        AccountRealUsers::create($request->all());
    }
}

You might want to consider specifying the actual parameters you want to store because an extra GET/POST parameter could break your code.

If you want to specify the parameters you should try the following...

class RegisterNewUsersPgController extends Controller
{
    /*
     * RegisterRealUserAccount
     *
     * @param StoreRegisterRealUserAccount $request
     */
    public function registerRealUserAccount(StoreRegisterRealUserAccount $request)
    {
        AccountRealUsers::create($request->only([
            'acceptor_farsi_name',
            'acceptor_english_name',
            'display_name',
            'username',
            'password',
            'service_type',
        ]));
    }
}

The above will get only the specified request parameters from the $request object and return them as an array.

Ad
source: stackoverflow.com
Ad