Hello Everyone,
I am looking to transfer an objects array from a controller method to a vue in laravel.
I started from the following code snipped to start my investigation:
In FrontendController.php:
class FrontendController extends Controller
{
public function usersToday(Request $request)
{
$users = User::where('date', date('d-m-Y'))->get();
return $users;
}
}
In api.php:
Route::get('/users/today','FrontendController@usersToday');
In users.vue:
{{users}}
...
mounted()
{
axios.get('/api/users/today').then((response) =>
{
this.users = response.data;
})
}
This principle works happily. My idea is now to transfer my own objects array like this:
in MyUser.php:
class MyUser extends Object
{
public $name;
public $id;
public $department;
function __construct($id, $name, $department)
{
$this->id = $id;
$this->name = $name;
$this->department = $department;
}
}
in FrontendController.php:
class FrontendController extends Controller
{
public function usersToday(Request $request)
{
$users[] = array();
$users[0] = new MyUser(1, "User1", "Covid");
$users[1] = new MyUser(2, "User2", "Covid");
}
return $users;
}
When I want to see the json format of users, it does not work.
Do I miss a serialize attribute to transfer the information into the view in json format ? Is it possible to do what I am trying to achieve ?
Thank you very much in advance.
Best regards.
Miqi.
What I have tried:
I have tried in usersToday:
return json_encode($users);
and also
$users = new MyUser(1, "User1", "Covid");