I am trying to use an Eloquent model inside the Sentry package (this could've been any package).
I've refered to it using use App\Models\User; in the top, and User:: when I need to call it.
However, I am getting this error: Class 'App\Models\User' not found
What am I doing wrong?
The current code is here: http://paste.laravel.com/H19
I assume thath your models are in the App part of the application.
In laravel, the App namespace is the top level namespace (the default one).
So to call your models, you have to call \User (because \ refer to the top level namespace)
Alternatively you can do:
use \User as User
And then you can call it User instead of \User
Related
I've performed a long-overdue update on a Laravel project from v5.7 to v9 and ran into a Target class does not exist error. I found this guide and used the first method to resolve the error (adding the namespace into the RoutesServiceProvider.php boot function). This resolved that error but now, everything is giving me Class "App\Whatever" not found.
I did notice that models are now stored in a Models directory within the app directory rather than directly in app, so have moved them to Models. I figured that might be breaking my use App\Whatever; lines at the top of my controllers, so I've tried use App\Models\Whatever and also use app\Models\Whatever (since the "a" is lowercase in the directory name) but no effect.
I should note I don't really have a good grasp of namespaces, MVC frameworks etc. so ELI5 etc :-)
Some of my controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Thing;
use App\AnotherThing;
...
public function thing_summary($id) // show the summary view of the thing
{
if(Auth::check()) {
$thing = Thing::find($id);
...
Laravel 7/8/9 sticks with strict namespacing. When you move the models to a new directory, you need to update the namespace in the models themselves (if specified at all) and any file that has a use for the model. If the models move from app/ to app/models, the namespace must be changed to App/Models
i am working on a laravel project .almost i see in each controller these line format :
!auth()->user()->can('something (differ from each controller to other one)')
but in my php editor it says method {can} not found for this object. so i try to found method can and not found it .even i edit __Call magic method to see if when can method is calling does magic function run but i know it never run for can function.so how it is possible to link function to object when it is not defined in class and its all mother class.and where does can function locate?i search and i see laravel has some policy for authorize users but yet i don not know how can function link to user object without
define in classes and even magic method does not run .and how can i develope these type of function (for authorizing in laravel and change policy)
can method is a part of Authorizable trait in Illuminate\Foundation\Auth\Access
Look at your User model, you will found that model is extended from Illuminate\Foundation\Auth\User as Authenticatable. and Authenticatable uses the above trait.
it's part of the core of laravel, you can read the documentation from official docs
I am trying to run the command php artisan scout:import "App\User" to import user records into search driver as per documentation (Laravel 5.3 Scout Documentation). I keep getting [BadMethodCallException]
Call to undefined method Illuminate\Database\Query\Builder::makeAllSearchable()
as an error. Why am I getting this error? I have included the searchable trait in my users controller and added the scout class to my app/config providers array, so I am struggling to see why the method doesn't exist...
You should not add the trait to the controller but to the model. So in your case to App\User.php
<?php
namespace App;
use Laravel\Scout\Searchable;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use Searchable;
}
Like Jakub has said, you have to add the Searchable trait to your User model, not to the controller.
If you're using toSearchableArray() on your model, do not forget to include the id column in the array, otherwise it won't work.
You could also comment the toSearchableArray() function, import the existing users and then add it back.
In Laravel 5.2 I made a folder app/Models.
This is one of my models: app/Models/Pin.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Pin extends Model {
protected $table = 'pins';
}
In my composer.json I added to "classmap": app/Models.
Then I runned composer dump-autoload
In my PinController, I have:
use App\Models\Pin as Pin;
When I do: Pin::all(); it returns a collection.
But when I try: Pin::find(1); I get:
Method find not found in class \App\Models\Pin
Is their a way I can get all methods from Eloquent?
I see nothing wrong with your approach. It should be working, and it works if I try it on a clean Laravel 5.2.5 install. Maybe some other dependencies are causing side effects for you. Have you tried to isolate the problem on a clean install?
You can find all Eloquent methods in the API docs, but keep in mind the Model class has a magic __call() method. This is why you won't see a find() method directly in the Model class; instead calls to Model::find() will be passed to the Builder::find()method.
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