Laravel: Set timestamp on database connection laravel mysql - laravel-5

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');
}
}

Related

How to create table in laravel without created_at and Updated_at column using migration

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;

Failing to create new migration

I want to create a new table, users_profile
here is the migration code
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersProfileTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('users_profile', function (Blueprint $table) {
$table->increments('id');
$table->string('city');
$table->string('country');
$table->integer('users_id')->unsigned();
$table->foreign('users_id')
->references('id')
->on('users')
->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('users_profile');
}
}
facing an error
base table or view already exists: 1050 Table "users" already exists
here is the migration code of users
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('first_name');
$table->string('last_name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
Although i am creating a new migration, a new table only i am declaring a primary key of users table as foreign key in users_profiles but facing the above error
help please!
Just a word of advice, Use Laravel Nomenclature to avoid these kind of issues. Always define your table names as plural snakecase and use singular camelcase model to access them.
In your case, change your table name to: user_profiles and use UserProfile model to access it automatically.
Ofocurse you need to change your class name in your migration accordingly: CreateUserProfilesTable
I believe it should resolve your issue.
I believe you are using : php artisan migrate and as users table already exists , running migration for users table again gives that error.
Use: composer dump-autoload and then php artisan migrate:refresh
to rollback all of your migrations and reinstall migrations.
Simple go to your database and drop Schema table ("users") and also remove entry in Schema table ("migrations") and run your migrations again...your problem is solve....

How to get the login log in Laravel 5.2

I am using Laravel 5.2, I would want to record user's login log, including user id,ip address and timestamps. what should I do?
//migration code
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class LoginHistory extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('login_history', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->string('ip')->default(false);
$table->timestamps();
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('login_history');
}
}
I am using Laravel 5.2, I would want to record user's login log, including user id,ip address and timestamps. what should I do?

Laravel migrations: dropping a specific table

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

Setting Default Value for Primary Key in Laravel Migration for sqlite

I've created the database migration below. What I'd like to do is create an accounts table with an id as a primary key. However, I don't want the key to autoincrement starting at 1. Rather, I'd like it to autoincrement starting at 800500.
Is there a way to set the default value of a primary key like this?
I'm currently using Laravel v4.2.11 and sqlite v3.8.3.
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateAccountsTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('accounts', function(Blueprint $table)
{
$table->increments('id')->unsigned()->default(800500);
$table->string('name', 100);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('accounts');
}
}
The default method in the schema builder
Declare(s) a default value for a column
If you need the increment to start at a given value take a look at this answer.
You'd add the query to the migration:
public function up()
{
Schema::create('accounts', function(Blueprint $table)
{
$table->increments('id');
$table->string('name', 100);
$table->timestamps();
});
DB::statement("UPDATE SQLITE_SEQUENCE SET seq = 800500 WHERE name = 'accounts'");
}
There is no 'laravel' way to do this.

Resources