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'm trying to create a crud in Symfony 4.4 with bin/console make:crud command but I'm getting the following error
In MakeCrud.php line 103:
Call to a member function getRepositoryClass() on null
this is part of the class I'm trying to use for the crud
<?php
namespace Domain\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
class Segment
{
private $id;
private $uidentifier;
private $name;
I type the class name with the namespasce oto genterate crud but I get the error, what could be wrong?
So I got a little bit curious and confirmed that it is a namespace issue.
I made myself a Domain\Entity\Segment class and then ran
bin/console make:crud 'Domain\Entity\Segment'
And got the same error message.
The make:crud command expects entities to live under App\Entity. Does not seem to be any way to convince it to look under Domain\Entity just for entities.
However the Maker Bundle itself allows you to change the name of the App's root namespace:
# config/packages/maker.yaml Need to add this file
maker:
root_namespace: Domain
# Then run
bin/console make:crud Segment
And it works. The problem is that the root namespace is used for generating all the files so, for example, you end up with a Domain\Controller\SegmentController class which is probably not where you want them to be.
I checked the source and there is no easy way to adjust just the entity namespace. If you plan on using crud quite a bit then you either need to copy your entities to App\Entity or specify the root_namespace and then copy/refactor your generated controllers and forms. Either way it will be a pain.
I suppose you could also fork the maker bundle and edit the source code directly. Might actually be worth the effort if you have a bunch of crud to generate. But that will be left as an exercise.
Hi i'm newbie in Laravel just practicing with some tutorial videos and i'm wondering if it is possible to configure the brackets position when i use the artisan commands.
For example when i create new controller using artisan make:controller it makes
class Controller extends Controller
{
//
}
What i want is the brackets places right next to the class declaration not in the new line like above. To be more specific
Class Controller extends Controller{
//
}
This is what i want. Is it possible to configure the brackets position?
I've googled a lot and read documentation but couldn't find any of information about it.
--Edited--
Thanks to you guys i know it's not Laravel problem and it can be configured in code editor.
I'm using vs code and have installed several php formatter but it seems all of them follow the PSR-2 styles.
I found JS configuration for its brackets position but couldn't find for PHP
i know it's not even problem i just don't like this format
As mentioned in the comments, the stubs that Laravel generates follow PSR-2 so making this change would break that.
To override the controller code that Laravel generates with make:controller you're going to have to override the ControllerMakeCommand and copy the default stubs you want to edit.
The stubs for this command have changed quite a bit with the different versions of Laravel so it will require a bit of copy and paste to ensure it works correctly.
Extend the ControllerMakeCommand
Inside your app/Console/Commands directory create ControllerMakeCommand.php and add the following:
<?php
namespace App\Console\Commands;
use Illuminate\Routing\Console\ControllerMakeCommand as BaseCommand;
class ControllerMakeCommand extends BaseCommand
{
//
}
Add the getStub method
Find the getStub() method in the Illuminate\Routing\Console\ControllerMakeCommand and copy that into your newly created ControllerMakeCommand.
Copy the default stubs
Copy
vendor/laravel/framework/src/Illuminate/Routing/Console/stubs
to
App/Console/Commands/stubs
(you don't have to copy all of the files inside it, just the controller.* that you want to override).
Change the formatting of the files as you see fit.
Laravel <=5.4
If you're using Laravel <=5.4 commands are not automatically loaded for you so you're going to have to tell Laravel that you want to use this command.
In your App\Console\Kernel.php add your new class to the $commands array e.g.:
protected $commands = [
\App\Console\Commands\ControllerMakeCommand::class
];
NB Obviously, feel free to store the stubs wherever you want just make sure you update the paths in your new ControllerMakeCommand.
I do not know why this error occurs when I execute the migrations as I do not have repeated classes.
Migrations:
2014_10_12_100000_create_password_resets_table.php
2019_01_18_020910_create_roles_table.php
2019_01_18_025535_create_members_table.php
2019_01_18_025536_create_users_table.php
2019_01_18_183649_create_projects_table.php
2019_01_18_184249_create_member_project_table.php
2019_01_18_184719_create_sprints_table.php
2019_01_18_185218_create_tasks_table.php
2019_01_21_033045_add_shortname_to_project.php
Error:
PHP Fatal error: Cannot declare class CreateRolesTable, because the name is already in use in
oyectos\database\migrations\2019_01_18_020910_create_roles_table.php on line 33
In 2019_01_18_020910_create_roles_table.php line 33:
Cannot declare class CreateRolesTable, because the name is already in use
Class:
class CreateRolesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->string('name',128)->unique();
$table->string('description');
$table->boolean('system');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('roles');
}
}
As well as other answers given, this error can also happen if the migration filename is not a snake-case version of the class name.
So a migration file 2019_01_18_020910_create_roles_table.php must contain the class CreateRolesTable. If it contains the class CreateRoleTable, with a missing s, then the "Cannot declare X..." error is thrown. I've found this on Laravel 8, and may apply to earlier versions.
It appears to happen because Laravel loads the migration file multiple times when the filename is misspelled, and the second time loading is when the exception is throw.
First Solution :
It seems like you have 2 migrations done at different time with essentially same name.
for example : 2019_01_18_020910_create_roles_table.php
and 2019_01_16_020910_create_roles_table.php
Laravel will convert this filename eliminating the date signature and Camel Casing the remaining text.
So both of these migration will have class CreateRolesTable even if the time signatures are different. Check if your migrations directory have such 2 files.
To check this run this from terminal in project root : grep -ri 'createrolestable' database/migrations
Second Solution :
Sometimes composer's internal class autoloading causes this issue. Do following to check if it resolves :
run composer install
Third Solution :
This is likely to be invalid but a same file should not have same class declaration 2 files by mistake.
Fourth Solution :
There might be a package you have installed which has a migration with same class name. To find run grep -ril 'createrolestable' vendor
If it shows any file then thats what causing 2 classes to have same names.
You can create a new one php artisan make:migration create_roles_table_custom . and then copy what you have in current migration to the new one and delete the existing one(not from package but the one you have created).
This will create a class CreateRolesTableCustom which is different than what the package already has.
If you are using Laravel 8 or above, you can use Anonymous Migration to avoid the conflict with Class name.
Below is how you to declare an Anonymous Migration. Do not forget the semicolon at the end.
return new class extends Migration
{
//
};
More from the Docs.
Becareful of migration file name.
For me, migration file name was:
2021-10-13_000000_create_examples_table
But correct was :
2021_10_13_000000_create_examples_table
LOL
In my case, i had my own package, which had migraration and it wasn't named properly. I named it without date like this: create_orders_table.
I changed it to 2021_08_03_000000_create_orders_table and it helped.
I ran into this (misleading) error and it turned out I accidently omitted the word Create from the migration class name.
Error: Cannot declare class FooTable, because the name is already in use
Incorrect: class FooTable extends Migration
Correct: class CreateFooTable extends Migration
For me, it was a problem with Laravel Sanctum (now built into Laravel 8). I had generated migrations via a package and somehow ended up with something in vendor\laravel\sanctum\database\migrations.
I ran php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider" to persist it to standard migrations.
See here for details.
I had this problem. I used composer dump-autoload and it solved the problem.
Even if you don't have any such files with same Class name and you are still facing the same problem then try
composer dump-autoload
This can be caused by a number of things. Follow these steps to resolve the issue.
First run this command in terminal
php artisan optimize:clear
composer dump-autoload
If those don't resolve the issue, then you have renamed a migration file that was published from Laravel Cashier. To resolve it, do the following:
Rename the migration file. Something like 2019_01_18_020910_create_roles_table can be renamed to 2019_01_18_020910_create_role_table
Rename the class. Something like CreateRolesTable can be renamed to CreateRoleTable
in my case, it's throwing the error because I changed the time of migration file which are added by laravel cashier, my time sequence is correct but it's still throwing name issue.
Then I revert the time back to the original migration time and the problem is solved.
I am trying to migrate my own extension from an older (typo3 6.2.31) version to the latest (Typo3 8.7.19). The extension includes a table with uploaded videos.
In my database the records are alle persistent.
I only can see a row for each video like in database, but without values. Using debugger utility I can see all attributes, like 'title'. But these are all empty except the uid.
I doublechecked the controller and the model and this looks fine to me.
Maybe there are changes in the tca settings or ext_tables.php which I need to perform.
What else can cause this problem?
Best Regards
Let me know if or witch code you need to see to give a more detailled answer and I will add it to my post
Class/Namespace Controller
namespace TYPO3\Videoverwaltung\Controller;
class VideoController extends
\TYPO3\CMS\Extbase\Mvc\Controller\ActionController
{
Class/Namespace Model
namespace TYPO3\Videoverwaltung\Domain\Model;
class Video extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity
{
Class/Namespace VideoRepository
namespace TYPO3\Institutsvideoverwaltung\Domain\Repository;
class VideoRepository extends \TYPO3\CMS\Extbase\Persistence\Repository {
Thanks in advance.