In this tutorial will teach Laravel 10 Api Crud Application step by step. Laravel 10 CRUD Application we will cover about Create, Read, Update, and Delete and View crud operation in Laravel. Generate the Api is very easy task to transfer data using various front end applications like React,Vue,Angular etc.
Laravel is world best famous PHP framework.it has various features. Laravel is a MVC architecture. crud using Laravel example I will show in the simple way to make eloquent Laravel crud application. Here is the best place where learn Laravel 10.laravel php used to create the web application very easy to create and hosting the application.
Create the new project which name is lbs-app.type by following command to create the Laravel project.
composer create-project laravel/laravel lbs-app
After Type the Command you have to wait until the project installation get finish. After Finished it.let’s do the setup on database.
Do the database configuration on .env File for username, password and DB Name in this project i have changed the database name as lbs
After that run the project.
Run the Application using following command
php artisan serve
In Laravel create the table called as Migration
Run the command to create the Migration
php artisan make:migration create_students_table
After that you can check the inside database folder migrations create_students_table file has been created
successfully.
Select and open the students table. add the following table fields stname,address,phone
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { public function up(): void { Schema::create('students', function (Blueprint $table) { $table->id(); $table->string('name'); $table->date('address'); $table->string('phone')->nullable(); $table->timestamps(); }); } public function down(): void { Schema::dropIfExists('students'); } };
After modified the columns names then run the migrate command to create the tables in the databases.before the run the
command please save project then run it.
php artisan migrate
Create Controller
in order to create the controller if it crud you can use StudentController –api.
php artisan make:controller StudentController --api
After create the controller need to create the model.
Model is used to get the data from the database.
Create the Model name which is Student
php artisan make:model Student
After Model is Created the look like this. Code inside Model Class (app\Models\)
Change it as like this
Add the Namespace above
namespace App\Models;
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Employee extends Model { protected $table = 'students'; protected $primaryKey = 'id'; protected $fillable = [ 'name', 'address', 'phone', ]; use HasFactory; }
Pages are Manage through routes. If you are crud system simple you can add it the routes one line look like this
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\API\StudentController; Route::get('/', function () { return view('welcome'); }); Route::apiResource('/student', StudentController::class);
List the Routes
php artisan route:list
After created the controller paste this following code snippet
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Employee; class EmployeeController extends Controller { protected $student; public function __construct(){ $this->student = new Student(); } public function index() { return $this->student->all(); } public function store(Request $request) { return $this->student->create($request->all()); } public function show(string $id) { $student = $this->student->find($id); } public function update(Request $request, string $id) { $student = $this->student->find($id); $student->update($request->all()); return $student; } public function destroy(string $id) { $student = $this->student->find($id); return $student->delete(); } }
Initialize the employee number, Hourswork,and Hoursrate to calculate a grosswage use the following condition. if…
Act as a Java developer to create a program that calculates the gross wage for…
Initialize the employee number, Hourswork,and Hoursrate to calculate a grosswage use the following condition. if…
In this tutorial, we will teach you how to create a simple school management system…
I have design the Admin Basic templete using React MUI Design Admin Dashboard and Login.Here…
In this tutorial ,i am to going teach the Laravel Breeze.Laravel Breeze provides a simple…