Laravel append URI in route - php

Hi I want to append the uri in laravel route function.
e.g we have /search?type=listing
//how do i can achieve this with
route('search',['type'=>'listing'])
Once the we are on the search. I want to have all the variable appended to search like
type=listing&query=blah blah

If I get you right, you want to save all query parameters. Use Request::query() to get it and then merge with your new parameters.
route('search', array_merge(\Request::query(), ['type' => 'listing'])));

If you have a named route and want to generate url with query params then:
route('route_name', ['param1' => 'value', 'param2' => 'value']);
In your case you can do this with
route('search',['type'=>'listing','subject' => ['blah'],[....]])

Related

Send data using redirect action using Laravel 7

I need to use something like this one :
return redirect()->action([HomeController::class, 'index'])->with('data'=>$data);
but I don't know how ?
Action method accepts two parameters which are array both. Second array is associative array of key as parameter name and value of parameter value.
In your case it would be
return redirect()->action(
[HomeController::class, 'index'],
['data' => $data]
);
Docs.

Pass parameter to GET request in laravel tests

In Laravel tests i want to send a get request with some parameters like this:
$response=$this->get(
route('orders.payment.pay',['order'=>$order->id]),
['pay_type','payment_gateway']
);
but when i run it, i have 302 Error code in response. But when use it like this it works correct:
$response=$this->get(
route('orders.payment.pay',['order'=>$order->id]).'?pay_type=payment_gateway'
);
Is there any way to pass parameter like first way?
This is the signature of the route helper:
function route($name, $parameters = [], $absolute = true)
You should add any query parameters you want to the array or parameters you are passing to the route helper:
route('orders.payment.pay', [
'order' => $order->id,
'pay_type' => 'payment_gateway',
]);
Any parameter that is not substituted for a Route Parameter is appended as a query string parameter.

How to put plural parameters into url with laravel

I want to build a website with a plural levels in the url. When it gets deeper,I find it difficult to get the parameters in the url.For example, www.example.com/level1/level2, I can get plural parameters level2(plural pages) because I know level1,but as it keeps going like level1/level2/level3,since parameter level2 is unknown value,how should I get level3? Because based on what I'm thinking, there are level4 and level5, at last, should the route file look like Route::get('/{parameter1}/{parameter2}/{parameter3}/{parameter4}','Controller#func')?
Any reply will be appreciated!
Is this what you are looking for?
{{ url('func', ['level1' => 'val1', 'level2' => 'val2', 'level3' => 'val3']) }} // your link
Route::get('/func', 'YourController#func');
your action method
public function func(Request $request){
$level3 = $request->get('level3');
}

How can i Get the paramter of URL contains slash

I have this URL :
dev.local.co/fr/admin/quoteManag/addquote/numberModel/123456/5
when I want to get the parameter numberModel.
On var_dump I get just "123456", not "123456/5"
You can use urlencode
$parameter = urlencode('123456/5'); // 123456%2F5
echo urldecode($_GET['numberModel']); // 123456/5
Or in your router, create a regex that will accept the slash as part of the parameter.
$route = new Zend_Controller_Router_Route_Regex("numberModel/([0-9\/]*)", array("module" => "MODULE", "controller" => "CONTROLLER", "action" => "ACTION"), array(1 => "numberModel"));
Not 100% but if this was me, I would get the url, explode it using /
Get the last 2 from the array, implode them.
But it depends on how you get the url, do you have a rewrite rule?
If so then I would look in that and you need to just sharpen it up slightly.

Laravel 4 How to pick random language string?

I am using Laravel 4 and I am creating an authentication app. I am stuck in a very small feature I want to implement but for me it's needed. When the user logs in I want to display a random array of "greetings" like "Howdly, username" or "Hey there, username" etc. from my language file. Is there any way I could do that?
I tried something like that:
{{ array_rand(trans('en.greetings') }}
But it displays the variable given for each string instead (for example hey_there which should be "Hey there")
My array:
"greetings" => array(
"howdly" => "Howdly",
"hello" => "Hello",
"hello_there" => "Hello there",
"hey" => "Hey",
"arr" => "Arr"
),
You could just shuffle the array each time and print the first index
//I am creating an array here, but you could assign whatever
$greetings = array(
"howdly" => "Howdly",
"hello" => "Hello",
"hello_there" => "Hello there",
"hey" => "Hey",
"arr" => "Arr"
);
shuffle($greetings);
echo reset($greetings); //will print the first value, you can return it or assgn it to a variable etc
I had the similar use case where I had to give random string back to the user. I'm using Laravel 5, and this is how I solved it. Your language file could look like this:
return[
"greeting_1"=>"myString 1",
"greeting_2"=>"myString 2",
"greeting_3"=>"myString 3",
];
I added a custom helper to my application and wrote a helper method to choose a response in random. My method looks like this:
function getRandomPrompt($key,$params=array()){
$key=explode(".",$key);
if(count($key)!= 2)
throw new Exception("Invalid language key format");
$file=$key[0];
$msgKey=$key[1];
//Get all the keys of the file
$keys_from_file=Lang::get($file);
//Filter all the prompts with this key
foreach ($keys_from_file as $file_key=>$value){
$key_parts=explode("_",$file_key);
if(($key_parts[0])!==$msgKey)
unset($keys_from_file[$file_key]);
}
$selected_key=array_rand($keys_from_file);
if(count($keys_from_file)>=1)
return Lang::get($file.".".$selected_key,$params);
else
return($file.".".$msgKey);
}
This helper simply looks for all possible keys, and return one string in random. You can also pass a parameter array to it. Now, whenever you want a string, you can get it by calling:getRandomPrompt("filename.greeting")
Was looking for something like this, but eventually I found another solution.
In your lang file:
return [
'String',
'Thong',
'Underwhere?',
];
Your blade solution:
{{ __('messages')[array_rand(__('messages'))] }}

Categories