Some tips and tricks in Laravel
Suresh Raj Bhattarai
1:39 PM
1. Generating migration code with additional features
If you want to create migration code with additional functionality (codes for auto incrementing ids, timestamps which includes updated_at and created_at columns automatically. Plus it also adds drop statement for dropping the table in down() function), then use the following lines of code in your command prompt.
C:\wamp\www\laravelproject> php artisan migrate:make create_users_table --create=users
Here we have added extra code --create=users
This will create up() function like this
public function up()
{
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->timestamps();
});
}
Now you can add your choice of columns to it. Here I added two extra columns for unique email and for password
public function up()
{
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->string('email')->unique();
$table->string('password',60);
$table->timestamps();
});
}
2. If you want to alter one of the columns in the table.
Let's say we want to add a column called "permission" to the table users.
C:\wamp\www\laravelproject> php artisan migrate:make add_permission_column_to_users_table
Add the following code to the up() and down() functions
public function up()
{
Schema::table('users', function($table){
$table->integer('permission');
});
}
public function down()
{
Schema::table('users', function($table){
$table->dropColumn('permission');
});
}
and run the migration code as
C:\wamp\www\laravelproject> php artisan migrate
3. If you face problems while running migration like
[PDOException]
SQLSTATE[HY000] [1045] Acc refus..............
In this case, it might be an environment issue.
To solve this run the code like this (if you are working on development environment):
C:\wamp\www\laravelproject> php artisan migrate --env=development
If you are working on production environment, then run like this:
C:\wamp\www\laravelproject> php artisan migrate --env=production
4. If you want to drop all the migration and re-run them, then
C:\wamp\www\laravelproject> php artisan migrate:refresh
5. If the mysql error is being displayed in French language, and you want it to be displayed in English.
Then run the following command going to the following directory
C:\wamp\bin\mysql\mysql5.6.17\bin>mysqld --lc_message=en_US
6. If you have problem in refreshing database migration and if you are receiving the errors like "pdoexception sqlstate 42s02 base table or view not found 1146"
In this case, drop the database and recreate the database and run the migration. It will help you migrate the tables successfully.
7. If you are getting error like "PDOException (1045)
SQLSTATE[HY000] [1045] Access denied for user 'homestead'@'localhost' (using password: YES)"
Go to app > config > local > database.php
There you will see the database homestead which you need to change. I have changed the settings like this and removed the configuration for pgsql (postGreSQL) because I am using MySQL.
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'laravelproject',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
),
8. To access the list of artisan, use this command
C:\wamp\www\laravelproject> php artisan
9. If you find class doesn't exist error (even if the class is present in your controller), then run this command to auto load the classes
C:\wamp\www\laravelproject> php artisan dump-autoload
10. Displaying all sql statements in Eloquent
Plus the following code in routes.php and you will be able to see SQL statements Eloquent is executing. This will help you in better debugging
// Display all SQL executed in Eloquent Event::listen('illuminate.query', function($query) { var_dump($query); });
OR
DB::getQueryLog()
11. Displaying current laravel version
C:\wamp\www\laravelproject> php artisan --version
12. Displaying list of available commands
C:\wamp\www\laravelproject> php artisan list
If you want to create migration code with additional functionality (codes for auto incrementing ids, timestamps which includes updated_at and created_at columns automatically. Plus it also adds drop statement for dropping the table in down() function), then use the following lines of code in your command prompt.
C:\wamp\www\laravelproject> php artisan migrate:make create_users_table --create=users
Here we have added extra code --create=users
This will create up() function like this
public function up()
{
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->timestamps();
});
}
Now you can add your choice of columns to it. Here I added two extra columns for unique email and for password
public function up()
{
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->string('email')->unique();
$table->string('password',60);
$table->timestamps();
});
}
2. If you want to alter one of the columns in the table.
Let's say we want to add a column called "permission" to the table users.
C:\wamp\www\laravelproject> php artisan migrate:make add_permission_column_to_users_table
Add the following code to the up() and down() functions
public function up()
{
Schema::table('users', function($table){
$table->integer('permission');
});
}
public function down()
{
Schema::table('users', function($table){
$table->dropColumn('permission');
});
}
and run the migration code as
C:\wamp\www\laravelproject> php artisan migrate
3. If you face problems while running migration like
[PDOException]
SQLSTATE[HY000] [1045] Acc refus..............
In this case, it might be an environment issue.
To solve this run the code like this (if you are working on development environment):
C:\wamp\www\laravelproject> php artisan migrate --env=development
If you are working on production environment, then run like this:
C:\wamp\www\laravelproject> php artisan migrate --env=production
4. If you want to drop all the migration and re-run them, then
C:\wamp\www\laravelproject> php artisan migrate:refresh
5. If the mysql error is being displayed in French language, and you want it to be displayed in English.
Then run the following command going to the following directory
C:\wamp\bin\mysql\mysql5.6.17\bin>mysqld --lc_message=en_US
6. If you have problem in refreshing database migration and if you are receiving the errors like "pdoexception sqlstate 42s02 base table or view not found 1146"
In this case, drop the database and recreate the database and run the migration. It will help you migrate the tables successfully.
7. If you are getting error like "PDOException (1045)
SQLSTATE[HY000] [1045] Access denied for user 'homestead'@'localhost' (using password: YES)"
Go to app > config > local > database.php
There you will see the database homestead which you need to change. I have changed the settings like this and removed the configuration for pgsql (postGreSQL) because I am using MySQL.
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'laravelproject',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
),
8. To access the list of artisan, use this command
C:\wamp\www\laravelproject> php artisan
9. If you find class doesn't exist error (even if the class is present in your controller), then run this command to auto load the classes
C:\wamp\www\laravelproject> php artisan dump-autoload
10. Displaying all sql statements in Eloquent
Plus the following code in routes.php and you will be able to see SQL statements Eloquent is executing. This will help you in better debugging
// Display all SQL executed in Eloquent Event::listen('illuminate.query', function($query) { var_dump($query); });
OR
DB::getQueryLog()
11. Displaying current laravel version
C:\wamp\www\laravelproject> php artisan --version
12. Displaying list of available commands
C:\wamp\www\laravelproject> php artisan list
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment