Search code examples
phplaravelcollections

Property [name] does not exist on this collection instance in laravel when merging two arrays


In laravel I faced that issue when using array merge in controller . I have to use it because I want to show data from both arrays and show them in one table of laravel so how could I resolve that error and what does it actually means? my controller

 $array1=DB::table('table1')
              ->whereDate('created_at', Carbon::today())
              ->select('table1.name as name','table1.age as age')
              ->get();
    $array2=DB::table('table1')
              ->leftJoin('table_2','table1.id','=','table2.table1_id')
              ->whereDate('new_date', Carbon::today())
              ->select('table1.name as name','table2.age as age')
              ->get();
    $data=array_merge(array($array1,$array2));
    
    return view('index')->with('records',$data)

my view

@foreach($records as $record)
<td>{{$record->name}}</td>
@endforeach

error

Property [name] does not exist on this collection instance

Solution

  • try to add -> toArray() after ->get().

    and in the view use {{$record['name']}}.

    and use array merge like this: $data=array_merge($array1,$array2);