#Laravel #PHP Do you use compact() when passing data to views....? Is it an elegant shorthand or readability killer? Drop your thoughts in the comments!๐
21
8
100
Replies
Enjoyed this tweet? Follow us - you will probably enjoy our other posts: https://t.co/q0R9wa0bh6
0
0
0
@laravelbackpack What does it do? Grab the variable by named reference from $_GLOBALS or something? I have never seen this before.
0
0
0
@laravelbackpack I don't use it ... it makes debugging harder when you need to track down where variables come from
0
0
12
X is throttling my posts.its also showing me a bunch of 1diot leftoid posts even though I muted them. My posts get 15 views with 3k plus followers? Might as well be 2017 again...hey @elonmusk ...WHAT THE FCK?!!!
0
0
6
@laravelbackpack I always use this : $data = [] ; $data['user'] = $user; return view('dashboard', $data); Then in blade {{$user->id}} etc
0
0
1
@laravelbackpack I used it in the early days (Laravel 4โ5). It's great for smaller projects with fewer variables to push to the view, but after 6โ10 attributes it becomes too much work to maintain. Pass an array where you can manage the name of your variables directly or a ViewModel/DTO.
0
0
3
@laravelbackpack I like it. It's the PHP version of object shorthand (`view('dashboard', { user, posts })`) in JavaScript.
0
0
2
@laravelbackpack I use compact() most of the time โ itโs clean and quick. But when sharing code with juniors or teams, explicit arrays are definitely more readable ๐
0
0
0
@laravelbackpack While I like itโs readability, I donโt do it because vscode doesnโt understand that itโs the $user variable, I would have loved to do compact($user, $posts)
1
0
2
@laravelbackpack it was fine back in the day, but we now have better solutions that allow for better diffs and help avoid unnecessary temporary variables.
0
0
0
@laravelbackpack The only performance cost for this function that it checks of variable exists or not, skips any undefined variable without stopping the script. It shows a warning but still builds the array, which leads to additional cpu cycles unnecessarily. Just be aware
0
0
0
The pharmaceutical fallacy lies with the idea that everyone claims price controls will eliminate profitability and decrease the profit motive that fuels the growth of medicines. This is only partially true because a profit motive is required, but how much? This contributes to the
3
4
27
@laravelbackpack Back in 2015, I worked for @opencart and there we used to define the variable as an array and pass it directly to the view. So sometimes or often, I do the same in Laravel ๐
0
0
3
@laravelbackpack It does not only kill readability it makes your views break upon renaming any compacted variable. Honestly shouldn't be in the framework anymore.
0
0
2