laravel return success response code example

Example 1: laravel return back with success

return redirect()->back()->with('message', 'IT WORKS!');

Displaying message if it exists:

@if(session()->has('message'))
    <div class="alert alert-success">
        {{ session()->get('message') }}
    </div>
@endif

Example 2: laravel return response json

return response()->json([
    'name' => 'Abigail',
    'state' => 'CA',
]);

Example 3: laravel return response view

return response()->view('your_view', compact('variableName'));