Laravel Useful Command

Create Model Command:

php artisan make:model Flight
php artisan make:model <folder_path>/Flight

Model With Database Migration

php artisan make:model <folder>/Flight -m

Create Seeder Database

php artisan make:seeder ModelNameSeeder          <modelName>

Run Migration

Run Specif file with defining Path::

php artisan migrate --path=/database/migrations/my_migration.php

Run DB Seed

php artisan db:seed                         ---> Run Full Seeder 

php artisan db:seed --class=UserSeeder      ---> Run Specific Seeder <user>

php artisan migrate:fresh --seed            ---> Run Migration with Seeder

php artisan db:seed --force                 ---> Force Full Seeing db

Laravel clear cache file manually if command not supported

When we migrate local file to online server. Laravel cache stored on local cached file also migrated. So this cache will look over the cache defined file but would not get local file location and gets an error like.

 

 

If server allow us to run command

the commad for clear cache is:

$php artisan config:cache

$php artisan config:clear

but we don’t access command line any type of server so that  we need remove file which are getting us problem to recreate cache file are located on.

Manual Remove Cache file(Server automatically generate new file with the location):

/bootstrap/cache/

there are

config.php

packages.php

services.php

 

 

so we need remove it manually then load site, the site are working properly from now.

 

+++++++++++++++++++++++++++

Add some clear config command with our route. then run the clear command but hitting url also be the fine solution to remove cache files.

+++++++++++++++++++++++++++

Route::get(‘/clear-cache’, function() {
Artisan::call(‘cache:clear’);
return”Cache is cleared”;
});

put these code into you route/web.php

and run from web url <your-domain>/clear-cache

it will works perfectly.

thanks

 

Laravel commad used to create Model,Controller and Migration file at once

We can create Model, Controller and Migration file at a time by this command.

 

php artisan make:model Test -crm or
php artisan make:model --migration --controller --resource Test

if we need the Model class  inside the <Model folder> we need slide change on command.

php artisan make:model --migration --controller --resource Model/Test

Git Laravel project setup in to local computer

Step 1:

Create a database locally named as your project name or wish utf8_general_ci

forexample we are creating laravel_cms database name and its collation into utf8_unicode_ci

 

Step 2:

Download composer https://getcomposer.org/download/

Pull Laravel/php project from git provider.

or place of your project file into your directory.

 

 

Step 3:

Rename .env.example file to .env inside your project root and fill the database information. (windows wont let you do it, so you have to open your console cd your project root directory and run mv .env.example .env )

Step 4:

Open the console and cd your project root directory

>mv .env.example .env

$cp .env.example .env
$php artisan key:generate

to change .env.example file to .env file.

 

Step 5:

Setup your database setting in to project.

for command line

>vim .env

edit and set your database name, host, password

 

DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_cms
DB_USERNAME=root
DB_PASSWORD=

for my case i have these credential for the database system.

 

Step 6:

Run

$composer install

$composer update

or

$php composer.phar install

it will create your vendor file for the laravel project.

 

Step 7:

Run

$php artisan key:generate

It will create key for project CSRF token.

 

Step 8:

Run

$php artisan migrate

it is for making table schema and its primary seeds(data) which are written in migration folder.

 

Step 9:

Run

$php artisan db:seed to run seeders, if any.

Primary seeds(data) which are written in migration folder.

 

Step 10:

Run

$php artisan serve

 

Set up and installing laravel project

+++++++++++++++++++++++++++++++++++++++++++++++

Installing and set up process of Laravel projects

+++++++++++++++++++++++++++++++++++++++++++++++   ++++++++++++++++++++++++

Git Clone —————————————–

$git clone https://github.com/prakashkumarbhandari.git  

 

++++++++++++++++++++++++ Copy file with rename

—————————————–

change .env.example file to .env file $ cp .env.example .env now set up you database connection configuration — of .env file DB_HOST=localhost DB_DATABASE=ribbon DB_USERNAME=root DB_PASSWORD=

+++++++++++++++++++++++++++++++++++++

Run composer

—————————————————————

$ composer install    

 

+++++++++++++++++++++++++++++++++++++++

Migrate Database

——————————————————————-

$ php artisan migrate    

++++++++++++++++++++++

Seed database :

————————————-

$php artisan db:seed

Adding Column in Existing Table Laravel (migration command code)

We can easily add remove existing column in existing table of our project. It is necessary to add column in exiting production database. Here i have share the steps to add column in existing table.

 

To create a migration, you may use the migrate:make command on the Artisan CLI. Use a specific name to avoid clashing with existing models

for Laravel 3:


php artisan migrate:make add_sankar_to_tbl_admin_menu1

 

for Laravel 5+:

php artisan make:migration add_sankar_to_tbl_admin_menu1

 

 

Here –>

add_ is prefix

sankar is column name

_to_  is        joiner

tbl_admin_menu1 is existing table name

 

use $table->integer('sankar')->after(whichever_column); to add this field after specific column.

 

 

 

Creating Migration file in Laravel Project

