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();
Related
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;"
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');
I'd start a new laravel project, and want to make a controller called ProductController using Artisan, but it ends up with an error
“BadMethodCallException : Method Illuminate\Routing\Route::get does not exist.“
I'm using laravel 6.0
routes:
Route::get('/products', 'ProductController#index');
Command used:
php artisan make:controller ProductController
Import the Route facade instead of Illuminate\Routing\Route
use Illuminate\Support\Facades\Route;
you maybe
use
href="{{route('--')}}"
instead of
href="{{url('---')}}"
for routing in your blade , just change it to href="{{url('---')}}"
This is the test case I've written and when i try to run it with this command vendor/bin/phpunit on linux it gives me the error "Call to undefined method Tests\Unit\ExampleTest::visit()"
namespace Tests\Unit;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*
* #return void
*/
public function testBasicTest()
{
$this->visit('/Login')
->type('KP123#gmail.com','email')
->type('123456','password')
->press('Login')
->seePageIs('/home')
->see('Katy Perry');
}
}
I've tried running composer update and it still could not work. Had Anyone experienced this issue before?
The visit() method looks like it's part of the SeleniumTestCase. So my guess is you either extend from the wrong base class. I assume it should look something like this:
class ExampleTest extends \PHPUnit_Extensions_Selenium2TestCase
{
// your test method
}
edit: I just noticed the Laravel tag, so it's probably more something like Illuminate\Foundation\Testing\TestCase. If this gives you errors that it can't find the class you have to set the bootstrap file in your phpunit.xml to the vendor/autoload.php or another appropriate bootstrap file where the file is registered in the autoloader.
I might have come in late onto this question but here is my 2 cents. I have been trying to use the same methods with my test case which led to a dismal failure.
I the realised that I could actually make use of the TestResponse class which is found on the Foundation namespace that wraps many methods around the response object.
$this->get('/url/to/visit'); //Then you can make your assertions on the returned response.
In my case I had authentication turned on for the application therefore it always complained about the header view where we display the username of the currently logged in user. If you run into that situation then use the Auth Facade like shown below:
\Auth::loginUsingId(1);
You should now be able to assert that your response has some text in it. Hope this helps. By the way I am on Laravel 5.4
If you are using new version of Laravel you must use Laravel Dusk to use similar methods to $this->visit or see. It was excluded from Laravel around version 6.
Instead you can use, but it has limited options:
$this->get('/Login')
composer require laravel/browser-kit-testing --dev
Try this
if you are using > Laravel 8 try :
$this->get('/Login')
instead of visit function, and assertSeeText instead of see
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