Laravel Custom Setters Example Tutorial

Laravel 8

Nicesnippets

1152

05-05-2021


Laravel Custom Setters Example Tutorial

Hi guys, Today i will explained to the laravel custom setter tutorial in your laravel project.The laravel custom setter tutorial is so easy to use.so you can just follow my step by step and learn laravel custom setter tutorial.

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 setPasswordAttribute($password)

{

$this->attributes['password'] = bcrypt($password);

}

Example :

Step 1: Create a User model

First step to create a User.php models and use this code.

App/Models/User.php

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;

use Illuminate\Database\Eloquent\Factories\HasFactory;

use Illuminate\Foundation\Auth\User as Authenticatable;

use Illuminate\Notifications\Notifiable;

class User extends Authenticatable implements MustVerifyEmail

{

/**

* The attributes that are mass assignable.

*

* @var array

*/

protected $fillable = [

'first_name',

'last_name',

'email',

'password',

];

public function setPasswordAttribute($password)

{

$this->attributes['password'] = bcrypt($password);

}

}

Step 2: Create Route

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

routes/web.php

<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\UserController;

/*

|--------------------------------------------------------------------------

| 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('users/create', [UserController::class, 'index'])->name('users.create');

Route::post('users/store', [UserController::class, 'store'])->name('user.store');

Step 3: Create a User Controller

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

app/Http/Controllers/UserController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\User;

class UserController extends Controller

{

/**

* The attributes that are mass assignable.

*

* @var array

*/

public function index(Request $request)

{

$data = [

'first_name' => $request->first_name,

'last_name' => $request->last_name,

'email' => $request->email,

'password' => $request->password,

];

$user = User::create($data);

dd($user);

}

}

Step 4: Create a Users Blade File

Next you can require to the users.blade.php so create a users.blade.php in your resources/views directory folder.

resources/views/users.blade.php

<!DOCTYPE html>

<html>

<head>

<title>Laravel Custom Setters Tutorial</title>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">

</head>

<body>

<div class="container">

<h1>Laravel Custom Setters Tutorial</h1>

{!! Form::open(array('route' => 'user.store','method'=>'POST')) !!}

<div class="col-md-12">

<div class="row mt-0">

<div class="col-md-6">

<div class="form-group pl-3 pr-3">

<label>First Name</label>

{{ Form::text('first_name', null ,['class'=>'form-control', 'placeholder'=>'First Name'] ) }}

</div>

</div>

<div class="col-md-6">

<div class="form-group pl-3 pr-3">

<label>Last Name</label>

{{ Form::text('last_name', null ,['class'=>'form-control', 'placeholder'=>'Last Name'] ) }}

</div>

</div>

<div class="col-md-6">

<div class="form-group pl-3 pr-3">

<label>Email</label>

{{ Form::email('email', null ,['class'=>'form-control', 'placeholder'=>'Email'] ) }}

</div>

</div>

<div class="col-md-6">

<div class="form-group pl-3 pr-3">

<label>Password</label>

{{ Form::password('password',['class'=>'form-control', 'placeholder'=>'Enter Password'] ) }}

</div>

</div>

<div class="col-md-12 text-center mt-2 mb-3">

<button type="submit" class="btn btn-success">Submit</button>

</div>

</div>

</div>

{!! Form::close() !!}

</div>

</body>

</html>

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

Browser url run : http://localhost:8000/users/create

output:

App\Models\User {#1268 ?

#fillable: array:4 [?]

#hidden: array:2 [?]

#casts: array:1 [?]

#connection: "mysql"

#table: "users"

#primaryKey: "id"

#keyType: "int"

+incrementing: true

#with: []

#withCount: []

#perPage: 15

+exists: true

+wasRecentlyCreated: false

#attributes: array:9 [?]

#original: array:9 [?

"id" => 30

"first_name" => "divyang"

"last_name" => "vadodariya"

"email" => "[email protected]"

"email_verified_at" => null

"password" => "$2y$10$4ZnAV8RyR0pSaRl6icRY2.VE6bjWJahWpgPR4FHbKqhx0LikTR3NW"

"remember_token" => null

"created_at" => "2021-05-05 12:40:33"

"updated_at" => "2021-05-05 12:40:33"

]

#changes: []

#classCastCache: []

#dates: []

#dateFormat: null

#appends: []

#dispatchesEvents: []

#observables: []

#relations: []

#touches: []

+timestamps: true

#visible: []

#guarded: array:1 [?]

#rememberTokenName: "remember_token"

}