1

I have configured a resource route as below

Route::resource('users', UserController::class); 

When a user posts data, it will call the store method in the controller where it will add the data and set a message for success/failure.

public function store(Request $request) { // return $request; $request->validate( [ "firstName" => 'required', "lastName" => 'required', "phoneNo" => 'required', "email" => 'email:rfc,dns' ] ); $date = date(now()); $data = [ 'firstName' => $request->firstName, 'lastName' => $request->lastName, 'phoneNo' => $request->phoneNo, 'email' => $request->email, 'designation' => $request->designation, 'status' => $request->status, 'createdAt' => $date, 'updatedAt' => $date, ]; $user = Firebase::insertData($data, static::$collection); if ($user->id() != null) { $message = "User Created Successfully"; } else { $message = "Something went wrong. Please contact System Admin with error code USR001"; } return redirect()->route('users.index', ['message' => $message]); } 

This will redirect to the index method of the same controller. How can I use the $message parameter in the index method and send it to the view? My index method is below

public function index() { $userCollection = app('firebase.firestore')->database()->collection('users'); $userData = $userCollection->documents(); $response = []; $app = app(); foreach ($userData as $data) { $user = $app->make('stdClass'); $user->firstName = $data["firstName"]; $user->lastName = $data["lastName"]; $user->phoneNo = $data["phoneNo"]; $user->email = $data["email"]; $user->designation = $data["designation"]; $user->status = $data["status"]; $user->createdAt = $data["createdAt"]; $user->updatedAt = $data["updatedAt"]; array_push($response, $user); } return view('pages.user.list-user', ['response' => $response]); } 

4 Answers 4

1

You can directly pass the message as a flash message using the with() method with the redirect method.

Edit your redirect code as:

return redirect()->route('users.index')->with('message', $message]); 

and add the below code in your pages.user.list-user blade file:

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

Visit https://laravel.com/docs/8.x/redirects#redirecting-with-flashed-session-data for more info on redirects with a flash message.

Sign up to request clarification or add additional context in comments.

Comments

1

Replace your redirect code :

return redirect()->route('users.index', ['message' => $message]); 

with

return view('pages.user.list-user', ['message' => $message]); 

Comments

1

(1) First of all, pass the message in the parameter of index function:

public function index($message) { ... } 

(2) This is okay, you wrote correctly:

return redirect()->route('users.index', ['message' => $message]); 

(3) Now just access the message in the view (blade) and print it:

{{ $message }} 

You can also store message in $response array and simply pass the $response to the desired view:

$response['message'] = $message; 

Comments

0

You can have the index method like if the parameter used in the controller.

public function index(Request $request) { // Your code $message = $request['message']; } 

If you want to access the message in view use

return redirect()->route('users.index')->with('message', $message]); 

and access from the view using session('message') like in OMi Shah's answer

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.