Passar string para uppercase – laravel

Question:

Good afternoon. I have the following code that saves perfectly.

public function store(Request $request)
{
    $this->validate($request, [
        'servidor' => 'required|unique:servidors|max:255',
       // 'dtprotocolo' => 'date|date_format:Y-m-d',
    ]);

    Servidor::create($request->all());

    Session::flash('create_servidor', 'Servidor cadastrado com sucesso!');

    return redirect(route('servidor.index'));
}

However I would like to change the value $request->server to uppercase. How to proceed?

Answer:

In your Servidor model make a Mutators that the value will always be capitalized from this configuration, from this code below, as an example:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Servidor extends Model
{
    // as outras configuração do seu model continuam

    // acrescente esse método
    public function setServidorAttribute($value)
    {
        $this->attributes['servidor'] = mb_strtoupper($value);
    }
}

For more details on configuring a Mutators check the Defining A Mutator documentation.

References:

Scroll to Top