Laravel get Route parameters outside controller - laravel

I have defined a dummy route like this:
Route::get('sth/{v1}/{v2}' , [
'uses'=>'SthController#sth',
]) ;
how can I get the value of v1 and v2, outside controllers?

use this code
$current_params = Route::current()->parameters();
dd($current_params->v1) ;

You can get the values of v1 and v2 anywhere like this:
request()->v1;
request()->v2;

In Laravel 5.6 for me it was:
Route::current()->parameters['v1']
Route::current()->parameters['v2']
etc...

You can put the data in session in controller when pass, then from anywhere you can get your desire data,
Session::put('v1');
Session::put('v2');
now anywhere you can access like:
Session::get('v1')
Session::get('v2')
if you need to delete session data just use
Session::forget('v1')
Session::forget('v2')

You can use the laravel helper named: request()
$value = request('key', $default);
For your route you can use
$v1 = request('v1','default data');
$v2 = request('v2','default data');
Laravel documentation: request helper laravel

haven't tried but think its Route::current(), use from anywhere to access the parameters
$currentParams = Route::current()->parameters();

This can be alternative way:
Route::getCurrentRoute()->getParameter('v1')

Related

How to get data from variable in ln laravel 5.2

My database table has name, phone, email etc.. fields. Now I store particular field data in different variable and pass them. Here is my code. I tried it from controller function. What should I do?
$var = DB::select("SELECT * FROM reg where email = '$c_email' and Password = '$c_pass' and type = '$c_type'");
$var2 = $var->name;
$var3 = $var->phone;
return redirect('farmer')->with('key', $var2)->with('key2', $var3);
You may try the given way
return view('farmer',compact('var2', 'var3'));
Here 'farmer' is your view page
http://www.easylaravelbook.com/blog/2015/03/09/passing-multiple-variables-into-a-laravel-5-view/
When you're using redirect()->with(), you're flashing data to the session. So to get this data after redirect, use session() helper:
session('key')
https://laravel.com/docs/5.3/responses#redirecting-with-flashed-session-data
use this code. if farmer is view part. or use view part name.
$var=DB::table('reg')
->where('email', $c_email)
->where('password ', $c_pass)
->where('type', $c_pass)
->select('name','phone')
->first();
return view('farmer')->with('var', $var);
get data in view part,
$name=$var->name;
$phone=$var->phone;

laravel paginate passing additional var

let say i have url like this
http://localhost:8080/myweb/listproduct?idcategory=3
but when i used laravel pagination link, my passing variabel by url disappear like this
http://localhost:8080/myweb/listproduct?page=2
i want laravel paginate to pass my url variable too like this
http://localhost:8080/myweb/listproduct?idcategory=3&page2
so far i already try to set baseurl like this
$listproduct = DB::table('product')->paginate(15);
$users->setBaseUrl('listproduct?idcategory=3');
but the result is like this (? not &)
http://localhost:8080/myweb/listproduct?idcategory=3?page2
i used laravel 4.2
you can use setPath()
$users->setPath('listproduct?idcategory=3');
or if you only want to add to the query string you may use
$users->appends(['idcategory' => '3'])
You can use this method to append more arguments to your pagination link:
$listproduct->appends(['idcategory' => $id])->links();

Laravel - Get route by a route name

route('products.create') returns full path like http://myapp.dev/products/create.
How do I get the actual route? Only this -> products/create?
There is no such functionality provided by Laravel at this point. However, you may try this:
$extra = URL::to('/');
$actual = route('products.create');
str_replace($extra, '', $actual);
That will remove the unnecessary base URL from your route URL.

Routing in Codeigniter (:any) gives me strange results

Here i'm using below routing config
$route['controllername/(:any)'] = "controllername/index/$1";
this is working well for me, but i want to use other methods in the same controller.
for that i'm using below route
$route['controllername/search'] = "controllername/search";
this is also working fine, but i want pass parameters to this method.
Here if i'm passing parameters but it is calling index method
i want to use both of above routes, i have tried with below route also but same result
$route['controllername/search/(:any)'] = "controllername/search/$1";
could anyone give any suggestions?
Thankyou!!
Ok.Do it like this for routing multiple functions in same controller :
$route['default_controller'] = "controller_name";
$route['index/(:any)'] = "controller_name/index/$1";
$route['search/(:any)'] = "controller_name/search/$1";
Let me know if you find any issues or doubts.
#user4039421
change your roouting rules position like below
$route['controllername/search/(:any)'] = "controllername/search/$1";
$route['controllername/search'] = "controllername/search";
$route['controllername/(:any)'] = "controllername/index/$1";

Get params value from component Joomla 3.0

I have saved value like this in my component table fields params.
unique=1
default_value=Default
validate=Validate
validate_error_msg=Validate error messag
searchable=1
Now i want to get value in my component.So I am passing values in my component's view.html.php
in this way
$params = new JForm($row->params); but its not working.
Now I want to get value so I am taking like this
$this->params->getValue('default_value');
But its not work where as in Joomla 2.5 ,we can get value like this
$this->params->get('default_value');
Try like this
For Ex.
$param = JComponentHelper::getParams('com_users');
$default = $param->get('default_value');
Have you tried to use
$params->get('your_parameter_value_name');
instead of
$this->params->get('your_parameter_value_name');
It should work.

Resources