D
D
Dos7712018-01-30 07:44:23
PHP
Dos771, 2018-01-30 07:44:23

What is the best way to code in Controller in Laravel?

Good afternoon. I ask you to help a beginner Laravel programmer)
Guys, how can I write this code better, I just repeat the same code twice, only the name value in the Controller changes. At the moment the code works, only it's shit code )) Thanks in advance

public function create(Request $request)
{
    $this->validate($request, [
        'pasport' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        'certificate' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
    ]);


    $filenameWithExt = $request->file('pasport')->getClientOriginalName();
    $filenameWithExtTwo = $request->file('certificate')->getClientOriginalName();


    $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
    $filenameTwo = pathinfo($filenameWithExtTwo, PATHINFO_FILENAME);

    $extension = $request->file('pasport')->getClientOriginalExtension();
    $extensionTwo = $request->file('certificate')->getClientOriginalExtension();

    $fileNameToStore = $filename . '_'.time(). '.' . $extension;
    $fileNameToStoreTwo = $filenameTwo . '_'.time(). '.' . $filenameWithExtTwo;

    $path = $request->file('pasport')->storeAs('public/images', $fileNameToStore);
    $pathTwo = $request->file('certificate')->storeAs('public/images', $fileNameToStoreTwo);


    $post = new Check();
    $post->user_id = auth()->user()->id;
    $post->pasport = $fileNameToStore;
    $post->certificate = $fileNameToStoreTwo;
    $post->save();

    return redirect('/sendpalate')->with('success', 'Ваши данные успешно отправлены');

}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
hakkol, 2018-01-30
@hakkol

1) Move the validation into a separate request
2) Create a helper that will have a method for saving the image
3) Call the helper method in the controller and pass the file there ($request->file('pasport') or $request->file('certificate '))

A
asd111, 2018-01-30
@asd111

Shit code is when nothing is clear in what is written, as in Bitrix for example. Everything is clear here, don't worry.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question