首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Laravel 8上更新帖子的问题

在Laravel 8上更新帖子的问题
EN

Stack Overflow用户
提问于 2021-01-15 03:32:44
回答 1查看 64关注 0票数 0

我使用的是Laravel 8;我可以创建帖子,但不能更新帖子。下面是我的update函数。如果我的代码中有任何错误,请随时指出。

代码语言:javascript
复制
public function update(Request $request, $id)
{
    $validated = $request->validate([
        'title' => 'required',
        'body' => 'required',
    ]);
    
    // Update Post
    $post = Post::find($id);
    $post->title = $request->input('title');
    $post->update($request->all());
    $post->save();
    
    return redirect('/posts');
}

我的编辑页面是:

代码语言:javascript
复制
@extends('layouts.apps')
@section('content')
<h1 class="font-bold text-3xl text-red-700 text-center"><strong>Edit this Post</strong> </h1>

@if (Auth::check()) 
@include('includes.messages')

    <form action= "posts/{post} " method="POST" class="mx-60 bg-red-300 border-2 border-black">
        @method('PUT')
        @csrf
        <div class="py-5">
            <label for="Title">Title</label>
            <br>
            <input type="text" name="title" id="title">
        </div>
        <div class="py-5">
            <label for="body">Description</label>
            <br>
            <textarea name="body" id="body" cols="50" rows="3" class="self-stretch"></textarea>
        </div>
        <div class="py-5">
            <label for="evidencePhoto">Attach photo</label>
            <br>
            <input type="file" name="evidencePhoto" id="evidencePhoto">
        </div>
        <div class="flex">
            <input type="reset" class="bg-gray-200 px-2 mx-4 text-center hover:border-white border-4 border-black md:rounded-lg">
            <button type="submit" class="px-4 w-24  hover:border-green-800 border-4 border-black bg-green-400 text-yellow md:rounded-lg"><strong>Post</strong></button>
            <a href="/posts" class="mx-4 px-2"><div class="bg-gray-200 text-center hover:border-red-700 border-4 border-black"><strong>>>Back to Posts<<</strong></div></a>
        </div>
    </form>
    <br>
    <div class="">
        <h1 class="font-bold text-3xl text-red-700 text-center"><strong>Warning!</strong> </h1>
        <p class="text-red-700 text-center"><strong>Please avoid the use of this platform for the spread of fake news.</strong> 
        You will be held responsible for any fake hazard alert you post on this platform.
        </p>
    </div>   
        
        
            
        @else
            
                <div class="flex justify-center px-10 place-content-center bg-red-200 text-center border-6 border-black">
                   <p> You are not authorized to post on this platform</p>
                  <strong><a href="/register" class="border-red.600 text-blue bg-gray-200 hover:text-red-600 border-4">Create an account</a> OR <a href="/login" class="text-blue border-red.600 bg-gray-200 hover:text-red-600 border-4">Log in</a>to be eligible.</strong>
                </div>
                
        @endif      
      
@endsection
EN

回答 1

Stack Overflow用户

发布于 2021-01-15 18:15:14

您正在使用()函数查找post,因此请将其替换为findOrFail()

  1. 首先使用id查找帖子,然后同时使用更新()和保存()函数,因此将会出错。

将其替换为

$post = Post::findOrFail($id);

$post->title = $request->input('title');

$post->body= $request->input('body');

$post->save();

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65725746

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档