Click here to Skip to main content
15,883,904 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
{
  // Properties
  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");
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900