auto-injected Laravel model has no attributes

I’m new to Laravel. I have created a model, a resource controller, and a route for one of my tables, I have modified the model class to use a particular table name, but the model object injected by Laravel 5.4 has no attributes even though a corresponding record exists in the database. Here are the steps I took.

1) Create the model with artisan. I ran this command:

php artisan make:model Tree

2) Modify the Tree model class as instructed to specify a specific table. I had to do this because my table is named tree, not the “trees” as Laravel would otherwise assume based on its internal rules.

/**
 * The table associated with the model.
 *
 * @var string
 */
protected $table = 'tree';

3) Create a resource controller that makes use of my model with this command

php artisan make:controller CategoryController --resource --model=Tree

4) Add a resource route routes/web.php in order to map a web server path onto the controller:

Route::resource('categories', 'CategoryController');

5) Modify the show() method of the CategoryController to var_dump the injected $tree object. It looks like this:

/**
 * Display the specified resource.
 *
 * @param  AppTree  $tree
 * @return IlluminateHttpResponse
 */
public function show(Tree $tree)
{
    // we need to display the children of $tree
    var_dump($tree);

}

6) My table structure follows all the conventions specified by Laravel docs. There is an integer id column that is unsigned & auto-incrementing. I have the created_at and updated_at timestamps. The only thing different is that the table name is “tree” and not “trees”, but that should be covered with the change I made above:

CREATE TABLE IF NOT EXISTS `tree` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `parent_id` int(10) unsigned DEFAULT NULL,
  `label` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `display_order` int(11) unsigned NOT NULL DEFAULT '0',
  `forum_id` int(5) NOT NULL DEFAULT '0',
  `url` varchar(128) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `flavor` tinyint(4) NOT NULL DEFAULT '0',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `parent_pkey` (`parent_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

This table contains data. It most definitely has a record with id=1.

7) I visit the url which should activate the show() method of my resource controller. The output I get verfies that this is in fact the method CategoryController::show(). http://example.com/categories/1

Here is the problem.

The output of var_dump($tree) has no attributes. There is no error, but something is wrong with the injected object.

object(AppTree)#217 (24) {
  ["table":protected]=>
  string(4) "tree"
  ["connection":protected]=>
  NULL
  ["primaryKey":protected]=>
  string(2) "id"
  ["keyType":protected]=>
  string(3) "int"
  ["incrementing"]=>
  bool(true)
  ["with":protected]=>
  array(0) {
  }
  ["perPage":protected]=>
  int(15)
  ["exists"]=>
  bool(false)
  ["wasRecentlyCreated"]=>
  bool(false)
  ["attributes":protected]=>
  array(0) {
  }
  ["original":protected]=>
  array(0) {
  }
  ["casts":protected]=>
  array(0) {
  }
  ["dates":protected]=>
  array(0) {
  }
  ["dateFormat":protected]=>
  NULL
  ["appends":protected]=>
  array(0) {
  }
  ["events":protected]=>
  array(0) {
  }
  ["observables":protected]=>
  array(0) {
  }
  ["relations":protected]=>
  array(0) {
  }
  ["touches":protected]=>
  array(0) {
  }
  ["timestamps"]=>
  bool(true)
  ["hidden":protected]=>
  array(0) {
  }
  ["visible":protected]=>
  array(0) {
  }
  ["fillable":protected]=>
  array(0) {
  }
  ["guarded":protected]=>
  array(1) {
    [0]=>
    string(1) "*"
  }
}

Have I done something wrong? How do I get Laravel to inject the correct object?


Source: stackoverflow-php

How to create multiple non-shared instances without injecting the ServiceLocator in ZendFramework2?

I have a factory that creates a non-shared service and a controller factory in my module.config.php file. This part of the configuration looks like this:

"service_manager" => [
    "factories" => [
        EntityUser::class => FactoryEntityUserFactory::class
    ],
    "shared" => [
        EntityUser::class => false
    ]
],
"controllers" => [
    "factories" => [
        ControllerUserAdminController::class => FactoryControllerUserAdminControllerFactory::class
    ]
]

The User entity is non-shared, because I allow the administrator to add multiple users at the same time, therefore I need to instantiate multiple instances of the User entity class.

Here is my controller factory:

<?php

namespace UserFactoryControllerCore;

use UserControllerUserAdminController;
use InteropContainerContainerInterface;
use UserEntityUser;
use ZendServiceManagerFactoryInterface;
use ZendServiceManagerServiceLocatorInterface;

class UserAdminControllerFactory implements FactoryInterface{

    /**
     * @param ContainerInterface $container
     * @param string $requestedName
     * @param array|null $options
     *
     * @return UserAdminController
     */
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null){
        $serviceLocator = $container->getServiceLocator();

        return new UserAdminController($serviceLocator->get(User::class));
    }

    /**
     * @param  ServiceLocatorInterface $serviceLocator
     *
     * @return UserAdminController
     */
    public function createService(ServiceLocatorInterface $serviceLocator){
        return $this($serviceLocator, UserAdminController::class);
    }

}

Currently, it injects only a single instance of the User entity class inside the constructor of the controller. However, I need an undefined amount of new instances, based on what the administrator wants to do.

One possible solution is to pass the whole service locator and create new instances every time by calling $this->getServiceLocator()->get(User::class) in the controller, BUT this is considered to be an anti-pattern.

The question here is how to create new instances of the User in the controller, using the UserFactory class?


Source: stackoverflow-php