making model and migration with a single command in laravel 5.2 - laravel

I've tried this artisan command as I followed a tutorial
php artisan make:model Foo -m
but I get this error:
exception 'RuntimeException' with message 'The "-m" option does not exist.'
why It's not recognizing that?
If It's a wrong way to do It , what the right one?

In short, this is how you do it.
C:\xampp\htdocs\lms>php artisan make:model Test -m
Model created successfully.
Created Migration: 2016_08_29_160434_create_tests_table
It must work. If that does not, do.
C:\xampp\htdocs\lms>composer install.
and that should work.

Related

Adding column using make migration command error

I'm trying to add an icon_path column to an existing table called tbl_device
by using php artisan make:migration add_icon_path_to_tbl_device_table --table=tbl_device
and after running php artisan migrate, it gives me this error.
PHP Fatal error: Cannot declare class CreateFailedJobsTable, because the name is already in use in ...path\database\migrations\<date>_create_failed_jobs_table.php
Cannot declare class CreateFailedJobsTable, because the name is already in use
I've also tried manually adding the icon_path column to the create_tbl_device_table.php migration and after running php artisan migrate it says Nothing to migrate.
I think I followed all the instructions.. any idea where I went wrong?
Call artisan migrate command only for your specific migration using:
php artisan migrate --path=/database/migrations/my_migrations
And see if it works.

Laravel Tinker error with SQLite database

Starting from a fresh project:
laravel new new-project
cd new-project
touch storage/database/database.sqlite
Then at .env
DB_CONNECTION=sqlite
DB_DATABASE=storage/database/database.sqlite
DB_FOREIGN_KEYS=true
The migration succeed...
php artisan migrate
php artisan tinker
>>>App\User::all()
But when I try to get all users it returns the following error:
PHP Fatal error: Class 'App/User' not found in Psy Shell code on line 1
What I could be missing?
Recently I reinstalled Laravel...
And I didn't realized I was using version Laravel 8
the models are at /app/Models
So the right command would be:
>>>App\Models\User::all()
i think it has something to do with the namespace alias. Try without the App/
Users::all();

Calling passport:client from route, "there are no commands defined in the "passport" namespace"

I have an issue where the passport namespace is not available when I call passport:client. I set up a brand new Laravel project with version 5.6.34 where I installed Laravel passport according to the documentation. I also checked a similar question and followed all steps there with no sucess.
Calling php artisan passport:client --password --name="Test" from the command line works without any issues and I can see the client in the database.
However if I create a route in routes/web.php like so:
<?php
use Illuminate\Support\Facades\Route;
use \Illuminate\Support\Facades\Artisan;
Route::get('/', function () {
Artisan::call('passport:client', [
'--password' => true,
'--name' => "Test client",
'--quiet' => true,
]);
});
and use the Artisan facade to call the command I get the error below when I navigate to http://homestead.test/.
Symfony \ Component \ Console \ Exception \ CommandNotFoundException
The command "passport:client" does not exist.
I have added Laravel\Passport\PassportServiceProvider::class in the providers array in config/app.php.
How can I call the passport:client command from a web route?
We have a Laravel application that has several hundred database connections which are handled dynamically. In the admin interface you can create a new project and thus a new database where we need to generate a client.
My other option is the instantiate a ClientRepository and call the createPasswordGrantClient() method but I would like to avoid this if possible. I am curious to know why the namespace is available when called on the CLI but not via a request. I can call other commands such as migrate:fresh via a request but passport:client will fail even if it is part of the migration or inside another command.
According to this link, I ran the below commands.
composer update
php artisan config:cache
php artisan migrate
php artisan passport:install
This helped me.

Error while migrating from Laravel tinker console (sqlite)

