Redirect to another route with data Laravel 5.8 - laravel

Good day everyone. I am encountering an error in my code. I am trying to redirect to another route and passing data with it.
Controller code:
return redirect()->route('customer.success', compact('data'));
Route:
Route::get('success-customer', 'CustomerController#showSuccess')->name('customer.success');
Blade:
Your assistant: {{Session::get($data['assistant'])}}
Now my error is, it shows the error of undefined data yet I used the compact function.
Answers and advices are highly appreciated!

In laravel 5.8 you can do the following:
return redirect('login')->with('data',$data);
in blade file The data will store in session not in variable.
{{ Session::get('data') }}

You can use this:
return redirect()->route('profile', ['id' => 1]);
To redirect to any controller, use this code
return redirect()->action('DefaultController#index');
If you want to send data using redirect, try to use this code
return Redirect::route('customer.success)->with( ['data' => $data] );
To read the data in blade, use this one
// in PHP
$id = session()->get( 'data' );
// in Blade
{{ session()->get( 'data' ) }}
Check here for more info

TRy this
return Redirect::route('customer.success')->with(['data'=>$data]);
In blade
Session::get('data');

Related

Form submit going to the wrong route

I am saving data from a simple form in my Laravel project.
While submitting, it should go to the route that is predefined for store() method. I use such code:
{!! Form::open(['action' => 'PostsController#store', 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}
It goes to the route that is for index() method. Any help?
In store() method, I have such code:
$posts = new Post;
$posts->title = $request->input('title');
$posts->body = $request->input('body');
$posts->save();
return redirect('/');
My web.php contains:
Route::resource('/','PostsController');
Your code is correct bro.. The only reason you're going to index is because of the
return redirect('/'); in the store function... Check whether youdata is saved in the database or not...
Have you tested to see if this actually saves the data still? With Route resources, the route will be the same for both store and index methods, just a different HTTP method.
Maybe your code is working well & data saved in the database. You return redirect('/') it to your index() method, so you don't understand the difference. Check your database.

How to write post response in Laravel?

How to convert this php code in to laravel?
While add this code in laravel blade getting error?
<?php
$status=$_POST["status"];
echo "<h3>Thank You. Your order status is ". $status .".</h3>";
?>
Laravel is an MVC framework, so the business logic remains in controller function like:
$status = Input::get('status');
and pass it to view like:
return view('view_name', ['status' => $status]);
and you can use this variable on view like:
<h3>Thank You. Your order status is {{ $status }}</h3>

Laravel undefine variable in view

I'm new to laravel. Using version 5.4 and tried to search but don't see what I'm doing wrong. I keep getting an "Undefined variable: post" in my view. I'm also doing form model binding. Model binding works properly when manually entering URL. Just can't click on link to bring up edit view.
My routes:
Route::get('test/{id}/edit','TestController#edit');
My controller:
public function edit($id)
{
$post = Post::find($id);
if(!$post)
abort(404);
return view('test/edit')->with('test', $post);
}
My form:
{{ Form::model($post, array('route' => array('test.update', $post->id), 'files' => true, 'method' => 'PUT')) }}
You're assigning the post value to 'test', so should be accessible with $test rather than $post.
You probably want to do either of these two things instead:
return view('test/edit')->with('post', $post);
or
return view('test/edit', ['post' => $post]);
https://laravel.com/docs/5.4/views
Your controller is sending a variable named "test", but your error says that your blade file doesn't have the $post variable passed into it. This can be fixed by changing "test" to "post" in your controller.

Laravel - Passing Parameters/ undefined variable

I'm struggling with passing parameters through in Laravel, I can access it via URL but I don't want that. I'm getting Undefined variable: user in the master.blade error.
Any help is appreciated
AuthController.php
function viewUserDetails($userId)
{
$user = User::find($userId);
return view('user/userdetails',['user' => $user]);
}
Web.php
Route::get('userdetails/{userId}', 'AuthController#viewUserDetails');
Master.Blade
<li>Your Details</li>
Other guys are telling you about userdetails view, but your code is totally fine. What you need to do is pass $user variable to the master view too:
return view('master', ['user' => $user]);
Try to pass like this
return view('user/userdetails')->with('user' => $user);
Controller::
function viewUserDetails($userId)
{
$user = User::find($userId);
return view('user.userdetails')-with('user',$user);
}
Then Used In blade File..
#foreach ($user as $data){
<h1> {{ $data['_id'] }} </h1>
<h1> {{ $data['name'] }} </h1>
#endforeach
I assume that you have extended master.blade in user/userdetails.blade. You need use array as second parameter to url function.
<li>Your Details</li>
It is best practice to use named route in laravel.
Change your route like this :
Route::get('userdetails/{userId}', 'AuthController#viewUserDetails')->named('user_details');
In your blade you can use route function to get the path.
<li>Your Details</li>
You can update your code like:
AuthController.php
function viewUserDetails($userId)
{
$user = User::find($userId);
return view('user.userdetails',compact('user'));
}
Web.php
Route::get('userdetails/{userId}', 'AuthController#viewUserDetails');
Master.Blade
<li>Your Details</li>

AJAX Request getContent Laravel 5.1

$content = $request->getContent();
return \Response::json([
'success' => $content
]);
I don't know how to format it to json in laravel 5.1. I want to use the data but if I use $request->input('name') it is null, it's actually in the controller. If you have another way to get the data it would be much appreciated.
you would have to parse the response in the client-side:
JSON.parse(ajaxResponse)
right now you're only getting a string back from the server

Resources