I learn Laravel from the tutorial and there is use factory(). In my project show me an error, Undefined function factory. I know that in Laravel 9 factory() does not exist, but someone could help me how can I modify the below code that will run in Laravel 9.
public function run(Faker $faker)
{
factory(
Task::class,
$faker->numberBetween(25, 50)
)->create();
}
factory() was used until Laravel 8. In newer versions of Laravel model factories are used: https://laravel.com/docs/9.x/eloquent-factories#main-content
Related
This question already has answers here:
Error “Target class controller does not exist” when using Laravel 8
(27 answers)
Closed 2 years ago.
I went to take Laravel 8 for a spin today, but it seems the Route facade cannot find controllers anymore.
The route /home gives me this error:
Target class [HomeController] does not exist.
I get a similar error when I run: php artisan route:list
Illuminate\Contracts\Container\BindingResolutionException
Target class [Auth\LoginController] does not exist.
at C:\...\vendor\laravel\framework\src\Illuminate\Container\Container.php:811
811 throw new BindingResolutionException("Target class [$concrete] does not exist.", 0, $e);
Thanks to lagbox, I ended up adding namespace('App\Http\Controllers') to the web route in RouteServiceProvider boot method:
public function boot()
{
$this->configureRateLimiting();
$this->routes(function () {
Route::middleware('web')
->namespace('App\Http\Controllers')
->group(base_path('routes/web.php'));
That did the trick for me. Any better solutions would be most welcome.
If this is a fresh install of Laravel 8, there is no namepsace defined in the RouteServiceProvider to be applied to the your routes. You can try to wrap the Auth::routes() call in a route group that declares the namespace App\Http\Controllers, or go about this in a different way. (assuming you have installed laravel/ui)
Route::namespace('App\Http\Controllers')->group(function () {
Auth::routes();
});
If you want to know how to deal with the lack of namespace being defined for your routes:
https://stackoverflow.com/a/63808132/2109233
I've just installed Backpack in my Laravel project. I followed the steps for creating general TourCrudController, but I get a 500 error and in logs the main issue is
local.ERROR: Undefined index: columns
. I did not make any changes in CRUD files except the route (made it plural and changed it in crud routes file, view, and TourCrudController). Other CrudControllers work just fine. Could you help me find the issue, please?
class TourCrudController extends CrudController
{
public function setup()
{
$this->crud->setModel('App\Models\Tour');
$this->crud->setRoute(config('backpack.base.route_prefix') . '/tours');
$this->crud->setEntityNameStrings('tour', 'tours');
}
Route::group([
...
], function () { // custom admin routes
Route::crud('tours', 'TourCrudController');
}); // this should be the absolute last line of this file
I close the issue, the bug was fixed in 4.0.11 version. If somebody needs it
https://github.com/Laravel-Backpack/CRUD/issues/2170
I'm in the process of upgrading a Laravel 4.2 app I inherited to Laravel 5.2. The app has multiple roles for logged in users that were handled with a before filter. Each controller has an array of functions and roles allowed for those functions:
public $actionFilter = [
'directories-create'=>['super','tsr'],
'directories-destroy'=>['super','tsr'],
'directories-edit'=>['super','tsr'],
'directories-directoryinfo'=>['super','tsr','admin'],
'directories-index'=>['super','tsr'],
'directories-store'=>['super','tsr'],
'directories-update'=>['super','tsr'],
];
then in the construct function it calls two beforeFilters that were in Controller.php
public function __construct()
{
$this->beforeFilter('#filterAuthorization');
$this->beforeFilter('#rerouteSite');
}
Controller.php had a public function filterAuthorization that checked if user's role had access to the route, and a public function rerouteSite that allowed user to stay on the same page but switch between accounts (for example, for a support rep).
I've spent a fair amount of time reading the manual, Googling and reading various tutorials, but I'm still unclear how to get my route-role array connected to the auth middleware. The Laravel docs provide syntax but not the context and the examples I've read either take a different approach or have a different usecase from mine.
I tried leaving the filter functions in Controller.php and calling them like this in the construct:
public function __construct()
{
$this->middleware('#filterAuthorization');
$this->middleware('#rerouteSite');
}
I get an error message: "Class #filterAuthorization does not exist"
I tried putting those functions in app\Http\Middleware\Authenticate, but I get the same error message: "Class #filterAuthorization does not exist"
I followed the steps on Matt Stauffer's blog here (https://mattstauffer.com/blog/laravel-5.0-middleware-filter-style/) and here (https://mattstauffer.com/blog/passing-parameters-to-middleware-in-laravel-5.1/) and on Nwanze Franklin's post here (https://dev.to/franko4don/deep-dive-into-middlewares-in-laravel-doo) as follows.
Create two new middleware files with Artisan
php artisan make:middleware FilterAuthorization
php artisan make:middleware RerouteSite
Edit the new middleware files with the functions from the old Controller.php
Register the new middleware in App\Http\Kernel
protected $routeMiddleware = [
'filterauth' => \Illuminate\Routing\Middleware\FilterAuthorization::class,
'reroutesite' => \Illuminate\Routing\Middleware\RerouteSite::class,
];
Edit the public function __contstruct() in the Controllers than need filtering
public function __construct()
{
$this->middleware('FilterAuthorization');
$this->middleware('RerouteSite');
}
Run
composer dump-autoload
php artisan clear-compiled
php artisan optimize
and I still get the same error:
Class FilterAuthorization does not exist
I'm sure there's a simple way to put this together without rewriting the whole role authorization system. Can someone point me in the right direction?
The kernel registration needs to reference the correct file locations as follows:
'filterauth' => \App\Http\Middleware\FilterAuthorization::class,
'reroutesite' => \App\Http\Middleware\RerouteSite::class,
And the controller boot should use the aliases rather than the class names:
public function __construct()
{
$this->middleware('filterauth');
$this->middleware('reroutesite');
}
Then Laravel can find the custom middleware.
I am learning testing on Laravel and I am unable to continue testing Eloquent by this error:
Error: Call to a member function connection() on null
This is my code:
public function test_users_can_follow_each_other()
{
$this->userOne = User::find(1);
$this->userTwo = User::find(2);
$this->userOne->followedBy($this->userTwo->id);
$this->assertTrue($this->userTwo->isFollowedBy($this->userOne->id));
}
I am also using the trait use DatabaseTransactions;
The database is already created with 2 records on users table. Do I need to config anything else, thanks!
replace
PHPUnit\Framework\TestCase
with
Tests\TestCase
I just want to create a global function in laravel 5.1
I create a file in App/Helper.php
Helper.php
<?php
namespace App\Helpers;
class Helpers {
public function somethingOrOther()
{
return "Yes It is";
}
}
test.blade.php
Helpers::somethingOrOther();
But it is not working
Every Time I gon a fatel error like "Class 'App\Helpers' not found"
Please Help me
Since you are Helper method are statics you could add your helper class your config/app alias just like a Facade, like so:
'aliases' => [
//'Helpers'=> 'App\Helpers\Helpers', //for Laravel 5.0
'Helpers'=> App\Helpers\Helpers::class, //for Laravel 5.1
]
check this question
What is the best practice to create a custom helper function in php Laravel 5?