P
P
Pavel2020-05-14 16:10:15
Laravel
Pavel, 2020-05-14 16:10:15

How to trim the length of a string if the database has a limit on the number of characters?

Hello everyone

In the database there is a limit on the length of 50 characters, and if I try to write 60 characters, there will be an error, but I don’t want to do a crutch in the form of a length of 500 characters for the title. Tell me

how this is decided, in which direction to dig?

All that is:

Standard form

<form action="{{route('admin.category.update', $category)}}" method="post" enctype="multipart/form-data">
      <input type="text" class="ss_input" name="title" value="{{$category->title ?? ''}}" placeholder="">
      <button class="ss_button">Сохранить</button>
</form>


and the method itself

public function update(Request $request)
    {

        Categories::where()->where()->update([
            //
            'title' => $request->title ?? null,
            //
        ]);

        return redirect()->route('admin.category.index');
    }


Please help

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Sarvarov, 2020-05-14
@yatort

public function update(Request $request)
{
    $request->validate([
        'title' => 'max:255', // где 255 - макс. длина строки
    ]);

    // ...
}

But it's better to follow the SRP principle from SOLID and do the validation in a separate Request.
You can also add a constraint to the input in html:
maxlength="50"

S
Sergey Kuzmenko, 2020-05-14
@SergeyKuzmenko

str::limit
Str::limit($request->title, 50);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question