public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('last_name')->nullable();
$table->string('phone_number')->nullable();
$table->text('Biography')->nullable();
$table->string('address')->nullable();
$table->string('photo')->nullable();
$table->string('facebook_link')->nullable();
$table->string('twitter_link')->nullable();
$table->string('youtube_link')->nullable();
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
I would like to changed the user to another name but could not and migration is not working.
If you want to rename the table, you can create a new migration with this code:
Schema::rename('users', 'some_other_table_name');
Then run composer du and php artisan migrate commands.
Or, you could recreate the table by rolling back this migration php artisan migrate:rollback whcih will work if you have this line in the down() method:
Schema::drop('users');
Change the table name:
Schema::create('some_other_table_name', function (Blueprint $table) {
And then run php artisan migrate
https://laravel.com/docs/5.5/migrations
Related
I'm trying to pass the foreign key but am stuck with this error Base table or view already exists: 1050 Table 'favorites' already exists"
how can I pass them all the foreign keys and the normal id like $table->bigInteger('user_id')->unsigned();
tables
public function up()
{
Schema::create('favorites', function (Blueprint $table) {
$table->Increments('id');
$table->bigInteger('user_id')->unsigned();
$table->bigInteger('product_id')->unsigned();
$table->timestamps();
});
Schema::create('favorites', function (Blueprint $table){
$table->bigInteger('user_id')->unsigned()->index();
$table->foreign('user_id')->references('id')->on('users');
$table->bigInteger('product_id')->unsigned()->index();
$table->foreign('product_id')->references('id')->on('products');
});
}
public function down()
{
Schema::dropIfExists('favorites');
}
You're migrating the table twice, pass the foreign keys in the same migration
public function up()
{
Schema::create('favorites', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->bigInteger('product_id')->unsigned();
$table->foreign('product_id')->references('id')->on('products');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('favorites');
}
Hope this helps
Other answers are correct. If ever you've missed the needed tasks before doing it, kindly try this one
1) Remove or delete the table you've migrated
2) In migrations table, delete the record which consists the name "favorites"
3) Copy and paste thi code inside the "up" function with this one
Schema::create('favorites', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('user_id')->unsigned();
$table->bigInteger('product_id')->unsigned();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('product_id')->references('id')->on('products');
});
4) Run this command again
php artisan migrate
I have this migration:
public function up()
{
Schema::create('clients', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->string('slug');
$table->timestamps();
});
}
And now I want to add a new column, and looks like this:
public function up()
{
Schema::create('clients', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->string('slug');
$table->string('pathlogo')->default('/');
$table->timestamps();
});
}
How can I make just an 'add column' on Laravel? I don't want to do php artisan migrate:refresh, or restart and then make again seed.
Now I have some data in DB which not exist on seed I just want to make a new column.
You need to change Schema::create to Schema::table (because you are not creating a table, just selecting it), and then your only line in that function should be:
$table->string('pathlogo')->default('/')->after('slug');
after will ensure the column is positioned how you want.
If you are still in development, ie. you don't have data in the table, you should just rollback all your migrations and edit the original.
Create a new migration and Use that as your up function:
public function up()
{
Schema::table('clients', function (Blueprint $table) {
$table->string('pathlogo')->default('/');
});
}
them migrate as normal.
I'm new to Laravel 5.0.
I'm trying to make a hasmany relationship between my two models, User and Device.
If in tinker I do App\user::first()->device the result is null
I populated the users and devices tables with some dummy data and the single find() work.
I searched on StackOverflow but I could't find out a solution
Here's my users table
public function up()
{
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
}
my devices table
public function up()
{
Schema::create('devices', function(Blueprint $table)
{
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->double('lat', 15, 8);
$table->double('lon', 15, 8);
$table->timestamps();
});
}
and my function in the User Model
public function device()
{
return $this->hasMany('App\Device');
}
Thank you all
The code was right, I found out sometimes tinker needs to be rebooted after the changes in the code
I'm trying to migrate my database to an empty database,
but I get the following error when I do php artisan migrate:
[Illuminate\Database\QueryException]
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'portal-for-fun.permissions' doesn't exist (SQL: select *
from `permissions`)
[PDOException]
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'portal-for-fun.permissions' doesn't exist
All migrations can be found here: https://github.com/cskiwi/portal-for-fun/tree/master/database
but I think the problem lies here:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateRolesTables extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up() {
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('label')->nullable();
$table->integer('role_id')->unsigned()->nullable();
$table->timestamps();
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
});
Schema::create('permissions', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('label')->nullable();
$table->timestamps();
});
Schema::create('permission_role', function (Blueprint $table) {
$table->integer('permission_id')->unsigned();
$table->integer('role_id')->unsigned();
$table->foreign('permission_id')->references('id')->on('permissions')->onDelete('cascade');
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
$table->primary(['permission_id', 'role_id']);
});
Schema::create('role_user', function (Blueprint $table) {
$table->integer('role_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->primary(['role_id', 'user_id']);
});
Schema::create('permission_user', function (Blueprint $table) {
$table->integer('permission_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('permission_id')->references('id')->on('permissions')->onDelete('cascade');
$table->primary(['permission_id', 'user_id']);
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down() {
Schema::dropIfExists('permission_user');
Schema::dropIfExists('role_user');
Schema::dropIfExists('permission_role');
Schema::dropIfExists('permissions');
Schema::dropIfExists('roles');
}
}
the database itself only contains an empty migration table
Create permission model in your project.
If you have permission model make sure that name in permission not Permission
I've had similar problem. Try to seaprate migrations and move part of them (those which use permissions table) from migrations folder. First, you should run migration which will create permissions table with php artisan migrate. Then move back migrations which create permission_role and permission_user tables to the migrations folder and run php artisan migrate again.
That helped me, hope it'll help you too.
I am new to Laravel PHP framework. I am confused with how Laravel schema builder and migrations work. I created two tables using Laravel migrations. Below are my two tables:
public function up()
{
Schema::create('hotels', function(Blueprint $table)
{
$table->increments('id');
$table->integer('hotel_id');
$table->string('name');
$table->string('address')->nullable();
$table->string('city')->nullable();
$table->string('province')->nullable();
$table->string('country')->nullable();
$table->timestamps();
});
}
This is my other table:
public function up()
{
Schema::create('rooms', function(Blueprint $table)
{
$table->increments('id');
$table->integer('hotel_id');
$table->string('name');
$table->timestamps();
});
}
My question is how can can I make relations with two tables using Foreign keys? If I have to use Schema Builder where do I have to put the schema file?
You can do it in the very same migrations, using Schema Builder. This is how you create a foreign key using migrations:
public function up()
{
Schema::create('rooms', function(Blueprint $table)
{
$table->increments('id');
$table->integer('hotel_id');
$table->string('name');
$table->foreign('hotel_id')
->references('id')
->on('hotels')
->onDelete('cascade');
$table->timestamps();
});
}