Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - Call to undefined method TrainingFacade::save()

I'm building a Laravel 4.2 app and I'm using Creolab's Modules package. It's an app for managing company trainings, so my module is called Trainings. All works well except saving newly created trainings. The error I receive when I submit the Create New form is:

Call to undefined method Roche\Trainings\Facades\TrainingFacade::save()

enter image description here

These are my TrainingsController, Training Model, TrainingFacade, Trainings Service Provider and routes. Nothing I try seems to fix it.

like image 515
Томица Кораћ Avatar asked Nov 07 '14 09:11

Томица Кораћ


1 Answers

You don't need to change your facade alias here, but you have other error here.

In your AdminTrainingsController you want to use Training model so you import Training before your class definition this way:

use Training;

but model Training is not in root namespace, in root namespace there is TrainingFacade because you probably define it in your app.php file this way:

'Training' => 'Roche\Trainings\Facades\TrainingFacade',

That's ok, you don't need to change this facade alias.

So now, Laravel inject into your constructor not Training model but TrainingFacade (because Training in root namespace is alias for TrainingFacade).

What you need here is importing correct namespace for your Training model.

When we look at your Training model class we see the code is:

<?php namespace Roche\Trainings\Models;

class Training extends \Eloquent {

protected $guarded = array('id');

}

so it's in Roche\Trainings\Models namespace.

So to make it work in AdminTrainingsController you need to change:

use Training;

into:

use Roche\Trainings\Models\Training;

Now Laravel will inject into constructor Training model (and not TrainingFacade as it was doing).

EDIT

You could also make some tests, for example changing in your app.php your TrainingFacade alias, for example to:

    'TrainingFacade' => 'Roche\Trainings\Facades\TrainingFacade',

but if you had use Training as you had, you would get the following error:

Class Training does not exist

because there would be no Training class in global namespace now (you changed alias for facade).

And if you removed use Training; completely, you would get:

Class Roche\Trainings\Controllers\Admin\Training does not exist

because by default class is loaded from current namespace, and your AdminTrainingsController is in Roche\Trainings\Controllers\Admin namespace

I hope now everything is clear, if it's not you could also look at How to use objects from other namespaces and how to import namespaces in PHP where I explained more about namespaces

like image 86
Marcin Nabiałek Avatar answered Sep 27 '22 23:09

Marcin Nabiałek