In this blog, I will describe Laravel CRUD operation. I have used Laravel 10 version. You have to install Laravel and Composer first.

 

Step – 1:

  • Create a database and change the database name in .env file

Step-2:

Type in command:

  • Laravel new my-laravel-app
  • php artisan make:model Projectname -mc –resource  // creates model, Projectcontroller and resource

 

Step -3

Change in database>migration>project_table.php

  • public function up()
    {
    Schema::create('projects', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->text('body');
    $table->timestamps();
    });
    }
  • type in command php artisan migrate
  • store some data in the database

Step 4:

Change in routes> web.php
Route::get('/projects', [ProjectController::class, 'index']);
Route::get('/project/{id}', [ProjectController::class, 'show']);
Route::get('/create-project', [ProjectController::class, 'create']);
Route::post('/create-project', [ProjectController::class, 'store']);
Route::get('/project/{id}/edit', [ProjectController::class, 'edit']);
Route::put('/project/{id}/edit', [ProjectController::class, 'update']);
Route::post('/project/{id}/delete', [ProjectController::class, 'destroy']);

Step-5:

Change in app> http> controller> projectcontroller.php

namespace App\Http\Controllers;

use App\Models\Project;
use Illuminate\Http\Request;

class ProjectController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$allData = project::latest()->paginate(1); // myNote: Use get() instead of paginate to show all project
return view(‘projects.index’, [
‘projects’ => $allData
]);

}

/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
return view(‘projects.create’);
}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//

request()->validate([
‘name’ => ‘required’,
‘body’ => ‘required’
]);
$project = new Project();
$project ->name = request(‘name’);
$project ->body = request(‘body’);

$project->save();
return redirect(‘/projects’);
}

/**
* Display the specified resource.
*
* @param \App\Models\Project $project
* @return \Illuminate\Http\Response
*/
public function show(Project $project, $id)
{
//
$project = Project::findOrFail($id);
return view(‘projects.single’, [‘project’ => $project ]);
}

/**
* Show the form for editing the specified resource.
*
* @param \App\Models\Project $project
* @return \Illuminate\Http\Response
*/
public function edit(Project $project, $id)
{
//
$project = Project::findOrFail($id);
return view(‘projects.edit’, [‘project’ => $project]);
}

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Project $project
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Project $project, $id)
{
//
request()->validate([
‘name’ => ‘required’,
‘body’ => ‘required’
]);
$project = Project::findOrFail($id);
$project ->name = request(‘name’);
$project ->body = request(‘body’);

$project->save();
return redirect(‘/projects’);
}

/**
* Remove the specified resource from storage.
*
* @param \App\Models\Project $project
* @return \Illuminate\Http\Response
*/
public function destroy(Project $project, $id)
{
//

$project = Project::findOrFail($id);
$project->delete();
return redirect(‘/projects’);
}
}

 

Step -6

To avoid writing HTML5 code repeatedly create a app.blade.php file in resource>view> and paste the following code

Step-7 :

create necessary files in resource>view>projectname

such as

  • index.blade.php
  • create.blade.php
  • edit.blade.php
  • single.blade.php

index.blade.php

Create.blade.php

 

edit.blade.php

single.blade.php

Create a folder in resource>views>partials

create a navigation file in partials folder 

nav.blade.php

Change Welcome.blade.php

 

Full Tutorial can be found at bellow:

লারাভেল – বেসিক থেকে প্রজেক্ট ডেভেলপমেন্ট

Leave a Comment