当我尝试显示所有具有权限的帖子时,问题是未定义的偏移量。 如果没有对链接进行编码和解码,则永远不会发生此问题。 当我编码并尝试通过编码链接来编辑帖子时,这没问题。但是,当我尝试显示所有帖子时,发生了问题,并显示了未定义的偏移量[0]。 如何解决此问题?
我的管理员来编辑帖子
@php $parameter = Hashids::connection('main')->encode($post->id); @endphp
@if(check_user_permissions($request, "BlogPost@edit", $parameter))
<a href="{{route('admins-blogpost.edit', $parameter)}}" class="btn btn-xs btn-warning">
<i class="fa fa-edit"></i>
</a>
@else
<a href="#" class="btn btn-xs btn-warning disabled">
<i class="fa fa-edit"></i>
</a>
@endif
我的观点
<?php
use Vinkla\Hashids\Facades\Hashids;
function check_user_permissions($request, $actionName = NULL, $id = NULL)
{
$currentUser = $request->user();
if($actionName)
{
$currentActionName = $actionName;
}
else{
$currentActionName = ($request->route()->getActionName());
}
list($controller, $method) = explode('@', $currentActionName);
$controller = str_replace(["App\\Http\\Controllers\\Backend\\", "Controller"], "", $controller);
$crudPermissionsMap = [
'crud' => ['create', 'store', 'edit', 'update', 'destroy', 'restore', 'forceDestroy', 'index', 'view']
];
$classesMap = [
'BlogPost' => 'post',
'CategoriesPost' => 'category',
'Users' => 'user'
];
foreach($crudPermissionsMap as $permission => $methods){
if(in_array($method, $methods) && isset($classesMap[$controller]))
{
$classesName = $classesMap[$controller];
// dd("{$permission}-{$classesName}");
if($classesName == 'post' && in_array($method, ['edit', 'update', 'restore', 'destroy', 'forceDestroy']))
{
$id = !is_null($id) ? $id : $request->route("admins_blogpost");
if( $id
&& (!$currentUser->can('update-others-post') || !$currentUser->can('delete-others-post')))
{
$post = \App\Post::withTrashed()->find(Hashids::decode($id)[0]);
if($post->author_id != $currentUser->id){
return false;
}
}
}
elseif(! $currentUser->can("{$permission}-{$classesName}")){
// abort(403, "forbidden access!");
return false;
}
break;
}
}
return true;
}
检查权限的方法和方法
{"status": "success", "data": { "candles":[["2018-10-09T09:15:00+0530",6830.7,6940,6830.7,6903.6,232210],["2018-10-09T10:15:00+0530",6903.7,6905.8,6824,6833.65,126885],["2018-10-09T11:15:00+0530",6831.15,6857,6750.15,6767.95,152549]
答案 0 :(得分:0)
对不起,这是我的错。我不检查所有帖子。
必须$post = \App\Post::withTrashed()->find(Hashids::decode($id)[0]?? $id));