Laravel redirect with variable - php

I want to use redirect with data in Laravel8. But this code is not showing data. What is the problem in syntax ?
IN CONTROLLER
$list = BE_MEDICAL_INSURANCE_FOR_FOREIGNERS::get();
redirect()->route('dashboard')->with( ['list' => $list] );

Try the following
redirect()->route('dashboard')->with('list', $list);
for more info follow https://laravel.com/docs/8.x/redirects

First be sure that list load some data with dd($list);
After use redirect helper like
$list = //...;
return redirect()->route('dashboard', ['list'=>$list]);
you can use closure function in the route in web.php and inspect if the data is bind through the route like
Route::get('/dashboard/{list}', function($list){
dd('list');
})->name('dashboard');

Related

Try to double filter sql query data laravel

guys i trying to filter my data from mysql with where clause but after put secound value laravel give me a blank result? If i try to filtered with first value example like this : http://localhost/transport/1 everything is good but if i try to set from destionation give me a blank result. example with fail : http://localhost/transport/1/Германия
Here is my Controller
class TransportController extends Controller
{
public function filtermethod($method){
$data['ads'] = db::table('ads')->where('method', $method)->get();
return view('transport', $data );
}
public function regionfrom($from){
$data['ads'] = db::table('ads')->where('from', $from)->get();
return view('transport', $data );
}
Here is my routes :
Route::get('transport/{method}', 'TransportController#filtermethod');
Route::get('transport/{method}/{from}', 'TransportController#regionfrom');
Your second route should be giving your controller 2 variables.
public function regionfrom($method, $from)
Is what your route your having problems with is calling, do the logic you like in there.
If you would like to filter twice, try this:
$data = DB::table('ads')-where('method', $method)->where('region', $region)->get();

Laravel route redirecting with data

I have a basic route that looks like this:
Route::prefix('/group')->group(function () {
// Some routes here
Route::prefix('/{uuid}')->group(function () {
// Some routes here
Route::get('/user/{id}', 'Controller#preview')->name('view-user')->where('id', '[0-9]+');
}
}
The logic is that I want the id to be only numerical value. What I want to do now is, to declare a redirection to this, if the value is non-numerical. Let's say the input of id is fs. In that case I would want it to redirect to id with value 1.
I tried using Route:redirect, but could not make it work. It looks something like this:
Route::redirect('/group/{uuid}/user/{id}', '/group/{uuid}/user/1')->where('id', '[^0-9]+');
I would prefer to put the redirect inside the groups, but it can be outside if this is the only way. Any help will be greatly appreciated.
What happens is, that I get a 404 error if I have the route redirect declared.
EDIT: I want to do it in the routes/web.php file. I know how to do it in the controller, but it is not what I need in the current case.
Closures are not an option either, because that would prevent routes caching.
Following up on the comment
You can create a Route in routes/web.php file that catches non-digit ids and redirect this to 'view-user' with id=1
It would look something like this
Route::get('/group/{uuid}/user/{id}', function ($uuid, $id) {
return redirect('view-user', ['uuid' => $uuid, 'id' => 1]);
})->where('id', '[^0-9]+');
// and then below have your normal route
Route::get('/group/{uuid}/user/{id}', 'Controller#preview')->name('view-user')->where('id', '[0-9]+');
Update
Following you comment that you do not want to use closures.
Change the "bad input route" to
Route::get('/group/{uuid}/user/{id}', 'Controller#redirectBadInput')->where('id', '[^0-9]+');
and then add the method in class Controller:
public function redirectBadInput ($uuid, $id) {
return redirect('view-user', ['uuid' => $uuid, 'id' => 1]);
}
You can see more in this SO thread about redirects and caching.
You declared it inverted.
In Laravel you can redirect passing parameters in this way:
You can pass the name instead of the url and simply pass variables.
Redirect::route('view-user', [$uuid, $id])
I think that you can do it inside of the controller of the router, with a logic like this:
class Controller {
// previous code ..
public function preview($uuid, $id) {
if(! is_numeric($id))
return redirect("/my-url/1");
// run the code below if $id is a numeric value..
// if not, return to some url with the id = 1
}
}
I think that there is no way to override the 'where' function of laravel, but I guess that have something like that in Routing Bindings:
Alternatively, you may override the resolveRouteBinding method on your Eloquent model. This method will receive the value of the URI segment and should return the instance of the class that should be injected into the route:
/**
* Retrieve the model for a bound value.
*
* #param mixed $value
* #return \Illuminate\Database\Eloquent\Model|null
*/
public function resolveRouteBinding($value)
{
return $this->where('name', $value)->first() ?? abort(404);
}
But it's require that you manage consise model's values instead of ids of whatever you want.
assign route name in route as like.
return Redirect::route('view-user', ['uuid'=>$uuid,'id'=>$id]);
As you want in web.php file then.
Route::get('/group/{uuid}/user/{id}', function($uuid, $id){
echo $uuid;
echo $id;
})->name('view-user')->where('id', '[0-9]+');

How to redirect url by a value in laravel by controller?

Is it possible to redirect to a url by a value from controller like the one we do in route?
Route example:
Route::get('/anyUrl/{id}', 'controller#function');
But how to do it in controller?
I mean how to pass the id by url in controller?
return redirect('/anyUrl/{$id}');
Something like above
You should try this:
return Redirect::to('/anyUrl/'. $id);
This should redirect you with your id:
return redirect("/anyUrl/$id");
Route::get('/anyUrl/{id}', 'controller#function');
Surely it helps you
public function yourFunctionName($id){
return Redirect::to('/anyUrl/'.$id)
}
You can do it that way:
return Redirect::route('user', array('nick' => $username));
Route::get('/anyUrl/{id}', 'controller#function');
in your controller receive like this way.
public function yourFunctionName($id){
dd($id);
}
return redirect("your url");
/anyUrl/$id
your id automatically sending into controller, just fetch that by request helper or by method argument and then return redirect("anyUrl/{$id}");
I think it is easy to redirect on a route. at first, you can add route name. example
Route::get('/anyUrl/{id}', 'controller#function')->name('profile');
then you can redirect by route name.
return Redirect()->Route('profile');
If your route has parameters, you may pass them as the second argument to the route method:
// For a route with the following URI: demo/{id}
return redirect()->route('demo', ['id' => 1]);

How to return back twice in Laravel?

In Laravel, there is a function return back();, which returns the user to the previous page. Is it possible to return back(); more than once within one function to return the user back twice or several times? I tried
public function ....()
{
return back();
return back();
}
but it doesn't seem to work.
No, but you could use session system to save URLs of 2-3-4 pages back. Use Session:: facade or session() helper for shorter syntax:
$links = session()->has('links') ? session('links') : [];
$currentLink = request()->path(); // Getting current URI like 'category/books/'
array_unshift($links, $currentLink); // Putting it in the beginning of links array
session(['links' => $links]); // Saving links array to the session
And to use it:
return redirect(session('links')[2]); // Will redirect 2 links back
it works for me Redirect::to($request->request->get('http_referrer'))

Some mandatory parameters are missing ("url_tag") to generate a URL for route "customUrl". Laravel 4

I am trying to break out a resource ("fans") into separate routes so I can have custom urls for the individual resource pages, as opposed to using just $id.
So instead of this:
Route::resource('fans', 'FansController');
I have broken it into this:
Route::get('fans/{url_tag}', array('as' => 'customUrl', function($url_tag)
{
$fan = Fan::where('url_tag','=',$url_tag);
return View::make('fans.show', compact('fan'));
}))
->where('url_tag', '[A-Za-z]+');
Route::get('fans/{id}', function($id)
{
// do something with $id or
return Redirect::route('customUrl');
})
->where('id', '[0-9]+');
In the fans controller, I have this:
public function show($id)
{
$fan = Fan::find($id);
return View::make('fans.show', compact('fan'))
->with('fans', Fan::all())
->with('latest_fan_likes', Fanartist::latest_profile_fan_likes($id));
}
Basically I would like the page /fans/$id to appear as /fans/$url_tag (where url_tag is another column in the db, that is unique). But I would like it to have all of the properties of the resource, so I can call $fan->column directly.
When I run this, I get the error:
Some mandatory parameters are missing ("url_tag") to generate a URL for route "customUrl".
Any idea what is going on or what is wrong? I am using Laravel 4. Thank you.
You probably are trying to generate an URL, somewhere else in your code, in a controller or even a view, something like (may not be exactly that!):
URL::route('customUrl');
or
route('customUrl');
But you need to provider the url_tag:
URL::route('customUrl', array('url_tag' => 'something');
For you could be something like:
URL::route('customUrl', array('url_tag' => $fan->url_tag);

Categories