How do you pass an array to mergeWhen function in Laravel? - php

I have this code.
$this->mergeWhen($request->stats=='partner_count',
['partner_count' => $this->partner_count])
Instead of $request->stats=='partner_count', I want to pass an array of request values. Is it possible or not?

Yes you can. As per the Laravel Manual you can pass an array like this
$this->mergeWhen($this->isAdmin(), [
'first-secret' => 'value',
'second-secret' => 'value',
]),

Related

Passing multiple values to same parameter with a PHP array with CURL

I'm need to execute a REST API POST request with PHP.
The URL post parameters looks like this and works fine when executed as such:
https://example.com.json?userid=MYUSER&key=MYKEY&order=000000&server=ns1.example.com&server=ns2.example.net&server=ns3.example.net
I use the following PHP array with success before without any issues for passing unique values to the same API in my PHP Curl Request:
$data = array(
'userid' => 'value',
'key' => 'value',
'order' => 'value',
);
The problem now, looking at the URL is that the "server" parameters accepts more than one value (order is not important) and I'm supposed to pass this as an string of arrays, minimum of 2 values.
But this does not work:
$data = array(
'userid' => 'value',
'key' => 'value',
'order' => 'value',
'server' => array('ns1.example.net','ns2.example.net')
);
I tried multiple different ways. How do I pass multiple values to the same parameter on that array?

How to Validate multidimensional array and use request value as parameter in Laravel 5.4?

I have this $request multidimension array
$this->validate(
$request, [
'items' => 'required|array',
'items.*.Quantity' => 'numeric|min:' . $request['items']['Packages']['min'] . '|max:' . $request['items']['Packages']['max'],
'items.*.Link' => 'required|url',
]
);
Notice that Packages is also array, how do I access that value in validate. If I use it like that it gives array to string conversion of course .I can do it with ForEach in other complicated method but I want to know if is there a simple way in Laravel ?

(Return view) is returning a limited number of variables

I'm simply trying to return three variables as shown below. I'm receiving an undefined variable: udraft which is the third variable. If I make them only two variables, it will work. Is there some limitation here?
return view('tabs', ['post' => $posts] , ['acc' => $acc_usrs] , ['udraft' => $usr_draft]);
As stated in the Laravel Documentation, here, you should pass an array of data, not a single array for each variable.
Your code should be something like:
return view('tabs', ['post' => $posts, 'acc' => $acc_usrs, 'udraft' => $usr_draft]);
Or use the ->with() method as mentioned in the documentation above.
Why not returning one array like this:
return view('tabs', [
'post' => $posts,
'acc' => $acc_usrs,
'udraft' => $usr_draft
]);
you can use compact
return view('tabs', compact('posts','acc_usrs','usr_draft'));

Laravel 5.1 array

I am using Laravel 5.1
And I need to convert array to diffrent array
So I am having
$wells = Well::get(array('well_id' => 'url','well' => 'name','iso3'))->toArray();
$users= User::get(array('id' => 'url', 'name' => 'name'))->toArray();
So from bouth of the arrays the output should be converted from well_id to url from wells and id to url from users. So even if they are different from the database the output will have the same 'name' because after I am having a function for both but it uses this 'name'.
So the examle I have given is not working but it should be something like this.
Thank you.
You can use select function of Laravel
$wells = Well::select('well_id as url','well as name','iso3')->get()->toArray();
and
$users= User::select(array('id' => 'url', 'name' => 'name'))->get()->toArray();
i haven't try it but it should work.

Problems with using hmset in Predis

I have a problem with using $predis->hmset(). What parameters i need use?
I try many variants, but without success.
$this->client()->hmset( $this->name, array( 1 => 3 ))
$this->client()->hmset( $this->name, array( 1, 3 ))
From the predis examples:
// Prepare an hash with some fields and their respective values.
$client->hmset('metavars', array('foo' => 'bar', 'hoge' => 'piyo', 'lol' => 'wut'));
Perhaps, make sure you use strings and not integers ...

Categories