Problem with packages after Laravel namespace change to App\Models - php

So I used the namespace models shift to make the change from App to App\Models.
One of the packages in use (Ticketit) is calls App\User in it and I need a way to override this.
The offending file is:
\vendor\kordy\ticketit\src\Models\Agent.php
How can I override the "use App\user;" line there?

From what I am looking at in the source code, that is hardcoded, so you cannot do anything then... I am not sure if you could "fake" the App\User to point to App\Models\User using composer but that would be nasty, same if you created a class on inside app folder with name User and it only extends the model like this:
namespace App;
use App\Models\User as UserModel;
class User extends UserModel { }
That is nasty but maybe is a solution for you...
Edit: If you read carefully the documentation (please do so next time you use a package), it already says to make sure that App\User exists...
It tells you to do:
namespace App;
class User extends Models\User {
//leave this empty
}
For more info, see this github issue or create yourself a new one.

Related

Laravel Model not being found

Hi there I am having trouble using a model within a class there error being shown is Error
Class 'App\Models\RegisteredUsers' not found.
I have made sure that the namespaces match what is being used but I repeatedly get the same error.
Model code
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class RegisteredUsers extends Model
{
//
}
Controller code
<?php
namespace App\Http\Controllers;
use App\Models\RegisteredUsers;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
class RegisterUser extends Controller
{
$UserObj = new RegisteredUsers();
}
The directory
App/Http/Controllers/RegisterUser - controller
App/Http/Models/RegisteredUser - model
I created the model and the controller using the command line with PHP artisan.
I have tried the solutions from
laravel model class not found
as well as Model not found in Laravel
and a few laracast questions but i still get the error.
May need to rebuild the classmap
composer dump-autoload
With App\Models namespace in your RegsiteredUser model, the RegisteredUser.php model file must be in the app/Models/RegisteredUser.php directory. Try to move the Models folder outside the Http folder. And from now, you should never put the Models folder in the Http folder again.
Your error is App/Http/Models/RegisteredUser and use App\Models\RegisteredUsers;, did you place your Models in Http?. that is not correct, the Models should be in App root directory, so you are calling the RegisteredUser in the wrong place

(1/1) FatalErrorException Class 'App\User' not found in HomeController.php (line 28)

I'm actually making a laravel CRUD app.
So there is this model called User which has been created, but when I try to use it in a controller (In this case, HomeController.php), it says:
Here is line 28 from the controller:
I'm sorry if this question already exists but I've searched everywhere for a solution but could not find it.
Thank you.
Ideally, you should post the line you are importing the model, and the first few lines of your model.
But I will try to explore the possibilities of error.
Is your model inside a specific folder (just like app/MyModels/User.php) or just inside the default place when you use php artisan make:model? Remember to put the exact path in namespace line inside of model.
In the first line of your model you should have the path of your model with namespace. If the model is in its default directory, you should have the line below, but if it is inside of a folder, you have to put the folder path after de App:
namespace App;
Verify if you model is extends of Model:
class User extends Model {
In your controller the use line have the same path of namespace line in model, if you model is in default path, should be like this:
use App\User;
Remember that: the App is the namespace of your project, if you used the php artisan app:name command your namespace is no longer an App, and you should change it if you have the old name somewhere. If you using linux, remeber of the case sensitive.
If it does not work, put into the post the codes of the files I've listed so we can help.
in the begining
<?php
use App\User;

Laravel Namespace, how does it work?

I am new to Laravel 5 and I would like someone to explain to me how exactly Laravel's namespacing works.
So I had a class named Variant in app/models/Variant.php my code looks like this
namespace App;
use Illuminate\Database\Eloquent\Model;
class Variant extends Model{
/*Some code*/
}
in my route.php I have:
use App\Variant;
/*calls Variant::all() some where in code*/
Then I get an error saying Variant is not defined. However, if I change my namespace in Variant.php from namespace App to namespace App\Models and in route.php from use App\Variant to use App\Models\Variant everything magically works.
Why is that? Does it have to do with php namespace or the classmap property in composer.json? I am very confused.
Your classes are probably loaded by composer. What's the content of it - autoloading section in particular?
I guess it's loaded by PSR-4 standard, which respects director-name\file-name pattern.
Meaning:
App\Variant is sought in app/Variant.php
App\Models\Variant is sought in app/models/Variant.php
Therefore, when you change your namespace to the one corresponding with your directory path, it works.

phpcs - class must be in a namespace of at least one level - how to fix?

I am using laravel 4.2.
Lets say there is such class:
class BShopsController extends ShopsController
To fix this, I try to use name space lets say this:
namespace app\controllers;
and then it does not find ShopsController
so I add
use \ShopsController;
Then I get error:
Class BShopsController does not exist
What namespace should I use first of all so it would not break anything?
Edit:
BShopsController and ShopsController are in folder Shops
As your files are inside the Shops folder and I believe that the Shops folder is inside the app folder you should namespace your class the following way.
<?php namespace Shops;
class BShopsController extends ShopsController{}
Similarly,
<?php namespace Shops;
class ShopsController{}
So with the help of Shhetri and this Using namespaces in Laravel 4
I did this way:
namespace App\Controllers\Shops;
class BShopsController extends ShopsController{}
Also in routes.php then need to change to this:
Route::controller('shops', 'App\Controllers\Shops\ShopsController');
And where calling action() method - also need to use namespace.
Also needed to run
composer dump-autoload -o
otherwise were errors.
Also in ShopsContrller needed to to this:
use \App\Controllers\BaseController;
Because Shops controller was in another namespace than BaseController and cannot find it. But is extending from BaseController, so need it.

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()
These are my TrainingsController, Training Model, TrainingFacade, Trainings Service Provider and routes. Nothing I try seems to fix it.
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

Categories