LARAVEL Tutorial


Laravel 6 Model Tutorial


Laravel is a free and open source MVC pattern based PHP framework. Here M stands for the Model. Laravel Model is used to querying data from the database tables. Laravel provides a very easy way to do that using the Laravel Eloquent ORM. Every table interacts own model to interact with the database.

A controller is located in the app/Http/ directory. You can create a new model in laravel with php Artisan command make:make using the command line (cmd).

Create a Model

Here, you will learn how to create a model with php artisan:make model in laravel using the command line (cmd):

Just open the command line and go to your laravel web application root directory. And type the following command for make model in laravel using command line(cmd): 

php artisan make:model Post

After that, you can view your created model by going to "App/". Your Eloquent model will look like the following:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{

}

Laravel Create Model with Migration

Here, you will learn how to create model with a migration file using php artisan:make model in laravel.

Just open the command line and go to your laravel web application root directory. And type the following command for make model in laravel using command line(cmd): 

php artisan make:model Post --migration

After that, you can view your created model by going to "App/". Your Eloquent model will look like the following:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{

}

And, you can view your created migration file by going to "database/migrations/". Your migration will look like the following:

<?php

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

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

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


Laravel Create Model, Migration with Controller

Here, you will learn how to create a model, migration file with the controller using php artisan:make model in laravel.

Just open the command line and go to your laravel web application root directory. And type the following command for make model in laravel using command line(cmd): 

php artisan make:model Post -mcr

Model:- you can view your created migration file by going to "App/".
Migration:- you can view your created migration file by going to "database/migrations/".
Controller:- you can view your created controller by going to "App/Http/Controllers/".

Laravel Retrieve Data from Database

To retrieve data from the database table, You could do like:

public function index() {
        
        $post = \App\Post::all();

        return view('home', $post);
}

Here, the all() method will return all the data from the database table and store it in the $post variable.

Laravel Insert Data in Database

To insert data in the database table, You could do like:

public function insert() {
        
        $data = ['name' => 'hello', 'email' => 'hello@gmail.com'];

        $post = \App\Post::insert($data); // return inserted object

        $post = \App\Post::insertGetId($data); // return last inserted id

}

Here, the insert() method will return data object and insertGetId() method will return the last inserted id from the database table.

laravel update data in database

To update data in the database table, You could do like:

public function update() {
        
        $data = ['name' => 'hello', 'email' => 'hello@gmail.com'];

        $post = \App\Post::where('id',1)->update($data);

}

Here, the update() method will update the data in your database table, where id = 1;

laravel delete data in database

To delete data in the database table, You could do like:

public function delete() {

   $post = \App\Post::where('id',1)->delete();

}

Here, the delete() method will delete the data in your database table, where id = 1;