Laravel Custom Getter Example

Laravel Custom Getter Example

Hey Dev,

Today, i will learn you how to laravel custom getter tutorial in your laravel project.The laravel custom getter tutorial is so easy to use.so you can just follow my step by step and learn laravel custom getter tutorial.

In this article we have three subject now we are getting three subject total marks how this possible now we can use laravel Getter function let's implement and here we go jump to full example follow my bellow step

So let's start to the example and follow to the my all step.

Solution:
/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
public function getStudentTotalMarkAttribute()
{
     return $this->seo_marks + $this->laravel_marks + $this->science_marks;
}
Step 1: Create a Student Controller

Next, you can require to the Student Controller so create a Student Controller in just following command through.

Path : app/Http/Controllers/StudentController.php
<?php

namespace App\Http\Controllers;

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

class StudentController extends Controller
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */

    public function index(Request $request)
    {
        $student = Student::find(1);
        dd($student->studentTotalMark);
    }
}
Step 2: Create a Student model

Last step to create a Student.php models and use this code.

Path : App/Models/Student.php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Student extends Model
{
    use HasFactory;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */

    protected $fillable = [
        'name',
        'seo_marks',
        'laravel_marks',
        'science_marks',
    ];

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */

    // Total Marks
    public function getStudentTotalMarkAttribute()
    {
        return $this->seo_marks + $this->laravel_marks + $this->science_marks;
    }
}

Step 3: Create Route

Last step to create a route in web.php file and use this code.

Path : routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\StudentController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('student-profile', [StudentController::class, 'index'])->name('student');

Ok, now you have to create some dummy records on users table.

So, finally we are done with our code we can get below output.

php artisan serve

Now you can open bellow URL on your browser:

Browser url run : http://localhost:8000/student-profile
It will help you...