Getting the name of the columns added by artisan Laravel - php

I run the Artisan command in the code like
Artisan::call('migrate', ['--path' => 'database/migrations/core']);
I Need to know the columns added by that batch of migration which ran recently. I tried using Artisan::output(); command but it returns the migration name alone.
Is there any way to get the column names of the migration batch or to extract the column name from the migration name?

You can analyze the Laravel query log:
DB::enableQueryLog();
Artisan::call('migrate', ['--path' => 'database/migrations/core']);
$log = DB::getQueryLog();
dd($log);

Related

Adding column in existing table without using migration in Laravel

Is there any method to adding column in existing table without using migration. I mean executing php artisan migrate and creating migration file through program/code in Laravel rather than CLI. Thank you.
Migration files are only an easy way to create tables and other db actions, you still can use raw queries on Laravel anywhere by using DB::raw(...); check its documentation (https://laravel.com/docs/5.8/queries) to proper use it.
You may also be able to write a Schema::table(,,,) anywhere on your code, it will return some a Schema object which you can execute some method to run the code on your apo runtime, if you are using some IDE just try to explore and see what will you have by typing Schema::table(...)->
TRy this add just coloumn in your migration table
public function up()
{
Schema::table('users', function($table) {
$table->string('name');// only add this line
});
}
php artisan migrate:refresh

Laravel - add fields to table

I use Laravel 5.6, I come from Symfony development and Doctrine ORM.
What is the way to add fields to a table ? I want to add some fields to the user table. So I created a migration :
php artisan make:migration add_data_users --table=users
And I added the fields in the migration. But when I try to migrate, I got this error :
Base table or view already exists: 1050 Table 'permissions' already exists
This table was already migrated, but it seems that migration rebuild all schemas (?!). In Symfony, I only add fields to the Entity, here, do I have to create a migration ? and then, how to apply only the new migration ? I tried to add the name of the migration to the command, but it doesn't work.
Edit
It seemed that I had 2 migrations concerning the permissions table, just look at your migrations carefuly, I deleted the second migration file, and it worked.
You should create new migration like:
php artisan make:migration update_users_table
UpdateUsersTable.php //your new migration
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class UpdateUsersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('name', 36)->nullable()->default(null);
});
}
Note: Please run migration for specific table
After create test folder in migrations folder then newly created migration moved/copied in test folder and run below command in your terminal/cmd like:
php artisan migrate --path=database/migrations/test/

Laravel seeding via SQL file

I'm trying to read data from a .sql file in a seeder to fill 3-4 tables with some data and DatabaseSeeder.php looks like this
public function run() {
$this->call([
UsersTableSeeder::class,
// Bunch of seeders using Eloquent
SqlSeeder::class
]);
}
All other seeders execute and, actually, when trying to throw an exception in SqlSeeder.php I'm able to stop the seeding. However, SqlSeeder.php won't seed the database via php artisan migrate:fresh --seed, seems like it's bypassed. I always need to run php artisan db:seed --class SqlSeeder after, in order to make it seed the database. SqlSeeder.php looks like this
public function run() {
$path = base_path().'/database/seeds/sql/data.sql';
$sql = file_get_contents($path);
DB::unprepared($sql);
}
Why's that?
I solved my own issue by removing transactions from the .sql file I was trying to execute via DB::unprepared(). Oddly enough, transactions completely fail when executing php artisan migrate:refresh --seed, but they work if I later call the SqlSeeder individually via php artisan db:seed --class SqlSeeder. There are no foreign key constraints for now and InnoDB was chosen as engine, just to be sure, but still transactions both fail and work depending on the command.
I guess it all depends on how Illuminate\Database\Seeder::call works and calls seeder classes internally, but I'm not sure.
Check if seeding data in sql file is in right order.
For example if you have foreign key category_id that references to categories.id posts table will be empty without any errors when you use this sql file:
INSERT INTO posts (title, category_id) VALUES ('test', 1);
INSERT INTO categories (title) VALUES ('category');
You should seed categories first and only then posts.
For Laravel 5 all you have to do inside of your main seed file is:
\DB::unprepared(\File::get(base_path('path/to/your/sql/file.sql')));

Laravel Eloquent model with sqlsrv is changing the table names

I'm new to laravel and have a problem with the model created with php artisan make:model [Name]
In my case i connect to the sqlsrv. The connection is established.
my route looks like this:
Route::get('/tasks', function () {
// $tasks = DB::table('WebShops')->get(); //This is working
$tasks = App\WebShops::all(); //This is not working
dd($tasks);
});
my datatable:
Webshops
For this i created a model to connect to the database. php artisan make:model Webshops.
When i access the db with the command $tasks = App\WebShops::all(); i'm getting an error:
SQLSTATE[42S02]: [Microsoft][ODBC Driver 11 for SQL Server][SQL
Server]Invalid Object Name "web_shops". (SQL: select * from
[web_shops]).
The problem is that ma table name is different. The query is searching where the table is web_shops and my table name is Webshop. I don't understand it.
the name in the db not write. you can't write 2 words behind each other like that als db name allows plural.
to avoid such problem you when you make model you can write like that
php artisan make:model Wenshop -c -m -r
when you write like that Laravel make for you Model+ Migration+ Controller with the right names

CakePHP Migration Script doesn't update the Table Model

I need to add an admin column to my user table in my database. I created the migration script with the following command.
bin/cake bake migration AddAdminToUsers admin:boolean
This mostly did what I wanted, I just changed the default value to false. My Migration script now looks like this.
<?php
use Migrations\AbstractMigration;
class AddAdminToUsers extends AbstractMigration
{
public function change()
{
$table = $this->table('users');
$table->addColumn('admin', 'boolean', [
'default' => false,
'null' => false
]);
$table->update();
}
}
Also, oddly enough, I've tried this several times and each time I'm only able to run this migration script once. I have to delete it and re-bake a new one if I want another one to work.
When you run a migration it marks as migrated and you can not run it one more time unless do the rollback. Rollback will cancel previous migration and you will be able to run it one more time.Here is fully docs for plugin that cakphp using for migrations.

Categories