V
V
Vitaly2020-08-02 12:47:22
Laravel
Vitaly, 2020-08-02 12:47:22

How to interrupt the execution of the code with an error?

Good afternoon!
There is a similar code (I use Laravel):

class Class1
{
public function startFunc1()
    {
        if (!$this->getFunc2()) {
            return back()->withErrors(['Произошла ошибка!']);
        };
        $i++;
        //Продолжается код функции
   }
}


$cl1 = new Class1();
$responseFunc1 = $cl1->startFunc1();
$cl2 = new Class2();
$data = $cl2->getFromList($request->articul);
return view('fs.index', ['data' => $data]);


How can you display an error in view('fs.index') from this line: but so that after this line further code execution stops and the error is displayed in this case in view('fs.index'). The option to specify return redirect () with a specific path does not seem to be suitable, because this function can be called from different views and, accordingly, the error should be displayed in different views.
return back()->withErrors(['Произошла ошибка!']);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2020-08-02
@Nc_Soft

There should not be such a thing in the business logic . It is necessary to throw exceptions and catch them in controllers.
return back()->withErrors(['Произошла ошибка!']);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question