Laravel
500
w3alert.com
07-01-2020
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).
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 { }
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'); } }
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/".
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.
To insert data in the database table, You could do like:
public function insert() { $data = ['name' => 'hello', 'email' => '[email protected]']; $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.
To update data in the database table, You could do like:
public function update() { $data = ['name' => 'hello', 'email' => '[email protected]']; $post = \App\Post::where('id',1)->update($data); }
Here, the update() method will update the data in your database table, where id = 1;
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;
This tutorial idea has taken from this https://laravel.com/docs/7.x/eloquent