Laravel 5 Can't Use User Class - laravel

I'm new to Laravel. I just installed Laravel 5 and have been going through some video tutorials in order to learn how it works.
I want to use the User class as it seems like it is very useful. In my web.php (routes) file, I have:
use app\Models\User;
Route::get('/users', function (){
$users = User::all(); //select * from users
return $users;
});
I get the following error upon loading that route in my browser:
I have a User.php file placed in my app/Models/ directory, and also changed in my auth.php file for it to reference this.
There are countless questions like this online and I try the fixes but I can't seem to get it to work. Any ideas?
Thanks!

Remove Models from use statement. It's not directory... Model is in namespace App so use:
use App\User;
Namespace and Directory is completely different things...
Or if you want to use in directory app/Models go to User.php model file and change namespace from App to App\Models

Related

Error message comes when trying to view template using controller

I am new to laravel and use the laravel 9.37v version. I have the following error message.
I created a blade template. I could view that blade template using view but after I tried to view that template using the controller I got this error message.
Target class [PagerConntroller] does not exist
Please check the spelling, if it is correct try to import the controller class in the Route. Im assuming the controller name is PagerConntroller and the function that you need to call is index so your web.php or api.php should be like :
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PagerConntroller;
Route::get('view',[PagerConntroller::class, 'index']);
Or else you can use the controllers without importing by simply uncommenting the namespace variable inside app/Providers/RouteServiceProvider.php line:29 the variable will look like protected $namespace = 'App\\Http\\Controllers'; then your route file should be like
<?php
use Illuminate\Support\Facades\Route;
Route::get('view','PagerConntroller#index');
If you tried both the ways and still the error persists kindly check your class name and the namespace as well

Laravel / Kakao Class 'App\Http\Controllers\Cache' not found

I try to set up kakao login to my Shopify store, but when I run the test I encountered this message error, what should I do? Thank you
The Cache facade doesn't live in the App\Http\Controllers namespace. If you do not include a use statement at the top of your PHP files for your classes, it is assumed the class does live in the same namespace.
The Laravel Cache facade lives in the Illuminate\Support\Facades namespace so at the top of your KakaoController with your other use statements, add the following:
use Illuminate\Support\Facades\Cache;
You must need to add "use Illuminate\Support\Facades\Cache;" on top of controller, middleware, command, event or blade files.
for example
<?php namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class KaKaoController extends Controller {
...
or "use \Cache;"

Laravel: "$ php artisan route:list" does not show route list in App\Http\Controllers\API

I created a UserController inside
app/Http/Controllers/API/UserController.php
Inside the UserController.php I have this
namespace App\Http\Controllers\API;
Inside the api.php I have this
Route::apiResources(['user' => 'API\UserController']);
I get this error below when I try to display the route list.
Error: Target class [App\Http\Controllers\UserController] does not
exist.
How do we tell Laravel that the UserController is inside app/Http/Controllers/API?
You need to defined the namespace, so laravel can find the controller:
<?php
namespace App\Http\Controllers\API;
...
class UserController extends Controller {
You have already defined the api.php with namespace API:
Route::apiResources(['user' => 'API\UserController']);
Try to clear the routes cache:
php artisan route:clear
php artisan optimize
Have you checked that the path you specify is correct ?
and you could also check the namespace in the UserController, correspond to:
namespace App\Http\Controllers\API ?
Or also you can simply add the controllers in the wep / api routes :
Route::get('prefix', 'DirOfYourController\YourController#SomeFunction);
Or as stated on the documentation you could take a look here:
https://laravel.com/docs/master/routing#route-group-namespaces
Well it's definitely because one of your web.php or api.php file
make sure that namespace of the file is correct in there for instance
Route::get('foo', 'API\UserController');
You need to follow only 2 steps to achieve the same:
Step1:
Define the correct name space in your UserController as below
namespace App\Http\Controllers\API;
Step2: Define route with directory name in your web.php for eg.
Route::get('dashboard', 'API\UserController#dashboard');

Laravel PhpStorm autocomplete

I am having problems with autocompletion in PhpStorm using Laravel. I have set up the IDE accordingly from this guide:
https://confluence.jetbrains.com/display/PhpStorm/Laravel+Development+using+PhpStorm
I cannot autocomplete basic functions like Input::only or Input::has. The closest answer I can find is in this thread:
Laravel Intellisense / autocomplete with PhpStorm
However, Input is already added as alias in config/app.php - still not working.
Anyone experienced same problem and/or know a solution to this?
EDIT:
Sorry for not providing code example - it's only been some Laravel trial and error, but here goes:
I have the route:
Route::post('/login', 'LoginController#authenticate');
In the action of the controller I have tried the following:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
class LoginController extends Controller
{
public function authenticate(Request $request) {
Input::get(); // <-- autocompletes
Input::has(); // <-- NO autocomplete
$request->only(); // <-- autocompletes
$request->validate(); // <-- NO autocomplete
}
}
use laravel ide-helper package
it can provide accurate autocompletion. Generation is done based on the files in your project.
phpstrom support auto-complete best thing is that

cant create laravel usercontroller

usually when I want to create controller I use php artisan
php artisan make:controller UserController
but in this case I have error in my terminal
[Symfony\Component\Debug\Exception\FatalThrowableError]
Class 'User' not found
and I wanna use it for users page - update and display users, as well as route
Route::resource('/user', 'UserController');
should I use it like that or something else? and why do I get error?
You get this error because you've used User class somewhere in the code (some controller probably). Find out where did you used it and add this clause to the top of that class:
use App\User;
Or just use full namespace, for example:
\App\User::get();

Resources