Difference to send on sight with compact in laravel - laravel

what is the difference when sending data to the view of the two ways I get the same result
return view ('admin.about.index', compact ('about'));
return view ('admin.about.index') -> with (compact ('aboutsImage'));

compact method support multiple variable to pass,
with method supports only one variable to pass, also you can declare it in method and still using it multiple times>
...->with('about','aboutImage')->with('text','aboutText')...
if you already have variables filled and need to pass more than two, compact mode will be easier.

To pass data to a view you can do both:
return view('greetings', ['admin' => $user, 'store' => $store]);
or
return view('greetings')->with('admin', $user)->with('store', $store);
// sugared
return view('greetings')->withAdmin($user)->withStore($store);
With these two methods, you still receive your object in the view under the variable names that you define.
On the other hand, when you do compact() it will create an associative array of the object/collection that you pass it in. It also let's you add multiple variables there.

Related

Implementing transpose in Laravel collection

This is a macro that I want to use it in Laravel collection. I know what the transpose is and how it works. But I didn't get exactly how this piece of code is implementing transpose in Laravel collection.
Can you explain how the items that we will pass, would be transposed?
What new static($items) does here?
Collection::macro('transpose', function () {
$items = array_map(function (...$items) {
return $items;
}, ...$this->values());
return new static($items);
});
The code you provided should be placed in a Service Provider according to the Laravel Docs: https://laravel.com/docs/8.x/collections#extending-collections. However, you could technically place them anywhere.
What exactly don't you understand about the piece of code? The macro function of the Illuminate\Support\Collection accepts a closure. In this case, it means $items is referring to the Collection that transpose is called on.
Then that Collection is mapped (using array_map) and the $items are passed as a spread operator function parameter. Perhaps you're familiar with the spread operator, but it spreads the values of an array (or other iterable) to separate variables.
In that same function, these separate variables are returned, and the values() are provided as a spread operator to run through the callback function. For more information about array_map, you can read the PHP Docs: https://www.php.net/manual/en/function.array-map.php.
If anything is unclear, please let me know what part needs clarification.

Returning multiple values in UDF

I have written an AggregateFactory Vertica UDF which returns a single value
getReturnTypes(si,columnTypes args,columnTypes returnTypes){
returnTypes.addVarbinary(512);
//I want to add second returnType
returnTypes.addFloat("");
}
getProtoType(si,columnTypes args,columnTypes returnTypes){
returnTypes.addVarbinary(512);
//I want to add second returnType
returnTypes.addFloat("");
}
this is not working, how can I return two values from an AggregateFactory UDF?
You cannot. User Defined Aggregate Functions (as explained in the fine manual) return ONE value per group. You might want to write a User Defined Transform Function (maybe a multi-phase Transform Function).

Post multiple parameters to web api controller action

I am working on a web api project (back end) and I am searching for some more optimized way to get multiple parameter from front end. There are numerous ways like
i) name each parameter in action parameter stack
ii) extract from request body like Request.Content.ReadAsStringAsync().Result;
iii) define complex types (model) and use that type to receive values. like MyAction(UserLog log)
I have to create hundreds of functions which may take variable number of parameters. I don't want to use first option above, it is hectic for large data.
The second is confusing as no one can predict what to post.
The third one forces me to create hundreds of input models. So is there any better way to do so?
You could try using a form data collection
Its basically a Key Value Pair which allows for an element of genericness.
Edit - another link
Another option would be to apply dynamic object parameters on your Post Verbs.
E.g.
public string Post(dynamic value)
{
string s = "";
foreach (dynamic item in value)
{
s = s + item.content + " ";
}
return s;
}

Returning multiple values from a method

I have a method drive that goes like this:
public double drive(double milesTraveled, double gasUsed)
{
gasInTank -= gasUsed;
return totalMiles += milesTraveled;
}
I know I can't return multiple values from a method, but that's kind of what I need to do because I need both of these values in my main method, and as it is now it's obviously only returning the one. I can't think of anything that would work. Sorry if this is a super beginner question. What can I do to get both values to return from the method?
You can return multiple value from a function. To do this You can use structure.
In the structure you can keep required field and can return structure variable after operation.
You can also make a class for the required field if You are using OOPS supporting language but Structure is best way.
In most languages you can only return a single value from a method. That single value could be a complex type, such as a struct, array or object.
Some languages also allow you to define output parameters or pass in pointers or references to outside storage locations. These kinds of parameters also allow you to return additional values from your method.
not sure, but can you take array of your values?
array[0]=gasInTank;
array[0] -= gasUsed;
array[1]=milesTraveled;
array[1] -= milesTraveled;
return array;

Why does ShellTile.ActiveTiles.Count() return different results on immediately subsequent calls?

Consider the following:
IEnumerable<ShellTile> pinnedtiles = ShellTile.ActiveTiles;
Console.WriteLine(pinnedtiles.Count());
Console.WriteLine(pinnedtiles.Count());
Assuming you have more than 0 ActiveTiles, the first call to Count() will return the correct value, but the second call will return 0.
If you dont set ShellTile.ActiveTiles to a local variable, it works fine. I assume this is because ActiveTiles is actually an instance of the internal class ShellTileEnumerator and for some reason, when accessed via the IEnumerable interface, it is acting like a forward-only enumerator. Seems like a likely 'gotcha', or am I misunderstanding something?
Yes you are right
MessageBox.Show(ShellTile.ActiveTiles.Count().ToString());
MessageBox.Show(ShellTile.ActiveTiles.Count().ToString());
The above one will work but not when you assign to IEnumeable .... :)
Another easy way is to assign it toa List instead of IEnumerable . This works too
List<ShellTile> pinnedtiles = ShellTile.ActiveTiles.ToList(); ;
MessageBox.Show(pinnedtiles.Count().ToString());
MessageBox.Show(pinnedtiles.Count().ToString());

Resources