Eloquent Model Conventions Tutorial in Laravel 11
<?php
namespace App\Http\Controllers;
use App\Models\Student;
use Illuminate\Http\Request;
class StudentController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
// $students = Student::all();
$students = Student::find(3);
return $students;
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
$student = new Student();
$student->name = "Shahil";
$student->age = 25;
$student->save();
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
//
}
/**
* 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)
{
//
}
}
Above File is app\Http\Controllers\StudentController FileBelow File is app\Models\Student File
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Student extends Model
{
protected $table = "my_table"; //Rename the table name
protected $primaryKey = "student_id"; //Rename the primary key column name
public $timestamps = false; //Removing created_at and updated_at column
// Renaming the created_at and updated_at column name
// First we have to rename created_at and updated_at column name from database then after we can execute below commands
// const CREATED_AT = "creation_date";
// const UPDATED_AT = "updated_date";
// If we don't insert any specific column value then default value will automatically set using below $attributes global variable
protected $attributes = [
"city" => "Goa"
];
}
Below File is database\json\students.json File
[
{
"name": "Yahoo Baba",
"age": 20,
"city": "Chandigarh"
},
{
"name": "Salman Khan",
"age": 22,
"city": "Mumbai"
},
{
"name": "Akshay Kumar",
"age": 23,
"city": "Goa"
},
{
"name": "John Abraham",
"age": 20,
"city": "Agra"
}
]
Below File is database\migrations\2024_12_09_045440_create_students_table File
<?php
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('students', function (Blueprint $table) {
$table->id();
$table->string("name");
$table->integer("age");
$table->string("city");
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('students');
}
};
Below File is database\seeders\DatabaseSeeder File
<?php
namespace Database\Seeders;
use App\Models\User;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*/
public function run(): void
{
// User::factory(10)->create();
// User::factory()->create([
// 'name' => 'Test User',
// 'email' => 'test@example.com',
// ]);
$this->call([
StudentSeeder::class
]);
}
}
Below File is database\seeders\StudentSeeder File
<?php
namespace Database\Seeders;
use App\Models\Student;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\File;
class StudentSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
$json = File::get(path: "database/json/students.json");
$students = collect(json_decode($json));
$students->each(function($student){
Student::create([
"name" => $student->name,
"age" => $student->age,
"city" => $student->city
]);
});
}
}
Below File is routes\web File
.png)
.png)
.png)
Comments
Post a Comment