首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在laravel-9中将参数传递给具有窗体的控制器

在laravel-9中将参数传递给具有窗体的控制器
EN

Stack Overflow用户
提问于 2022-07-23 19:57:33
回答 1查看 1K关注 0票数 0

我想将$category发送给控制器,但是当我在控制器中增强$category时,我发现它是空的。

我在edit.blade.php中的表格

代码语言:javascript
复制
    <form action="{{ route('categoryUpdate', $category) }}" method="post" class="ui form">
        @method('put')
        @csrf
        <div class="field">
            <label for="title">title</label>
            <input type="text" name="title" value="{{$category->title}}" />  
        </div>
        <div class="field">
            <label for="description">description</label>
            <textarea name="description">{{$category->description}}</textarea>
           
        </div>
        <div class="field">
            <label for="active">status</label>
            <select name="active">
                <option value="0" @if($category->active==0) selected @endif>Not Active</option>
                <option value="1" @if($category->active==1) selected @endif> Active</option>
            </select>
        </div>
        <div class="field">
            <button type="submit" class="ui primary button"> edit</button>
        </div>
    </form>

我的web.php

代码语言:javascript
复制
Route::get('/category/{category}',[CategoryController::class,'show'])->name('categoryShow');
Route::get('/category/edit/{category}',[CategoryController::class,'edit'])->name('categoryEdit');

我的控制器

代码语言:javascript
复制
public function update(UpdateCategoryRequest $request, Category $category)
    {
        dd($category);
    }
EN

回答 1

Stack Overflow用户

发布于 2022-07-23 21:39:41

为了获得更新,您需要执行以下操作:表单中的更改:

代码语言:javascript
复制
<form action="{{ route('categoryUpdate', $category) }}" method="post" class="ui form">
        @csrf
        <div class="field">
            <label for="title">title</label>
            <input type="text" name="title" value="{{$category->title}}" />  
        </div>
        <div class="field">
            <label for="description">description</label>
            <textarea name="description">{{$category->description}}</textarea>
           
        </div>
        <div class="field">
            <label for="active">status</label>
            <select name="active">
                <option value="0" @if($category->active==0) selected @endif>Not Active</option>
                <option value="1" @if($category->active==1) selected @endif> Active</option>
            </select>
        </div>
        <div class="field">
            <button type="submit" class="ui primary button"> edit</button>
        </div>
    </form>

路线:

代码语言:javascript
复制
Route::post('/category/{category}',[CategoryController::class,'store'])->name('categoryStore');
Route::post('/category/edit/{category}',[CategoryController::class,'update'])->name('categoryUpdate');

当我们处理API时,我们使用PUT更新实体,但是当您处理web表单时,使用了POST方法,这是更新实体的最流行方法。

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

https://stackoverflow.com/questions/73093846

复制
相关文章

相似问题

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