Create Migration using command - laravel

guys.
I tried creating a migration, model and controller using the php artisan commands and it was all created, but with wrong permissions: if I want to modify something and then save it in these new files, I need to save using sudo, which is a waste of time. I'd like to know if there's a way to change those permissions when creating the files. The docker is already outside sudo.
Thanks a lot.

Run php artisan command with the user you will use to access this files later or change the permissions for the files after you create them.

Related

Change default directory for creating migration in Laravel

I want to change the default directory for creating migration i.e. If I do php artisan make:migration create_users_table, it should create users table in /database/migrations/child_database. I do not want to use --path every time I create a migration for child_database. I want to change the default directory for php artisan make:migration.
you can create your own artisan command to achieve this:
create your a new artisan command and use it to make migration in your custom directory using this article. hope help you ^_^
try this package github repo or use laracast link
You can use loadMigrationsFrom() in your AppServiceProvider.
Inside the boot() function, run:
/**
* Register Custom Migration Paths
*/
$this->loadMigrationsFrom([
database_path().DIRECTORY_SEPARATOR.'migrations'.DIRECTORY_SEPARATOR.'folder1',
database_path().DIRECTORY_SEPARATOR.'migrations'.DIRECTORY_SEPARATOR.'folder2',
]);/
This question has been answered before, even though it's a little bit old it still has it's value.
Have a look at: https://stackoverflow.com/a/35895106
If you use this answer, the default directory is changed and you will have the solution you want.

Laravel configuration usage for different servers in pure php file

I am new in Laravel and want to know such problem
I am running an web application on several server using Laravel.
But I have encountered with an issue.
When there is modification for the project, I need to sync with git on several servers.
But it has different settings for each server (eg: DB name, DB password...)
I have set it manually because I couldn't use .env or configuration file since the file is just pure php file.
The issue I want to solve is how can I get Laravel configuration data from pure PHP file(not controller or whatever).
It will be thankful if someone teach me solution.
You asked:
how can I get Laravel configuration data from pure PHP file
The answer would be this:
You just make some file in config/ folder of laravel app (or use the existing file like config/app.php)
You make an array of your key value pairs
<?php
return [
'some_key' => 'some_value',
...
and you simply call it with this code where ever you need it:
config('config_file.key');
for example
config('app.name');
would give you Laravel by default.

Laravel Cashier - publish migration results in "cannot declare class CreateCustomersColumns"

I have a fresh Laravel installation and I've added Cashier to my project.
Since the Users model on my app won't have a stripe connection, but rather an Accounts model, I need to alter their migration to add columns to Accounts instead of Users
The documentation says to run:
php artisan vendor:publish --tag="cashier-migrations"
which adds the two migration files to database/migrations
From there I can change users to accounts in the migration file.
When I try to run php artisan migrate, I get:
Whoops\Exception\ErrorException : Cannot declare class CreateCustomerColumns, because the name is already in use
This problem only goes away when I delete the migration files, but then the new columns are added to users.
The documentation states that you can disable their migration files by putting Cashier::ignoreMigrations(); in AppServiceProvider
I didn't realize that's what I wanted to do. I thought the publish command only published the two files I needed to edit, however, those are the only migration files that come with Cashier.
Be sure to add Cashier::ignoreMigrations(); in the register method.
And add use Laravel\Cashier\Cashier;
The file (and thus the class) must exist twice, probably in your database/migrations directory, most likely with 2 different datetime prefixes on the filenames.

how to create migration from existing database in laravel

I am new to laravel. i know command to create database from laravel migration.
I have already created database in mysql. so how can i convert migration from that database.
You could use Jeffrey Way's generator tool for Laravel 4, and do them by hand. It is a very useful tool. https://github.com/JeffreyWay/Laravel-4-Generators (if you are using Laravel 5, use this package instead: https://github.com/laracasts/Laravel-5-Generators-Extended)
Or you could try out one of the following packages, they should convert your existing schema to Laravel migrations. I have not tried them, but have a look:
https://github.com/adamkearsley/laravel-convert-migrations
https://github.com/barryvdh/laravel-migration-generator
Also, read the following answer in another question, might be useful to you or others: Reverse engineering or Auto-Generations of Entities in Laravel?
For modern versions of Laravel you should probably use a package like https://github.com/kitloong/laravel-migrations-generator
I quick wrote this python script to make migration files out of an existing database.
https://github.com/aljorhythm/sql-to-laravel-migrations
After set up, just do
python retrieve-and-convert.py
If the accepted answer (at the time of editing this post) does not work for you, use this simple approach.
Check out this package by Kitloong is really easy to use.
https://github.com/kitloong/laravel-migrations-generator
After installation, just run something like this to export the migration to a location of your choice:
php artisan migrate:generate --path="path\to\existing\folder"
Example:
php artisan migrate:generate --path="C:\xampp\htdocs\laravel_bs4\database\migrations\my_new_migrations"
You should end up with migrations created from the database and saved in the location you have specified.
You can omit the --path option and simply run
php artisan migrate:generate
This would export it to the default Laravel's migration folder (which is something I usually do not want).
Be sure to follow the prompts. Sticking to the default is usually okay.
How about this?
Migrations Generator.It's for Laravel, 4, 5, 6, 7, but I don't know if it is for Laravel 8
Simple solution, to use online Laravel migration generator for your existing SQL table schema without installing any package. Just enter your SQL schema and get Laravel migration file.
Try: https://laravelarticle.com/laravel-migration-generator-online
If you don't mind using Doctrine 2 in your project instead of Eloquent that has reverse engineering built in.
For laravel 9 follow below link
https://laravel.com/docs/9.x/migrations#generating-migrations

Codeigniter bootstrap for PHP CLI

I am developing a PHP CLI tool, which I want to bootstrap with CodeIgniter to be able to use some of it's libraries, like database and accessing configuration.
So basically I want to have a CLI script, that would allow me to load up the CodeIgniter environment and then do $CI->db->stuff().
I know that I could create a controller for this but I am looking for a way to make it CodeIgniter-installation-independent, so that I could simply $ cd into a dir, load it's CI environment and perform magic via command line. Another reason why I don't want to use a controller is that I don't want it to be accessible to public via address bar.
So, any ideas?
Command line capabilities are baked into the latest version now. Enjoy:
http://codeigniter.com/user_guide/general/cli.html

Resources