php 如何在laravel中更新数据库中的数据?

ogsagwnx  于 2022-10-30  发布在  PHP
关注(0)|答案(2)|浏览(179)

下面是我目前的代码:控制器:
公共函数编辑($id){ $post =帖子::查找($id);

return view('edit', compact('post'));
}

public function update(Request $request, $id){
    $post = Posts::find($id);

    $validatedRequests = $request->validate([
        'title' => 'required|max:255|string|integer',
        'description' => 'required|max:255|string|integer',
        'price' => 'required|integer|max:255|'
    ]);

    $post->create($validatedRequests);

    return redirect('/Post/{{$post->id}}')->with('mssg', 'updates successfully');
}

Web.php:

Route::get('/post/{id}/edit', [PostController::class, 'edit']);

/post/{id}/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
查看方式:

<form method="POST" action="/post/{{$post->id}}/update">
    @csrf
    @method('PUT')
    <label class="underline" for="title">change title:</label></br>
    <input type="text" name="title" value="{{$post->title}}"></input></br>
    <label class="underline" for="decsription">change description:</label></br>
    <input type="text" name="description" value="{{$post->description}}"></input></br>
    <label class="underline" for="price">change price:</label></br>
    <input type="text" name="price" value="{{$post->price}}"></input></br>

    <input type="submit" value="submit">
</form>
11dmarpk

11dmarpk1#

在控制器中

public function edit(Request $request, $id)
{
   $post = Post::find($id);
   $post->title = $request->title;
   $post->description = $request->description;
   $post->price = $request->price;
   $post->save();
   return redirect('/Post/{{$post->id}}')->with('mssg', 'updates successfully');
}

您的路线

Route::post('/post/{id}/edit', [PostController::class, 'edit']);

您的观点

<form method="POST" action="/post/{{$post->id}}/edit">
@csrf
<label class="underline" for="title">change title:</label></br>
<input type="text" name="title" value="{{$post->title}}" required></input></br>
<label class="underline" for="decsription">change description:</label></br>
<input type="text" name="description" value="{{$post->description}}" required></input></br>
<label class="underline" for="price">change price:</label></br>
<input type="text" name="price" value="{{$post->price}}" required></input></br>

<input type="submit" value="submit">
4dc9hkyq

4dc9hkyq2#

您的控制器应该:

public function edit(Request $request, Post $post) {
        $validatedRequests = $request->validate([
            'title' => 'required|max:255|string|integer',
            'description' => 'required|max:255|string|integer',
            'price' => 'required|integer|max:255|'
        ]);    

        $post->update([
            'title' => $request->title,
            'description' => $request->description,
            'price' => $request->price    
        ]);    

        $post->save();

        return redirect('/Post/{{$post->id}}')->with('mssg', 'updates successfully'); 
}

您的路线

Route::post('/post/{post:id}/edit', [PostController::class, 'edit'])->name('post.update');

您的视图应该是:

<form method="POST" action="{{route('post.update', ['post'=> $post->id])}}">
@csrf
<label class="underline" for="title">change title:</label></br>
<input type="text" name="title" value="{{$post->title}}" required></input></br>
<label class="underline" for="description">change description:</label></br>
<input type="text" name="description" value="{{$post->description}}" required></input></br>
<label class="underline" for="price">change price:</label></br>
<input type="text" name="price" value="{{$post->price}}" required></input></br>

<input type="submit" value="submit">

相关问题