Migrations are like version control for your database, allowing your team to easily modify and share the application’s database schema. Migrations are typically paired with Laravel’s schema builder to easily build your application’s database schema. If you have ever had to tell a teammate to manually add a column to their local database schema, you’ve faced the problem that database migrations solve.

The Laravel Schema facade provides database agnostic support for creating and manipulating tables across all of Laravel’s supported database systems.

 

Generating Migrations

To create a migration, use the make:migration Artisan command:

 

php artisan make:migration create_users_table

 

The new migration will be placed in your database/migrations directory. Each migration file name contains a timestamp which allows Laravel to determine the order of the migrations.

The --table and --create options may also be used to indicate the name of the table and whether the migration will be creating a new table. These options simply pre-fill the generated migration stub file with the specified table:

 

php artisan make:migration create_users_table --create=users

php artisan make:migration add_votes_to_users_table --table=users

If you would like to specify a custom output path for the generated migration, you may use the --path option when executing the make:migration command. The given path should be relative to your application’s base path.

 

Migration Structure

A migration class contains two methods: up and down. The up method is used to add new tables, columns, or indexes to your database, while the down method should simply reverse the operations performed by the up method.

Within both of these methods you may use the Laravel schema builder to expressively create and modify tables. To learn about all of the methods available on the Schema builder, check out its documentation. For example, this migration example creates a flights table:

<?php

use Illuminate\Support\Facades\Schema;
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();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('flights');
    }
}

 

Column type Available method (function) for Database column type like Int, Varchar, long string, bool etc.

Running Migrations

To run all of your outstanding migrations, execute the migrate Artisan command:

php artisan migrate 

Forcing Migrations To Run In Production

Some migration operations are destructive, which means they may cause you to lose data. In order to protect you from running these commands against your production database, you will be prompted for confirmation before the commands are executed. To force the commands to run without a prompt, use the --force flag:

php artisan migrate --force

Rolling Back Migrations

To rollback the latest migration operation, you may use the rollback command. This command rolls back the last “batch” of migrations, which may include multiple migration files:

php artisan migrate:rollback

You may rollback a limited number of migrations by providing the step option to the rollback command. For example, the following command will rollback the last five migrations:

php artisan migrate:rollback --step=5

How to Create a Model in Laravel

In the Laravel project we can make Model file by Command line or making by inner to model folder.

This is code for making Model (class) with named Widget:

We’re going to tackle something really basic by making a Widget model that we can use for future tutorials. I’m calling it widget so it can describe anything generic. This will come in really handy and you will love how easy and intuitive creating a model is in Laravel 5.1*.

Let’s start by going to the command line and running:

php artisan make:model Widget -m

 

Obviously we are using the make command in artisan and you can see how intuitive this really is. You’ll note that we added the flag -m, so that it will also make a migration for us. This is a super optimized workflow, gotta love that.

In your app folder, you will now see a Widget.php file:



<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Widget extends Model
{
   //
}

And that’s all there is to that!

So let’s go to our migration, which is located in database/migrations, you’ll see something like:



2015_08_171507_create_widgets_table.php

The date time is prepended to the descriptive name of the migration. You should note that Laravel expects a singular name for the model and plural name for the actual DB table.

This is very intuitive because you can think of the model returning a single instance of the tables records, so when we want a widget, we look in the widgets table.

Ok, so with migrations, you a nice stub, ready for you:



use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateWidgetsTable extends Migration
{
   /**
    * Run the migrations.
    *
    * @return void
    */
   public function up()
   {
       Schema::create('widgets', function (Blueprint $table) {
           $table->increments('id');
           $table->timestamps();
       });
   }

   /**
    * Reverse the migrations.
    *
    * @return void
    */
   public function down()
   {
       Schema::drop('widgets');
   }
}

Obviously you are getting the auto-incrementing id column as well as the timestamps, so this is a great timesaver.

So we’re just going to add one column to the up method to keep this simple:



$table->string('widget_name')->unique();

You’ll note that I’m calling it widget_name instead of name. The reason I like to do this is to keep the column name clear. Imagine you have 10 different models, all with a name column. This makes it harder to work with when you are doing joins, so I take the extra step to prepend the modelname_ to the column name. This is not a hard-baked rule, but I like to follow this convention.

Also note, that we put a unique constraint on it. So now your up method should look like this:



public function up()
{
   Schema::create('widgets', function (Blueprint $table) {
       $table->increments('id');
       $table->string('widget_name')->unique();
       $table->timestamps();
   });
}

Now let’s run the migration from the command line:



php artisan migrate

And with that you should have your new table.

Since we’re planning to add records to the widgets table, we need to add the following property to our Widget model:



/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['widget_name'];

This tells eloquent that this is a mass assignable property, and if we didn’t have this, Laravel’s Eloquent wouldn’t allow us to save any records.

And that’s all there is to making a model in Laravel. In our next tutorial, we will use Laravel’s model factory to populate some test records. Then, in other tutorials, we will make the routes, controllers and views, so we can play with our widgets.

I hope you have enjoyed this tutorial and found it useful. Click on the sprocket icon at the top of the page to see all tutorials. Please comment, share, and like if you can, thanks.