Home > other >  trying to insert an array but Cannot access offset of type string on string laravel 8
trying to insert an array but Cannot access offset of type string on string laravel 8

Time:02-08

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 >
            <label>@lang('site.phone')</label>
            <input type="text" name="phone[]" >
            </div>
        @endfor
            <input type="text" placeholder="add address" name="address"><br>
            <button type="submit" >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');
}

CodePudding user response:

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

CodePudding user response:

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

  •  Tags:  
  • Related