Install Laravel 12 App
composer create-project laravel/laravel example-app
Setup mysql database
update .env file
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel12app //database name
DB_USERNAME=root
DB_PASSWORD=
migrate database
php artisan migrate
Create factory file
php artisan make:factory ContactFactory
database\factories\ContactFactor.php like as below code
<?php
namespace Database\Factories;
use App\Models\Contact;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Contact>
*/
class ContactFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
protected $model = Contact::class;
public function definition(): array
{
return [
'name' => fake()->name(),
'email' => fake()->unique()->safeEmail(),
'phone' => fake()->unique()->phoneNumber(),
'address' => 'India',
'created_at' => now(),
'updated_at' => now()
];
}
}
Create seeder file
php artisan make:seeder ContactFactory
database\seeders\ContactSeeder.php like as below code
<?php
namespace Database\Seeders;
use App\Models\Contact;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class ContactSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
Contact::factory(25)->create();
}
}
Create Model & Migration
php artsan make:model Contact -m
app\Models\Contact.php like as below code
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Contact extends Model
{
use HasFactory;
protected $fillable = [
'name',
'email',
'phone',
'address',
];
public static function getData()
{
$data = self::paginate(5);
return $data;
}
public static function createData($request)
{
self::create([
'name' => $request->name,
'email' => $request->email,
'phone' => $request->phone,
'address' => $request->address,
]);
}
public static function editData($id)
{
$data = self::find($id);
return $data;
}
public static function updateData($request, $id)
{
self::where('id', $id)->update([
'name' => $request->name,
'email' => $request->email,
'phone' => $request->phone,
'address' => $request->address,
]);
}
public static function deleteData($id)
{
self::where('id', $id)->delete();
}
}
database\migrations\2025_02_27_075816_create_contacts_table.php like as below code
<?phpp
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('contacts', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->string('phone')->unique();
$table->text('address');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('contacts');
}
};
Run below this command
php artisan migrate
php artisan db:seed --class=ContactSeeder
Create route
routes/web.php like as below code
<?php
use App\Http\Controllers\ContactController;
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
Route::resource('contacts', ContactController::class);
Create controller
php artisan make:controller ContactController --resource
App/controller/ContactController.php like as below code
<?php
namespace App\Http\Controllers;
use App\Models\Contact;
use App\Http\Requests\ContactFormRequest;
use Illuminate\Http\Request;
class ContactController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
$data = Contact::getData();
return view('contact.index')->with('data', $data);
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*/
public function store(ContactFormRequest $request)
{
return ''Contact successfully added.';
}
/**
* Display the specified resource.
*/
public function show(string $id)
{
//
}
/**
* Show the form for editing the specified resource.
*/
public function edit(string $id)
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
//
}
}
Run Laravel App:
Now Run your laravel 12 app after run these below commands
No comments: