英文:
What is the difference between $request->merge() and $request->mergeIfMissing()
问题
基本上,我正在创建一个中间件来检查与用户订阅相关的权限。在请求传递到控制器之前,我需要将权限数据添加到用户的请求中。我发现 $request->merge()
可以实现这一点。
Laravel 文档中说:
有时,您可能需要手动将附加的输入数据合并到请求的现有输入数据中。为了实现这一点,您可以使用
merge
方法:
$request->merge(['votes' => 0]);
如果相应的键在请求的输入数据中不存在,可以使用
mergeIfMissing
方法将输入数据合并到请求中:
$request->mergeIfMissing(['votes' => 0]);
因此,我创建了一个测试路由、一个中间件,并在 Kernel 中进行了注册:
// web.php
Route::get('test', [PortalController::class, 'showHomePage'])->middleware('check.user.permission')->name('test');
// Kernel.php
'check.user.permission' => \App\Http\Middleware\CheckUserPermission::class,
// CheckUserPermission.php
public function handle(Request $request, Closure $next)
{
$request->merge(['permission' => 'free']);
dd($request->permission);
return $next($request);
}
无论我使用 merge()
还是 mergeIfMissing()
,我都会得到相同的结果。
英文:
Basically, I'm creating a middleware to check user permission related to their subscription.
I need to add permission data to user's request before it came to Controllers.
I found $request->merge() can do that.
Laravel Doc says:
> Sometimes you may need to manually merge additional input into the request's existing input data. To accomplish this, you may use the merge method:
$request->merge(['votes' => 0]);
> The mergeIfMissing method may be used to merge input into the request if the corresponding keys do not already exist within the request's input data:
$request->mergeIfMissing(['votes' => 0]);
So I create a test route, a middleware, and registered in Kernel:
// web.php
Route::get('test', [PortalController::class, 'showHomePage'])->middleware('check.user.permission')->name('test');
// Kernel.php
'check.user.permission' => \App\Http\Middleware\CheckUserPermission::class,
// CheckUserPermission.php
public function handle(Request $request, Closure $next)
{
$request->merge(['permission' => 'free']);
dd($request->permission);
return $next($request);
}
If I do merge() or mergeIfMissing() I get the same result.
答案1
得分: 0
Merge()方法将一个数组或集合的元素与另一个数组或集合合并。如果存在重复的键,来自第二个数组或集合的值将覆盖来自第一个的值。
$collection1 = collect(['permission' => 'not free']);
$collection2 = collect(['permission' => 'free']);
$result = $collection1->merge($collection2);
// 输出:['permission' => 'free']
$collection2中键 'permission' 的值覆盖了$collection1中的值。
MergeIfMissing()方法仅在第二个集合的键在第一个集合中不存在时才合并元素。
$collection1 = collect(['permission' => 'not free']);
$collection2 = collect(['permission' => 'free']);
$result = $collection1->mergeIfMissing($collection2);
// 输出:['permission' => 'not free']
因为键 'permission' 已经存在于$collection1中,所以它不会被$collection2中的值覆盖。
英文:
It's because you're not merging any key which already exist in the initial request.
merge(): the new data will overwrite the old value.
mergeIfMissing(): will keep it.
Merge()
Merge() method combines the elements of one array or collection with another array or collection. If there are duplicate keys, the values from the second array or collection will overwrite the values from the first one.
$collection1 = collect(['permission' => 'not free']);
$collection2 = collect(['permission' => 'free']);
$result = $collection1->merge($collection2);
// Output: ['permission' => 'free']
The value of key 'permission' in $collection2 overwrites the value from $collection1.
MergeIfMissing()
MergeIfMissing() method merges the elements only if the keys from the second collection are not already present in the first one.
$collection1 = collect(['permission' => 'not free']);
$collection2 = collect(['permission' => 'free']);
$result = $collection1->mergeIfMissing($collection2);
// Output: ['permission' => 'not free']
Since the key 'permission' already exists in $collection1, it is not overwritten with the value from $collection2.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论