1

I need advice how to store an array to database. For example i have an input with name="user_phone[]" and i want to store to database the value of this input. I have a form like so, also there other inputs but i copy just one:

{!! Form::open([route('some.router')]) !!}

 <fieldset class="form-group">
   {{ Form::label(null, 'Phone') }}
     {{ Form::text('user_phone[]', null, ['class' => 'form-control'] ) }}
 </fieldset>

{!! Form::close() !!}

and the controller:

public function postAddOrder(Request $request)
    {
        $this->validate($request, [
            'receipt_date' => 'date|required',
            'return_date' => 'date|required',
            'user_name' => 'required',
            'user_phone' => 'required',
            'work_sum' => 'integer|required',
            'user_descr' => 'required',
            'foruser_descr' => 'required'
        ]);

        $user = new User;
        $user = $user->all()->find($request->input('user_name'));

        $order = new Order([
            'receipt_date' => $request->input('receipt_date'),
            'return_date' => $request->input('return_date'),
            'user_name' => $user->fio,
            'user_phone' => $request->input('user_phone'),
            'device' => $request->input('device'),
            'work_sum' => $request->input('work_sum'),
            'master_name' => $request->input('master_name'),
            'user_descr' => $request->input('user_descr'),
            'foruser_descr' => $request->input('foruser_descr'),
            'type' => $request->input('type'),
            'status' => $request->input('status'),
            'engineer' => $request->input('engineer'),
            'partner' => $request->input('partner'),
            'office' => $request->input('office')
        ]);

        $order->save();

        return redirect()->route('admin.orders.index');

    }

The problem is when i'm submitting the form and getting the error:

htmlentities() expects parameter 1 to be string, array given

Also i'm using casts to store an array to DB:

/**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
    protected $casts = [
        'user_phone' => 'array',
    ];

The values are storing correctly, the main problem is that the validate() method is not catching the errors. For example im not filling some data in inputs which are required. When instead of getting the error like something is required im getting error

htmlentities() expects parameter 1 to be string, array given

When im filling all input with data everything goes ok.

pwnz22
  • 469
  • 2
  • 9
  • 19

2 Answers2

1

I think the problem comes from your rule

'user_phone' => 'required

To validate array values you should use the array validation. (Link)

rewrite your rule like so

"user_phone.0" => "required"

this will ensure that at least one user_phone is provided.

In case you wanna validate phone format just go with:

"user_phone.*" => "{Insert validation here}"
  • `{{ Form::text('user_phone[]', null, ['class' => 'form-control'] ) }}` is it true to have an input like this?? with this brackets to store array of values? or maybe there is another way in laravel? – pwnz22 Sep 16 '16 at 13:55
  • No problem with that. Did you identify if the error comes at validation or during order creation ? – jeremy_nikolic Sep 16 '16 at 13:58
  • https://i.gyazo.com/d4a83e7d3b2f480339c97c0e0ca4bf90.png - maybe it helps. I dont really know. I think the errors comes before validation. Because im getting error right after the submitting form – pwnz22 Sep 16 '16 at 14:06
  • Also I just realised the section were you select the user should be something like that: $user = User::find($request->input('user_name')); – jeremy_nikolic Sep 16 '16 at 14:07
  • I'm wondering if the saving part is actually fine(check if you have new rows in DB), the error seems to happen when rendering your view "admin.orders.index". Check how do you use the user_phone field in the view – jeremy_nikolic Sep 16 '16 at 14:09
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/123539/discussion-between-hyti-and-pwnz22). – jeremy_nikolic Sep 16 '16 at 14:13
  • If im filling all the inputs in my form the data is storing and im getting the right view. All info displaying fine. But if im not filling even one input im getting this error – pwnz22 Sep 16 '16 at 14:13
0

Found the definition.

{!! Form::open([route('some.router')]) !!}

 <fieldset class="form-group">
   {{ Form::label(null, 'Phone') }}
     {{ Form::text('user_phone[0]', null, ['class' => 'form-control'] ) }}
 </fieldset>

{!! Form::close() !!}

We must pass the index in inputs. Like name="user_phone[0]" after that we are not getting the error:

htmlentities() expects parameter 1 to be string, array given

And validate() method catching the errors. It was the only solution for me.

pwnz22
  • 469
  • 2
  • 9
  • 19