0

i want to insert an array but it tells me Cannot access offset of type string on string

and i made foreach and when i do $return->request it looks like

{
    _token: "qb7dTYdsDVtw1RJnQQARzJMEqIfHPeQbHobiC8u2",
    _method: "POST",
    name: "Wanda Rojas",
    phone: [
    "+1 (841) 393-5088",
    "+1 (769) 441-1936"
    ],
    address: "Et est cum delectus"
}

and here is my model for clients

and i make phone field as array in protected $casts

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Client extends Model
{
    use HasFactory;

    protected $fillable = [
        'name',
        'address',
    ];

    protected $casts = [
        'phone' => 'array'
    ];
}

here is my form

<form action="{{route('clients.store')}}" method="POST">
        @csrf
        @method('POST')
    
        <input type="text" placeholder="add name" name="name"><br>
        @for ($i = 0; $i < 2; $i++) <div class="form-group">
            <label>@lang('site.phone')</label>
            <input type="text" name="phone[]" class="form-control">
            </div>
        @endfor
            <input type="text" placeholder="add address" name="address"><br>
            <button type="submit" class="btn btn-primary">add</button>
</form>

and here is my controller at store method

 public function store(Request $request)
{
    //return $request;

    $this->validate($request,[
        'name' => 'required',
        'phone' => 'required|array|min:1',
        'phone.*' => 'required',
        'address' => 'required'
    ]);

    $phone = $request->phone;
    foreach ($phone as $p){  
        $add = new Client();
        $add->name = $request->name;
        $add->phone = $p['phone'];
        $add->address = $request->address;
        $add->save();
    };

    return redirect()->route('clients.index');
}
3
  • What about if you change $add->phone = $p['phone']; to $add->phone = $p;? Commented Feb 7, 2022 at 23:59
  • error htmlspecialchars(): Argument #1 ($string) must be of type string, array given Commented Feb 8, 2022 at 0:10
  • You cannot output arrays in blade/PHP with echo, you would need to output the phone numbers using @foreach loop or print_r($phone, true) inside the curly braces. Commented Feb 8, 2022 at 0:14

2 Answers 2

1

Your code when you store client should looks like this

   public function store(Request $request)
    {
        //return $request;

        $this->validate($request,[
            'name' => 'required',
            'phone' => 'required|array|min:1',
            'phone.*' => 'required',
            'address' => 'required'
        ]);


        $phone = $request->phone;

        $add = new Client();
        $add->name = $request->name;
        $add->phone = $phone; // $phone it's already an array, so you should only set it to property 
        $add->address = $request->address;
        $add->save();
        
        return redirect()->route('clients.index');
    }

and in clients.index.blade.php to access phone

@foreach($client->phone as $phone)
  ...
  {{ $phone }}
  ...
@endforeach
Sign up to request clarification or add additional context in comments.

Comments

0

You are iterating through the array of phone numbers so $p is the phone number. $add->phone = $p should resolve your issue.

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.