Web progamming and database development

Diary of problems and solutions related to web programming, database development and related technologies and platforms.

Some tips and tricks in Laravel

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

No comments :

Post a Comment

Managing settings to debug in Laravel

1. You need to identify your host name first. To know the host name type the following on command mode.

C:\> hostname
SOM013

You will use this host name for setting up for proper debugging in step no 2.

2. Go to bootstrap > start.php and go to the position having following code.

$env = $app->detectEnvironment(array(
'local' => array('homestead'),
));

Change the above code to the following like this:

$env = $app->detectEnvironment(array(
'local' => array('homestead', 'SOM013'),
));

Here we have added the host name inside the array.

This will show you the stack trace with all the environment variables which helps in debugging. Enjoy debugging using Laravel!

No comments :

Post a Comment

Things to do for setting up Laravel Project

1. Creating a unique Laravel Key for the application or project.

Go to laravelproject > app > config > app.php

Change

key = 'YourSecretKeyGoesHere!'

to

key =''

Save the file and go to command line prompt

C:\wamp\www\laravelproject>php artisan key:generate

2. Configure database settings.

Go to app > config > database.php

Change the details like this. I changed the database name and username as they were not matching according to my database set up.

'mysql' => array(
'driver'    => 'mysql',
'host'      => 'localhost',
'database'  => 'laravelproject',
'username'  => 'root',
'password'  => '',
'charset'   => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix'    => '',
),

3. Install and create migration  table

C:\wamp\www\laravelproject>php artisan migrate:install 

Open localhost:phpmydmin and laravelproject databaes. There you will see migrations table created.

4. Creating authors table

C:\wamp\www\laravelproject>php artisan migrate:make create_authors_table

It creates migration file. For this go to app > database > migrations > ......................create_authors_table.php. Open this file.

There you will see CreateAuthorsTable class having up() and down() functions. Up() functions helps to make changes to the database while down() function helps to revert (perfom undo) operations for the changes made at up() function.

Define the up and down functions like this:

public function up()
{
Schema::create('authors', function($table){
$table->increments('id');
$table->string('name');
$table->text('biography');
$table->timestamps();
});
}

public function down()
{
Schema::drop('authors');
}

Execute/run this file via command mode as:

C:\wamp\www\laravelproject>php artisan migrate
Do you really want to run this command? yes

This will create an authors table in the database

Inserting records to the table. Create a class which helps to do that:

C:\wamp\www\laravelproject>php artisan migrate:make add_authors

Open add_authors.php file just created inside migration folder.

Add the following code to add the records:

public function up()
{
DB::table('authors')->insert(array(
'name' => 'Suresh Raj Bhattarai',
'biography' => 'Suresh is a good author',
'created_at' => date('Y-m-d H:m:s'),
'updated_at' => date('Y-m-d H:m:s')
));

DB::table('authors')->insert(array(
'name'=>'Surendra Sedhai',
'biography' => 'Surendra is a good author',
'created_at' => date('Y-m-d H:m:s'),
'updated_at' => date('Y-m-d H:m:s')
));
}

public function down()
{
DB::table('authors')->where('name', '=', 'Suresh Raj Bhattarai')->delete();
DB::table('authors')->where('name', '=', 'Surendra Sedhai')->delete();
}

Make changes to the table ie add the records by running this again:
C:\wamp\www\laravelproject>php artisan migrate

Look at the table authors. You will see two records added there.

If you want to undo the insertion, the run the rollback command like this:
C:\wamp\www\laravelproject>php artisan migrate:rollback

This rollbacks the last migration done to the database. That is it rollbacks the insertion of the data that took place to the authors table.

To rollback the creation of authors table, run the rollback command again.
C:\wamp\www\laravelproject>php artisan migrate:rollback
This will delete the table authoers.

To migrate everything back, then run the migration command like this
C:\wamp\www\laravelproject>php artisan migrate
This will create authors table and insert the values to the table again.

5. Go to app | views | hello.php

Clear the content inside body tag and type any html content you like. This will change the content of the laravel project for the index page.

Try browsing the page : http://localhost:81/laravelproject/public/

The reason behind displaying this view as hello.php is because of definition on routes file which is located at app > routes.php which has been defined like this way:

Route::get('/', function()
{
return View::make('hello');
});

Another example, if you create about.php inside the same view folder, then you can define routes like this way to open the file:

Route::get('/about', function(){
return View::make('about');
});

Browse the page as :http://localhost:81/laravelproject/public/about

No comments :

Post a Comment

Running Laravel project using Local Server Routes

Alternative 1:

Go to laravel project. Lets say
C:/>cd wamp/www/laravelproject

Type the following command to run the laravel project using local server routes
C:/wamp/www/laravelproject>php -S localhost:8888 -t public

Open a brower and enter the following URL on address bar
http://localhost:8888

Alternative 2:

To start the project at localhost:8000, run the following lines of command
C:/wamp/www/laravelproject>php artisan serve

No comments :

Post a Comment

To change data format from mm/dd/yyyy to yyyy-mm-dd using MySQL

DATE_FORMAT(STR_TO_DATE(DateValueORColumnName, '%d.%m.%y'), '%Y-%m-%d')

No comments :

Post a Comment

Changing MySQL error language from French To English or to your preferred language

If you have been facing the default error language setting of MySQL as French, you can change the settings. For this go to my.ini file and follow these steps.

1. Search the following content
lc-messages=fr_FR

2. Replace the content to your preferred language, for example to change to English language
lc-messages=en_GB

3. Save the file and restart MySQL.

No comments :

Post a Comment