0

I'm trying to use https://github.com/sebdesign/laravel-state-machine in my laravel application. But the package is not installed correctly. My controller doesn't recognise it. First, I added the following to my composer.json

"repositories": [
  {
     "type": "vcs",
     "url": "https://github.com/sebdesign/state-machine"
  }
]

Then I run

composer require sebdesign/laravel-state-machine:1.0

Then I added the following in my config/app.php

'providers' => [
    Sebdesign\SM\ServiceProvider::class,
],

'aliases' => [
    'StateMachine' => Sebdesign\SM\Facade::class,
],

After that I Publish the config file in config/state-machine.php using

php artisan vendor:publish --provider="Sebdesign\SM\ServiceProvider"

And that was it. Now I'm trying to use it in my controllers as:

// Using the facade
$stateMachine = StateMachine::get($article, 'simple');

But StateMachine is not recognised. I'm getting

Undefined type 'App\Api\V1\Controllers\Resource\StateMachine'

Am I missing something here? Should i add a use statement for something in the beginning of my controller?

Edit: Laravel 5.1 Thanks

marmahan
  • 183
  • 2
  • 15
  • Looks like you haven't included a `use` statement. Please check your code accordingly, and remove the tags that are not relevant – Nico Haase Apr 20 '20 at 08:03
  • Yeah that's my question, what should i include as a use statement. Can you write it down please – marmahan Apr 20 '20 at 08:34
  • Yeah i just figure it out. I added the use \SM\Factory\Factory as SMFactory; to my model – marmahan Apr 20 '20 at 09:03

1 Answers1

0

You don't need to use following lines if you are using latest version and auto discovery tool.

Since version 5.5, Laravel uses package auto-discovery, so you don't need to manually add the ServiceProvider and the facade. If you don't use auto-discovery or you are using an older version, add the service provider and the facade in config/app.php.

<?php

'providers' => [
    Sebdesign\SM\ServiceProvider::class,
],

'aliases' => [
    'StateMachine' => Sebdesign\SM\Facade::class,
],

After composer require you need to directly run this command.

php artisan vendor:publish --provider="Sebdesign\SM\ServiceProvider"

You need to install compatible version of the package as per your laravel version.

composer require sebdesign/laravel-state-machine:^1.0

Then you need to use it as below:

use \SM\Factory\Factory as SMFactory;
Dipak Mewada
  • 345
  • 2
  • 5
  • @marmahan i have updated my answer pls. have a look for how to install your compatible version of the package. – Dipak Mewada Apr 20 '20 at 07:55
  • @marmahan you need to follow all steps again with specific version of package. – Dipak Mewada Apr 20 '20 at 08:04
  • Please explain further how this code resolves the given error message `Undefined type 'App\Api\V1\Controllers\Resource\StateMachine'` – Nico Haase Apr 20 '20 at 08:04
  • It is working now. Thank you. I reinstalled the package and added use \SM\Factory\Factory as SMFactory; to my model. And then i was able to call SMFactory – marmahan Apr 20 '20 at 09:02
  • 1
    sure, I just did. But it would be a full answer if you could add to it the use statement use \SM\Factory\Factory as SMFactory; – marmahan Apr 20 '20 at 10:25