I'm new to using Laravel.
I have a .sql file where created a whole bunch of tables("CREATE TABLE my_table......."). Is there a way to somehow import these statements into Laravel? I could always manually rewrite these tables in raw php, but I feel that this would take too long and there is possibly an easier way to do it.
You would use migrations in Laravel to do this for example
I want to create a table called tasks I would use this command in artisan
php artisan make:model Task
This would create a model called task and a migration for this table.
You can then access the migration file and input something like this
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTasksTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('tasks', function (Blueprint $table) {
$table->increments('task_id');
$table->string('name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('tasks');
}
}
You would then run the artisan command to create this table
php artisan migrate
This then automatically creates your table.
Its a very useful feature and you can also rollback your migrations or even refesh it meaning it will remove the table and then reinstate it with no data in.
php artisan migrate:refresh
link to the docs
http://laravel.com/docs/5.1/migrations
Related
I'm developing a web using Laravel 9. I used the command sail php make:migration add_bought_to_products_table to add a boolean column called "bought" in a products table. When trying to modify the value using Eloquent helpers (Product::where('id', $product->id)->update(array('bought'=>true)) the value is not updated in the database. When looking at it, I se that the new field "bought" created by the migration is marked as Read-only: No corresponding table column.
The migration code is the following:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('products', function (Blueprint $table) {
$table->boolean('bought');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('products', function (Blueprint $table) {
$table->dropColumn('bought');
});
}
};
Here a screenshot of the database:
I have already tried to clean the cache and rebuild many times the database doing a rollback and migrating again. The curious thing is that I previously added the field "visibility" which works perfectly with the exact same code and steps as the field that is giving the problem.
How can I solve it?
After breaking my head a lot, I solved it by simply doing a clean restart of the docker containers. It seems that the issue had nothing to do with Laravel but with Docker!
For anyone experiencing similar issues: make sure to end Docker and all containers and do a clean restart.
I simply restarted my database tool and it was gone.
Iam trying to migrate Laravel table migrations into the database but every time I make new tables and run php artisan migrate Laravel complains that user table exists and even the newly added migrations are not created in the database, and when I run php artisan migrate:fresh I lose my data is there a better way to do this without losing my data in the already existing tables. Below is the table i want to add.
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up() {
Schema::create('drivers', function (Blueprint $table) {
$table->id();
$table->string('name')->nullable();
$table->string('email')->unique();
$table->string('contact')->unique()->nullable();
$table->string('code')->unique();
$table->string('nin')->nullable();
$table->date('birthday')->nullable();
$table->string('image')->nullable();
$table->bigInteger('country_id')->nullable();
$table->boolean('verified')->nullable()->default(false);
$table->timestamps();
$table->foreign('country_id')->references('id')->on('countries');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down() {
Schema::dropIfExists('drivers');
}
}
Is it perhaps saying that the Class CreateUsersTable already exists?
I can see that the class name is CreateUsersTable, but you should probably rename to CreateDriversTable
I`m running a new installation of Laravel, with clean database and files.
I created a table named "frooth" and it has the columns id, title and created_at (id PK, varchar and datetime)
When I run "php artisan make:migration frooth" command, the created migration file is empty, only containing the up() and down() functions and nothing more (no columns)
How can I solve this, I followed the basic configuration of the framework as documented in official website, I can access and create functions in artisan as expected, only migrations its not working.
I generated the project with the command: composer create-project --prefer-dist laravel/laravel blog
create table laravel.frooth
(
id int auto_increment
primary key,
title varchar(250) null,
created_at datetime null
);
The PHP class generated in database/migrations/2019_10_25_012925_frooth.php:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class Frooth extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
//
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
//
}
}
Console output:
php artisan make:migration frooth
Created Migration: 2019_10_25_012925_frooth
Delete that table you made manually and delete that migration file.
Run php artisan make:migration create_frooths_table
Then add your columns to the new migration file.
Something similar to this:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class Frooth extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('frooths', function (Blueprint $table) {
$table->increments('id');
$table->string('title')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('frooths');
}
}
Then run php artisan migrate
For the id you might need to use $table->bigIncrements('id'); if using Laravel 6
I have a problem with migrations in a Laravel 5.6.
This is a problem:
My code in a Laravel:
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCompaniesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('companies', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('user_id');
$table->foreign('user_id')->references('id')->on('users');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('companies');
}
}
What i need to do?
Table already exists as it is telling you in the message, you may need to drop it first or check that you're using the right database name in your ENV file. Also check you're not creating the table companies in a previous migration.
If you're trying to add fields to an existing table you don't need the create method but:
Schema::table("companies", function (Blueprint $table) {
// The fields you need
});
Also for the foreign key is safer to use unsignedInteger as data type
As shown in the error message "companies" table already exist.
you can use following commands to resolve it.
php artisan migrate:rollback
php artisan migrate:reset
please refer the laravel documentation for more details.
You can use below command to drop every table and migrate before that you have to take a backup of your sql because it deletes every data
php artisan migrate:fresh
i have just started working with laravel 5.2.. this is a simple migration file but when i run the php artisan migrate command i get the error shown on the screenshot. what should i do now?
migration file
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateProductCategoryTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('product_category', function (Blueprint $table) {
$table->increments('id');
$table->string('product_category_name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('product_category');
}
}
You need to change Schema::table to Schema::create.
For me, it was because i was dynamically registering Scheduled Tasks in Kernel.php via my own custom Task object by loop over Task::all(). But my 'task' db table wasn't created yet causing the error. so I commented out the Kernel.php code, ran the php artisan migrate command then uncommented my Kernel.php code.
Not sure, but may be best to just do a try{ dbCode; } catch{doNothing;} in the Kernel.php code.