Pass string to uppercase - Standard

Asked

Viewed 2,510 times

3

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?

  • $comUpper = strtoupper($request->server); not right?

  • From, $request->server= strtoupper($request->server), but when I save to the database, Server::create($request->all()), it goes back to the original.

  • Juliano the answer was the answer to his question accepted as an answer

  • Try this: $data = $request->all(); $data['server'] = strtoupper($data['server']); Server::create($data);

2 answers

2


In his model Servidor make a Mutators that the value will always be uppercase from that configuration, from that 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 to configure a Mutators check the documentation Defining A Mutator.

References:

  • 1

    hi friend, I’m starting in Aravel, I need to call this function somewhere in the code or just declare the same in the model to work?

  • 1

    The time you pass the amount to $model->servidor = "novovalor"will be passed all to uppercase @Italorodrigo or any means of passing value to that member of the object.

0

If you are using inputs, you can capitalize the data at the time of insertion of the user. Use this javascript at the end of the page:

<script type="text/javascript">
// INICIO FUNÇÃO DE MASCARA MAIUSCULA
function maiuscula(z){
    v = z.value.toUpperCase();
    z.value = v;
}
//FIM DA FUNÇÃO MASCARA MAIUSCULA

in your input, use call the function:

<input name="cliente" class="form-control"  placeholder="nome cliente" onkeyup="maiuscula(this)">

Browser other questions tagged

You are not signed in. Login or sign up in order to post.