How to reference a directory in laravel? - php

This is probably a pretty simple question. But in my Laravel project I have my User.php file which is located in
App\Models\User.php
And I need to call it in a file that is located in App\Http\Controllers.
So I reference it as
App\Models\User
but instead the code thinks I'm saying
App\Http\Controllers\App\Models\User
Which obviously is incorrect and causes and error. How do I reference a directory in Laravel that's outside the one I'm currently in?

You need to learn about how namespace works.
I suppose at the top of your controller file, there is a line
namespace App\Http\Controllers;
so If in your code you wrote
$c = new A\B\C()
it looks for App\Http\Controllers\A\B\C. You have two options:
use $c = new \A\B\C, in your case, \App\Models\User
add the namespace alias on the top under the namespace line:
use App\Models\User;
And then in your code $user = new User

Related

Laravel upgrade broke model paths

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

Raw PHP vs Laravel Framework : 'include' file

I am going through basic concepts in Laravel and I have a conceptual question.
In Raw PHP:
When I want to create an object of a class (e.g User), that is in a different directory (e.g. app\user.php) with a namespace (e.g. namespace App), I have to include that file first (using include 'app/user.php') and then add the 'use' (use App\User).
<?php
include 'app/user.php'; // including the file
use App\User;
$user = new User('John');
However, in Laravel I have seen that they don't include any file at all. They just add the 'use' keyword (use App\User.php)and then can instantiate an object of it. ($user = User::find(1)).
<?php
namespace App\Http\Controllers;
use App\User;
class UserController extends Controller
{
public function index()
{
$user = User::find(1);
}
}
Can someone please explain how that happens?
This is not laravel feature this is autoload_class from php.
In https://www.php.net/manual/en/language.oop5.autoload.php
The spl_autoload_register() function registers any number of autoloaders, enabling for classes and interfaces to be automatically loaded if they are currently not defined. By registering autoloaders, PHP is given a last chance to load the class or interface before it fails with an error.
All classes exist in composer autoload file:
vendor/composer/autoload_classmap.php

Laravel Class 'App\FBLogin\authlogin' not found

I created a new FBLogin.php file in App Folder, added class:
namespace App\FBLogin;
class authlogin {}
Now i want to use this class in my Controller File, so i added:
use App\FBLogin\authlogin;
Now when i am trying to use this class authlogin, it is showing me error Class 'App\FBLogin\authlogin' not found
Is there something i am doing wrong?
Laravel Version: 5.5
Why would you use a lowercase format when naming your classes? Anyway, your namespace inside your app folder should follow your file structure.
If you create your class like below,
namespace App\FBLogin;
class authlogin {
// code here
}
Your file structure must be:
app/
FBLogin/
authlogin.php
Then you can use the class anywhere in your app by declaring the proper namespace
use App\FBLogin\authlogin;
$authlogin = new authlogin();

(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;

Can't get user_id Laravel with Auth::user() just with \Auth::user()

sorry if it is a noob question, but I'm trying to learn laravel on laracast and can't solve this by my own.
there is a store function on my ArticlesController like this:
public function store(ArticleRequest $request)
{
$article = new Article($request->all());
Auth::user()->articles()->save($article);
return redirect('articles');
}
and it returns a blank page, making clear that is some error, but if I change to
\Auth::user()->articles()->save($article);
it works as expected, saving the article with the user_id field.
I tried import with use App\Http\Controllers\Auth\AuthController; but I think this is not the way.
*obs: Laravel 5.0
In modern PHP, if you see the following at the top of a file
namespace App\Foo\Bar;
it means all the code inside that file in part of the App\Foo\Bar namespace. If you try to use a namespaceless class inside this file
$object = new Auth;
PHP will assume you want to use the class. In other words, it's the same as saying
$object = \App\Foo\Bar\Auth;
When you say
\Auth
you're telling PHP "use the global, top level namespace class named Auth.
This is a perfectly valid use of PHP, by the way. However, if you don't like using the global \ -- you can import the \Auth class into your PHP file (sometimes referred to as a "module" in other languages") by using the use statement.
namespace App\Foo\Bar;
//pull in the global class `Auth`
use Auth;
//...
$foo = new Auth;

Categories