Consider
laravel new bug
Then adding in .env
DB_CONNECTION=sqlite
DB_DATABASE=/home/me/Code/bug/storage/database.sqlite
creating database
touch /home/me/Code/bug/storage/database.sqlite
migrating
php artisan migrate && php artisan migrate:fresh
Now trying to migrate programmatically in tinker
php artisan tinker
>> Artisan::call('migrate');
First time resulting in => 0 (as expected ?) But second time:
>>> Artisan::call('migrate:fresh')
Illuminate\Database\QueryException with message 'SQLSTATE[HY000]:
General error: 17 database schema has changed (SQL: select * from
sqlite_master where type = 'table' and name = migrations)'
Artisan::call('migrate')
Third time:
Symfony\Component\Console\Exception\CommandNotFoundException with
message 'There are no commands defined in the "migrate" namespace.'
Any ideas whats going on here? Can you reproduce?
Update using postgres. The second time running Artisan::call('migrate:fresh') I get:
Symfony\Component\Console\Exception\CommandNotFoundException with
message 'There are no commands defined in the "migrate" namespace.'
UPDATE 2: Issue filed here: https://github.com/laravel/framework/issues/22997
https://github.com/laravel/tinker/issues/37
I don't think if this is a laravel bug, but I found your problem:
Calling a non existing command like Artisan::call('migrate:fresh') will crash. The reason is a typo: tinker does not accept fresh, only refresh. Calling the command via tinker will empty the sqllite file so you can't execute any commands until you migrate without tinker.
Here are my steps:
php artisan migrate # sqllite file is filled with content
php artisan tinker
> Artisan:call('refresh')
> Artisan::call('fresh') # now the sqllite file is empty
> Artisan::call('migrate') # will crash
php artisan migrate
> Artisan::('refresh') # works
so I think it's a tinker bug, not a laravel one. Tinker is doing some kind of snapshot (If you change a Model you have to restart tinker to call it there), maybe the error handling is not implemented is the best way to catch all errors.

How to use artisan to make views in laravel 5.1

I have been looking in the docs for a solution to make views with basic CURD operations but without much success.
I guess this might be pretty simple, but am missing something or not looking hard enough.
i can make models and controllers using the below artisan commands
php artisan make:model modelName
php artisan make:controller controllerName
But how do we make the basic CURD views. something like
php artisan make:views
cant find any doc for this. Please help
At the time of writing, there isn't a way to create views via artisan without writing your own command or using a third party package. You could write your own as already suggested or use sven/artisan-view.
if you are using laravel 5.1 or 5.2 this gist can help you make:view command just create command copy and paste the code from gist.
Step 1:
php artisan make:command MakeViewCommand
Step 2:
copy class from this gist
https://gist.github.com/umefarooq/ebc617dbf88260db1448
Laravel officially doesn't have any Artisan cammands for views.
But you could add third party plugins like Artisan View
Here's the link Artisan View
After adding this plugin to your project by the guide provided here you should be able to perform following cammands :
Create a view 'index.blade.php' in the default directory
$ php artisan make:view index
Create a view 'index.blade.php' in a subdirectory ('pages')
$ php artisan make:view pages.index
Create a view with a different file extension ('index.html')
$ php artisan make:view index --extension=html
There is very easy way to create a view(blade) file with php artisan make:view {view-name} command using Laravel More Command Package.
First Install Laravel More Command
composer require theanik/laravel-more-command --dev
Then Run
php artisan make:view {view-name}
For example
It create index.blade.php in resource/views directory
php artisan make:view index
It create index.blade.php in resource/views/user directory
php artisan make:view user/index
Thank you.
In v5.4 you need to create the command with:
php artisan make:command MakeView
and before you can use it, it must be registered in App/Console/Kernel like
protected $commands = [
Commands\MakeView::class
];
then you make a view like: php artisan make:view posts/create
To create a view (blade) file through command in laravel 8:
composer require theanik/laravel-more-command --dev
php artisan make:view abc.blade.php
You can install sven/artisan-view package to make view from CMD, to install package write this command:
composer require sven/artisan-view --dev
After installing it, you can make a single view or folder with all views that contain {index-create-update-show}
To make a single file we using this command:
php artisan make:view Name_of_view
For example
php artisan make:view index
To make a folder that contain all resources index - create - update - show write name of folder that contain all this files for example:
php artisan make:view Name_of_Folder -r
For example:
php artisan make:view blog -r
-r is a shorthand for --resource you can write full name or shorthand to make resource.
you can extend yields from master page if master page inside in directory layouts we write command sith this format
php artisan make:view index --extends=layouts.master --with-yields
layouts is a directory this directory may be with a different name in your project the idea is name_of_folder/master_page that you want to extend yields from it.
For more view docs

Resources