In my php symfony app I got the following parameters.yml:
my_param: some_value
my_nested:
param: some_value2 //4 spaces here, need to set only this param
param2: some_value3 // this should NOT be changed
I can set the parameter in a container with the following code:
$container->setParameter('my_param', $some_value);
it works fine, but I need to set a nested parameters, like this:
$container->setParameter('my_nested.param', $some_value);
I get an error saying that it cannot set nested param. Any ideas how to fix that? my_nested.param2 (and other nested params here) should not be changed. Thank you.
You need to use an array-like structure, like this:
$container->setParameter('my_nested', [
'param' => $some_value_2,
]);
Have a look here: https://symfony.com/doc/current/service_container/parameters.html
An example to change a nested parameter:
$container->setParameter('my_nested', [
'top' => [
'nested' => 'a',
]
]);
$parameter = $container->getParameter('my_nested');
$parameter['top']['nested'] = 'b';
$container->setParameter('my_nested', $parameter);
you can define the value like that :
the parameters.yml :
parameters:
my_param: some_value
my_nested.param: some_value2
you can get the value as you show in your description , but modify this value is impossible .
symfony will notify you with the error :
Impossible to call set() on a frozen ParameterBag.
Related
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.
I have a line of code similar to the following:
Sport::pluck('id', 'name)
I am dealing with frontend JavaScript that expects a list in this format:
var list = [
{ text: 'Football', value: 1 },
{ text: 'Basketball', value: 2 },
{ text: 'Volleyball', value: 3 }
...
]
I am trying to figure out how I can somehow transform the id and name values that I pluck from my model to a format similar to the Javascript list.
If that's unclear, I am looking to end up with an associative array that contains two keys: text and value, where text represents the name field on my model, and where value represents the id of the model - I hope this makes sense.
How would I approach this?
I initially tried something like this (without checking the documentation)
Sport::pluck(["id" => "value", "name" => "text]);
But that isn't how you do it, which is quite clear now. I've also tried some map-related snippet, which I cannot seem to Ctrl-z to.
Any suggestions?
Another method is to use map->only():
Sport::all()->map->only('id', 'name');
The purpose of pluck is not what you intend to do,
Please have a look at below examples,
Sport::selectRaw("id as value, name as text")->pluck("text","value");
// ['1' => 'Football', '2'=>'BasketBall','3'=>'Volleyball',...]
Syntax
$plucked = $collection->pluck('name', 'product_id');
// ['prod-100' => 'Desk', 'prod-200' => 'Chair']
Please see the documentation.
Your output is possible using simple code.
Sport::selectRaw('id as value, name as text')->get();
You could use map.(https://laravel.com/docs/5.8/collections#method-map)
$mapped = Sport::all()->map(function($item, $index) {
return [
"id" => $item["id"],
"name" => $item["text"]
];
});
This is the easiest way. Actually Laravel offers a better way for it. You can use api resources to transform your data from eloquent for the frontend:
https://laravel.com/docs/5.8/eloquent-resources
Try with toArray function:
Sport::pluck('id', 'name)->toArray();
Then you can return your result with json_encode php function;
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'],[....]])
I was wondering how to perform something like this:
Table::update(array('position'=>'position+1'));
As far as I know, laravel 4 handles 'position+1' as a string, thus is becomes 0.
I want to perform something like
UPDATE table SET position = position + 1
Can I do that using eloquent?
EDIT: nevermind, doh.."DB::table('users')->increment('votes');"
Simply make use of the increment method:
DB::table('users')->increment('position');
The same is valid for decrement:
DB::table('users')->decrement('rank');
You may even set the second parameter to the amount you want to add/subtract:
DB::table('users')->increment('posts', 5);
DB::table('users')->decrement('likes', 3);
Also, if you need to update other columns along with it, you pass it as the third parameter:
DB::table('users')->increment('range', 3, array(
'name' => 'Raphael',
'rank' => 10
));
And the same goes for Eloquent models, as well:
$firstUser = User::find(1);
$firstUser->increment('height', 0.1, array(
'active' => false
));
you can also do with DB::raw method like this:
DB::table('tablename')->where(your condition)->update(['position' => DB::raw('position+1')]);
you can also do other operations with this like
DB::table('tablename')->where(your condition)->update(['position' => DB::raw('position * 2')]);
This worked fine for me
\Models\User::where("id", $userId)->increment("points");
simply you can use the DB::raw method like this:
Table::update(DB::raw('position=position+1'));
I am building a simple messaging system, and i have a collection in mongodb with documents like this:
{ "_id" : ObjectId("50ad003f9811e5bc5c000000"), "between" : [ "user1,user2,user3" ] }
I want to perform this query:
db.conversations.find({'between': ["user1,user2,user3"]});
to get this exact document back. This query works in mongo shell.
in php-mongo, i tried this:
$collection->find(array("between"=>array("user1", "user2", "user3")));
but it does not work.
What am i doing wrong ?
Wouldn't you want to do an In query here?
db.collection.find( { "between" : { $in : ["user1", "user2", "user3"] } } );
See In query here:
Mongo Advanced $in query
making your PHP query look like:
$collection->find(array("between"=>array("$in"=>array("user1", "user2", "user3"))));
//untested, should be something similar to this.
or if you're trying to find it exactly wouldn't you just be able to do:
$collection->find(array("between"=>array("user1,user2,user3")));
First of all when you are saving your data you have to use array not a string
{ "between" : [ "user1,user2,user3" ] }
this stores in "between" an array of one element "user1,user2,user3"
Basically when you do your query in shell everything is ok, because you are asking for a array with one element. But in php you are asking for an array of three elements.
So, when you save your data, most probably that is what you need :
{ "between" : [ "user1","user2","user3" ] }
and this will give you an array of three users.
Then read the documentation http://www.mongodb.org/display/DOCS/Advanced+Queries to and adjust your query depends on what you need: either the exact array or only some elements in the array
Have you tried:
$collection->find(array("between"=>"user1,user2,user3"));
or
$collection->find(array( "$elemMatch" => array( "between"=>"user1,user2,user3" ));
The $in operator is analogous to the SQL IN modifier, allowing you to specify an array of possible matches.
Consider the following example which uses the $or operator.
$collection->find([
'select' => ['$in' => ['option 1', 'option 2', 'option 3']]
]);
References