Skip to content
Advertisement

Laravel problem with data array from database

I cant figure out how assign variables in controller from array with database datas.

this line:

 $user = User::where('id', $id)->first()->toArray();

dd($user) return this :

array:3[
    "id"=> 1
    "name"="John"
    "surname"="Wick"
]

how can assign variables from array ?

 $fullName = $name . $surname;

Advertisement

Answer

You can use extract to extract array keys as PHP variables.

<?php

$user = User::where('id', $id)->first()->toArray();

extract($user);

$fullName = $name . " " . $surname;
 
echo $fullName;

Demo: https://3v4l.org/1IRRF

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement