How to pass multiple filter parameters - laravel-4

How can you pass multiple parameters to a filter?
This function in the compiled.php indicated that you use a comma as a separator
protected static function parseParameterFilter($filter)
{
list($name, $parameters) = explode(':', $filter, 2);
return array($name, explode(',', $parameters));
}
However, I try to dump the $value and only the first parameter is dumped. Why is this?

Related

How to pass multiple parameters to route

Hello i have this function called show and it has 2 parameters id and options this is the code from blade.php :
<a href="{{route('orders.show',$order->id,'1')}}">
<button>Edit</button>
</a>
The number one is static but i have another button that has value 2 now this is the code from the controller:
public function show($id, $option)
{
$orders = Order::with('client', 'products')->where('id', $id)->firstOrFail();
$clientList = Client::all();
$productList = Product::all();
switch ($option)
{
case 1:
{
return view('orders.edit', compact('orders', 'clientList', 'productList'));
break;
}
case 2:
{
return view('orders.show', compact('orders', 'clientList', 'productList'));
break;
}
}
}
But when i execute teh code i get this error:
Too few arguments to function App\Http\Controllers\OrderController::show(), 1 passed in /home/user/Desktop/k_project/vendor/laravel/framework/src/Illuminate/Routing/Controller.php on line 54 and exactly 2 expected
Basically the second argument is not passing i used another method like this:
Student detail
From this page: Page
While before i passing just one value $id and it worked fine but now i need a second parameter to pass.
This is my route definition that just one value passing $id works but with two values passing $id and $option will make error:
Route::resource('orders', OrderController::class);
Your route has 3 parameters id, parameter, name.
You can have two solutions for that.
1. First Solution
route.php
Route::get('/orders/show/{id}', 'OrderController#show')->name('orders.show');
OrderController.php
public function show(Request $request, $id)
{
$name = $request->input('name');
$parameter = $request->input('parameter');
....
}
2. Second Solution
route.php
Route::get('/orders/show/{id}/{name}/{parameter}', 'OrderController#show')->name('orders.show');
OrderController.php
public function show($id, $name, $parameter)
{
....
}

Argument 2 passed to Spatie\Searchable\SearchResult::__construct() must be of the type string, null given

im using spatie/laravel-searchable for my website.
it works very well in this function:
public function index(Request $request)
{
$results = (new Search())
->registerModel(Product::class, 'name', 'price','barcode')
->registerModel(Category::class, 'name')
->registerModel(Catalog::class, 'name')
->registerModel(Color::class, 'fatitle','entitle')
->search($request->input('query'));
return response()->json($results);
}
but in some words(like:cu006), i have this error:
Argument 2 passed to Spatie\Searchable\SearchResult::__construct() must be of the type string, null given
vendor/spatie/laravel-searchable/src/SearchResult.php:19
public function __construct(Searchable $searchable, string $title, ?string $url = null)
In your model, when you create the getSearchResult function
public function getSearchResult(): SearchResult
{
return new \Spatie\Searchable\SearchResult(
$this,
$this->title
);
}
If you write $this->title you need to make sure that your model actually contains the title field, if it doesn't it'll give you that error.
public function getSearchResult(): SearchResult
{
$companySlug = currentCompanySlug();
$url = url('/'.$companySlug.'/'.config('global-search- url.'.class_basename($this)));
$null = null;
return new SearchResult($this, $this->field_name ?:$null, $url);
}
please add in your model.

Retrieve parameters from custom validation object

I have basic custom validation rule. In
public function passes($attribute, $value)
{
foreach ($parameters as $key)
{
if ( ! empty(Input::get($key)) )
{
return false;
}
}
return true;
}
I have my rule defined. I, although need to retrieve parameters from the rule but the passes method does not provide it as an argument.
If I would use the style Validator:extends... that provides 4 arguments: $attribute, $value, $parameters, $validator. Then I could use the parameters easily, unfortunatelly I have to use this way.
EDIT:
To clear the question. I want to retrieve the parameters of the rule, like so in other way of coding it:
'not_empty:user_id'. The array of values behind the colon.
Edit:---
The custom rule object is simply an object. If you want to pass it any more parameters you can in it's constructor:
$request->validate([
'name' => ['required', new MyCustomRule('param', true, $foo)],
]);
Then save those and use them in your passes function.
public function __construct($myCustomParam){
$this->myCustomParam = $myCustomParam;
}
Then in your passes function use:
$this->myCustomParam
I believe the only way is to retrieve it from the request when using rule objects.
For example:
public function passes($attribute, $value)
{
foreach ($parameters as $key) {
// Or using \Request::input($key) if you want to use the facade
if (!empty(request()->input($key)) {
return false;
}
}
return true;
}

Laravel internal links

I'm looking for some code example which make keyword from post into internal link.
In post table I have body attribute where is text of post. And I make extra table "keyword" where is word and URL attributes.
So i need script which will automatically recognize word which is adding in table "keywords" and found this word in post text and turn this word to hyperlink. Can you guys help me, please?
use App\Post;
use App\Category;
use App\Keyword;
public function getSingle($slug) {
// fetch from the DB based on slug
$categoriesall = Category::all();
$post = Post::where('slug', '=', $slug)->first();
// return the view and pass in the post object
return view('news.single')->withPost($post)->withCategoriesall($categoriesall);
}
This is just a starting point for whatever you are building
public function getSingle($slug) {
$keywords = Keyword::get()->toArray();
$categoriesall = Category::all();
$post = Post::where('slug', '=', $slug)->first();
$post->text = $this->transform($keywords, $post->text);
return view('news.single', compact('post', 'categoriesall'));
}
private function transform($keywords, $text)
{
//takes 2 parameters
//1st parameter is keywords array
//2nd parameter is text to be transformed
$words = explode(" ", $text); //split wherever we have space
foreach( $words as $key => $value ) {
if ( in_array($value, $keywords) ) {
$words[$key] = ". $value .";
}
}
$paragraph = implode(" ", $words);
return $paragraph;
}
Keep in mind, this is a starting point as this solution is not optimized at all. e.g: if your text contains html tags, those tags might be converted as anchors because this algorithm uses space as a delimiter; and most of the time there are spaces in html.

Input array loop on controller laravel 5

I have inputs array and i need to make a foreach but laravel $request->all() only return last one:
url:
http://localhost:8000/api/ofertas?filter_pais=1&filter_pais=2&filter_pais=3
controller:
public function filtroOfertas(Request $request){
return $request->all();
}
result:
{"filter_pais":"3"}
result should return 1, 2 and 3 and i need to make a foreach in filter_pais.
Any solution? Thanks
Use [] at the key of query string.
http://localhost:8000/api/ofertas?filter_pais[]=1&filter_pais[]=2&filter_pais[]=3
It will be parsed as array.
Repeated parameters make no sense and should be avoided without exception.
But looking at other solutions, there are several:
routes.php
Route::get('/api/ofertas/{r}', 'Controller#index');
Controller:
public function index($r)
{
$query = explode('&', $r);
$params = array();
foreach($query as $param)
{
list($name, $value) = explode('=', $param);
$params[urldecode($name)][] = urldecode($value);
}
// $params contains all parameters
}
Given that the URL has no question marks:
http://localhost:8000/api/ofertas/filter_pais=1&filter_pais=2&filter_pais=3

Resources