I want to create a table by migration with name "blogOST".
But Laravel create a table is "blogpost".
In Laravel, how to use migration to create table name with camelCase format.
This is my migration code
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateOrderDetailTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('blogPost', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title', 100);
$table->dateTime('createdAt');
$table->dateTime('updatedAt');
$table->dateTime('deletedAt')->nullable();
$table->charset = 'utf8';
$table->collation = 'utf8_general_ci';
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('orderdetail');
}
}
Mostly its a common way to usesnake casefor table names and column names, Not onlyLaravel` but also other languages uses same practice.
In Laravel Eloquent Model automatically maps to lower case plural table name. You can explicitly mention different table name if you want using protected $table = 'my_table_name';
So its Laravels default behavior.
Related
Can anyone know how to create Table in laravel without created_at and Updated_at Column in table using a migration.
I try this Following code but it's still create created_at and Updated_at column in table:-
Migration Laravel :-
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateNewusersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('newusers', function (Blueprint $table) {
$table->bigIncrements('id');
$table->text('name');
$table->string('email', 100)->unique();
$table->bigInteger('phone_no');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('newusers');
}
}
Add this in your model
public $timestamps = false;
I am new at Laravel and I am wondering how actually Laravel determines default foreign key name.
According to the Laravel document, it says "Eloquent determines the default foreign key name by examining the name of the relationship method and suffixing the method name with _id".
Then, in this case↓, the default foreign key supposed to be named as info_id as the document says.
app/Models/Item.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Item extends Model
{
public function itemInfo()
{
return $this->hasOne(‘App\Models\ItemInfo’);
}
}
I just changed the name of infofunction as infooo THEN, no error happened. It do works...
What I expected was an error occurs because I thought Laravel would name the default foreign key as infooo_id and there is not such a column name in the tables.
It means Laravel does not determine the default foreign key name by the method name, doesn`t it?
Then what portion actually Laravel determine the default foreign key from?
These are the migration files of tables:
database/YYYY_MM_dd_hhiiss_create_items_table.php
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateItemsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create(‘items’, function (Blueprint $table) {
$table->increments(‘id’);
$table->string(‘name’);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists(‘items’);
}
}
YYYY_mm_dd_hhiiss_create_item_infos_table.php
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateItemInfosTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create(‘item_infos’, function (Blueprint $table) {
$table->increments(‘id’);
$table->integer(‘item_id’)->unsigned();
$table->string(‘genre’);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists(‘item_infos’);
}
}
Is there any way/laravel-command to drop a specific table from the production server?
Set up a migration.
Run this command to set up a migration:
php artisan make:migration drop_my_table
Then you can structure your migration like this:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class DropMyTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
// drop the table
Schema::dropIfExists('my_table');
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
// create the table
Schema::create('my_table', function (Blueprint $table) {
$table->increments('id');
// .. other columns
$table->timestamps();
});
}
}
You can of course just drop and not check for existence:
Schema::drop('my_table');
Read further in the docs here:
https://laravel.com/docs/5.2/migrations#writing-migrations
You may also have to consider dropping any existing foreign keys/indexes, for example if you wanted to drop a primary key:
public function up()
{
Schema::table('my_table', function ($table) {
$table->dropPrimary('my_table_id_primary');
});
Schema::dropIfExists('my_table');
}
More in the docs in dropping indexes etc here:
https://laravel.com/docs/5.2/migrations#dropping-indexes
I would like to understand how to set the timestamp on every connection to the mysql database connection through laravel Is there any configuration which helps in achieving this.
Eloquent automatically updates the updated_at attribute whenever a model is updated. Just add the timestamps to your migration, like this:
$table->timestamps();
created_at and updated_at fields will then be added to your table and eloquent will use them automatically.
Full example from the laravel docs:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateFlightsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('flights', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('airline');
$table->timestamps(); // <<< Adds created_at and updated_at
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('flights');
}
}
https://github.com/Xethron/migrations-generator
I migrated my database structure into Laravel using php artisan migrate:generate command with the help of the extension above. But there's a small problem, my primary key's aren't named as id, I rather used a different convention by adding a prefix for each of them like user_id, product_id, photo_id, etc. All of these are auto incremented, of course.
Here's my current create_users_table.php file inside my migrations folder. I defined user_id to override the default id option, is that the correct use?
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->primary('user_id');
$table->integer('user_id', true);
$table->string('name', 500);
$table->string('email', 500);
$table->string('password', 500);
}
);
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('users');
}
}
I read that I need to add something like below, but I'm not sure where to define protected $primaryKey since my class extends Migration rather than Eloquent.
class CreateUsersTable extends Eloquent {
protected $primaryKey = 'user_id';
}
I'm getting the following error when I go to /auth/login page, which I think causes because of the user_id usage rather than id. How can I fix it?
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.id' in 'where clause' (SQL: select * from `users` where `users`.`id` = 5 limit 1)
You need to specify your non-default primary key in your User model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model {
protected $primaryKey = 'user_id';
You will need to do this for all Models that don't use id as their primary key.