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.

1
  • you can use php's implode function and store comma separated values into database.. Commented Sep 16, 2016 at 13:27

2 Answers 2

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}"
Sign up to request clarification or add additional context in comments.

7 Comments

{{ 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?
No problem with that. Did you identify if the error comes at validation or during order creation ?
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
Also I just realised the section were you select the user should be something like that: $user = User::find($request->input('user_name'));
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
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.