I just can't figured out this issu; On a new Laravel project, I just can't use table method on DB class.
I use PhpStorm as EDI on OSX Sierra with Laravel 5.5.
Here my steps:
In terminal: Laravel new testproject
In terminal: composer install
In terminal: php artisan make:controller testdummy
In EDI:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class testdummy extends Controller
{
//
public function index(){
DB::table('title')->insert([
'label' => 'test1',
'desc' => 'test2',
]);
}
}
The 'table' have a warning popup in EDI that say:
'Method table not found in Illuminate\Support\Facades\DB'.
And no insertion are made to database.
Have you some hints on where to look next to help me find the problem?
thanks,
Related
I use the Maatwebsite package to import Excel files in Laravel 8. I also executed the following command after installing the package and adding needed aliases and providers in the app.php.
php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider"
But when I create an import with a model Mymodel (a model that I have), I get the following error.
undefined Maatwebsite\Excel\Concerns\ToModel;
Import class
class Importer implements ToModel
{
public function model(array $row)
{
return new Universite([
//
]);
}
}
You have to restart your VS Code, because it'll apply your latest package installed
Add the following before you create the class:
use Maatwebsite\Excel\Concerns\ToModel;
I created a posts table, and I used php artisan make:model Post to create a Post model.
php artisan make:migration create_posts_table --create="posts"
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
//
}
When I tried to get all records using model::all()...
route::get('/find', function (){
return var_dump(App\Post::all());
});
It always gives me this error.
Symfony \ Component \ Debug \ Exception \ FatalThrowableError
(E_ERROR) Unsupported operand types.
Can someone please help me?
The problem is caused by App\Post::all() but I dont know why
This is a bug concerning Xdebug when using PHP 7.3 or 7.3.1 with Laravel 5.7.
Temporary workarounds include:
- Disable php-xdebug on PHP 7.3.0*
- Disable OPCache (opcache.enable=0 on php.ini, then restart PHP services)
- Set opcache.optimization_level=0x7FFFBBFF
More info here: https://github.com/laravel/framework/issues/27030
initialize you table name into your model ....
class Post extends Model
{
protected $table = 'posts';
}
route::get('/find', function (){
return dd(\App\Post::all());
});
Thanks to Jonas Staudenmeir' comment. This is the problem with laravel version 5.7. And it has not been fixed yet
I reinstall laravel 5.5, it works perfect!!!
I'm trying to inject the class HotelsTransformer without success with the next code:
UserTransformer
<?php
namespace App\Transformers;
class UserTransformer extends Transformer
{
...
}
HotelsTransformer
<?php
namespace App\Transformers;
class HotelsTransformer extends Transformer
{
...
}
ApiHotelsController
<?php
namespace App\Http\Controllers\Api;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use \App\Hotel;
use \App\Transformers\HotelsTransformer;
class ApiHotelsController extends ApiController
{
protected $HotelsTransformer;
public function __construct(HotelsTransformer $HotelsTransformer)
{
$this->HotelsTransformer = $HotelsTransformer;
dd($this->HotelsTransformer);
}
When I inject UserTransformer, it's all OK, but when I change UserTransformer with HotelsTransformer it throws me this error.
I don't know why is this happening, because I cloned UserTransformer and change its name but same error persists.
Check your following namespace. May be it does not exist or namespace path is not correct
use \App\Transformers\HotelsTransformer;
Try to run composer dumpauto command.
Try running these commands from your terminal (from the root directory of your project)
// use sudo if it asks for the root permission
composer update
composer dump-autoload
php artisan config:clear
Rerun the app/project and try again.
These 2 commands will refresh composer loaded classes and clear the cache to make the project run freshly, hope it helps.!
Ok, I have solved it, the filename was wrong, changed:
app/Transformers/HotelsTranformer.php
to:
app/Transformers/HotelsTranformer.php
1 hour spent on that like a crazy, good job.
I use Lumen 1.0 for an API project.
I have already enable Eloquent by uncomment the following line in bootstrap/app.php file :
$app->withEloquent();
But when I want to create my first model with migration it fails :
php artisan make:model Book --migration
Error message :
[InvalidArgumentException]
Command "make:model" is not defined.
Did you mean one of these?
make:seeder
make:migration
Laravel doc about Eloquent (http://laravel.com/docs/5.1/eloquent#defining-models).
Lumen doc (http://lumen.laravel.com/docs/installation) doesn't include Eloquent doc because, it's not enable by default.
Do you have any ideas to avoid this error ?
Add details
php artisan --version
Displays :
Laravel Framework version Lumen (5.1.6) (Laravel Components 5.1.*)
You're seeing this error because Lumen doesn't come with make:model.
To see a list of all the artisan commands you have at your disposal just run php artisan.
That being said I did just find this package which I've added to a lumen installation and it seems to work fine https://github.com/webNeat/lumen-generators#installation
Hope this helps!
If you check all the available commands using php artisan list you will see that you don't have all the default ones offered by laravel. But you can get the most importants using the lumen-generator package (not to be confused with lumen-generators). It has the advantage of offering more commands than the other one mentioned.
To use it just install it using composer:
composer require flipbox/lumen-generator
Then enable it in your bootstrap/app.php file:
$app->register(Flipbox\LumenGenerator\LumenGeneratorServiceProvider::class);
You will now be able to use all these new commands using artisan:
key:generate Set the application key
make:command Create a new Artisan command
make:controller Create a new controller class
make:event Create a new event class
make:job Create a new job class
make:listener Create a new event listener class
make:mail Create a new email class
make:middleware Create a new middleware class
make:migration Create a new migration file
make:model Create a new Eloquent model class
make:policy Create a new policy class
make:provider Create a new service provider class
make:seeder Create a new seeder class
make:test Create a new test class
Just have a look at the official documentation: https://github.com/flipboxstudio/lumen-generator
Go to the project directory and add the generators package to your composer.json using the following command:
composer require wn/lumen-generators
Add following code segment to app/Providers/AppServiceProvider.php:
public function register()
{
if ($this->app->environment() == 'local') {
$this->app->register('Wn\Generators\CommandsServiceProvider');
}
}
Make sure that you have un-commented the following line in bootstrap/app.php to allow service providers on your project:
$app->register(App\Providers\AppServiceProvider::class);
Run php artisan list on the project directory (document root). Now you will see new items there.
just create your model file manually in the app directory
example
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Article extends Model {
protected $table = ‘articles’;
protected $fillable = [
‘title’,
‘description’,
‘body’
];
}
$app->register(Flipbox\LumenGenerator\LumenGeneratorServiceProvider::class); add this line into "bootstrap\app.php" and save this file then make the command.It will work.
there are some packages that can help you to have all of artisan command that you has on Laravel .
install below package to have more artisan command.
https://github.com/flipboxstudio/lumen-generator
I have this DatabaseSeeder.php:
<?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class DatabaseSeeder extends Seeder {
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
Model::unguard();
$this->call('MemberInvitationSeeder');
}
}
I have this file MemberInvitationSeeder.php, sibling to the DatabaseSeeder.php file
<?php
use Illuminate\Database\Seeder;
use App\MemberInvitation;
class MemberInvitationSeeder extends Seeder {
public function run()
{
MemberInvitation::truncate();
MemberInvitation::create( [
'id' => 'BlahBlah' ,//com_create_guid(),
'partner_id' => 1,
'fisrt_name' => 'Thats',
'last_name' => 'Me',
'email' => 'me#mymail.com',
'mobile_phone' => '444-342-4234',
'created_at' => new DateTime
] );
}
}
Now I call
php artisan db:seed
and I get:
[ReflectionException]
Class MemberInvitationSeeder does not exist
I tried everything I could find including "composer dump-autoload". to no avail. What am I doing wrong?
Step one - generate seed:
php artisan make:seed MemberInvitationSeeder
Step two - In DatabaseSeeder.php add line:
$this->call(MemberInvitationSeeder::class);
Step three:
composer dump-autoload
Step four:
php artisan db:seed
This should work
If this isn't the clue, check the composer.json file and make sure you have code below in the "autoload" section:
"classmap": [
"database"
],
I solved this by adding the class to the seeder file, with the instruction use:
<?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
use App\YourClassName;
I believe I know the reason now.
The new class MemberInvitationSeeder wasn't in the autoloaded classes in the composer.json file.
It wasn't there because I added that class manually.
Now, going forward, if I add such classes again, what should I use in order for my class to automatically to the autoloader?
This work for me
composer dump-autoload
php artisan db:seed
I ran into the similar error but I was using DB facade DB::table rather than model. I am posting the answer just in case somebody has similar issues.
The seeder file had namespace namespace Database\Seeders; and laravel was trying to look for DB class inside the namespace and hence the error appeared.
Class 'Database\Seeders\DB' not found.
Resolutions:
Remove the namespace and run composer dump-autoload
OR Add a backslash to \DB::table('stock_categories')->([ ... ]); so Laravel starts looking the DB facade from root (not from the namespace specified)
Eg,
\DB::table('MemberInvitation')->insert( [
'id' => 'BlahBlah' ,//com_create_guid(),
'partner_id' => 1,
'fisrt_name' => 'Thats',
'last_name' => 'Me',
'email' => 'me#mymail.com',
'mobile_phone' => '444-342-4234',
'created_at' => new DateTime
] );
If the above solutions doesn't work, try this one.
You may have changed the namespace (by default, it's "App").
What you need to do is to go to the composer.json file and check this:
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
}
},
If the namespace is App like this example, this solution is not for you.
Otherwise, take the namespace you found and insert that line into your seeder class:
use NameSpaceFound\User;
You should include namespace if you want to use a string as a parameter
$this->call('Database\Seeders\MemberInvitationSeeder');
After making php artisan make:seed SeederName
then you need to run composer dump-autoload command after that
php artisan db:seed It is working for me.
I was facing this problem with laravel package with spatie
to solve this problem just
1- append seeders path in composer.json of your package
"autoload": {
"psr-4": {
...
"{packagename}\\{vendorname}\\Database\\Seeders\\": "database/seeders"
}
},
then go to your main project and update composer with composer update
auto-load your classess with composer dump-autoload
If it doesn't work delete Vendor folder and run composer install