【问题标题】:Laravel - Post Policy Didn't Redirect Back to Post IndexLaravel - 帖子政策没有重定向回帖子索引
【发布时间】:2021-01-05 00:03:57
【问题描述】:

我正在使用 laravel 框架学习网络。我在创建授权策略时遇到问题。

问题是我无法使用 Create 重定向回 post.index,并且没有保存在 phpMyAdmin 中。编辑、更新和删除功能。

这是我的代码:

PostController.php

    public function create(){
        return view ('admin.posts.create');
    }

    public function store(){
        $this->authorize('create', Post::class);
        $inputs = request()->validate([
            'title'=>'required|min:8|max:255',
            'post_image'=>'file', //mime: jpeg, png
            'body'=>'required'
        ]);
        if(request('post_image')){
            $inputs['post_image'] = request('post_image')->store('images');
        }
        auth()->user()->posts()->create($inputs);
        session()->flash('post-create-message', 'Post was Created ' . $inputs['title']);
        // return back();
        return redirect()->route('post.index');
    }

    

    

还有这个 PostPolicy.php:

public function create(User $user)
    {            
        return $user->is($user);
    }

【问题讨论】:

  • 如果没有重定向则为验证错误
  • 你能给我提供更多详细信息吗?....成功登录并尝试创建帖子后,它什么也没做,也没有给我任何错误
  • 那么您正在提交表单吗?
  • 是的,我尝试提交新帖子但没有保存它...它也没有给我任何错误...在帖子中的读取、编辑和删除功能工作时。
  • 'post_image'=>'sometimes|file'再试一次,你的验证需要一个现在的文件。没有上传文件=失败。除非那是你想要的;

标签: php laravel authentication web


【解决方案1】:

在你的 from put 里面是验证错误,你可以通过这个代码检查

@if (count($errors) > 0)
    <div class="error">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

【讨论】:

    猜你喜欢
    • 2017-10-05
    • 2021-04-27
    • 2016-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多