【发布时间】:2021-02-23 01:23:53
【问题描述】:
我有这个表格,我想做的是如果这个id的数据存在,我的提交按钮变成更新功能。
我仍然为我的控制器使用存储数据功能,因为我不知道如何执行逻辑。
public function store(Request $request)
{
$request->validate([
'title' => 'required',
'description' => 'required',
'category_page_id' => 'required',
]);
$seo = new Seo;
$seo->category_page_id = $request->input('category_page_id');
$seo->title = $request->input('title') ?? '';
$seo->description = $request->input('description');
$seo->save();
return back()->withStatus(__('SEO successfully updated.'));
}
这是我的更新功能的形式
<form method="post" action="{{ route('seo.update') }}" autocomplete="off">
@csrf
@method('put')
<h3 class="heading-small mb-4">{{ __('PAGE SEO') }}</h3>
@if (session('status'))
<div class="alert alert-success alert-dismissible fade show" role="alert">
{{ session('status') }}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
@endif
<div class="pl-lg-4">
<div class="form-group{{ $errors->has('category_page_id') ? ' has-danger' : '' }}">
<div class="form-group form-box">
<label for="category_page_id">Page</label>
<select class="form-control form-control-alternative{{ $errors->has('category_page_id') ? ' is-invalid' : '' }}" name="category_page_id">
@foreach ($categorypages->slice(0, 1) as $key => $value)
<option value="{{ $value->id }}" @if (old('category_page_id') == $value->id) {{ 'selected' }} @endif>{{ $value->name }}</option>
@endforeach
</select>
@error('category_page_id')
<small class="text-red">{{ $message }}</small>
@enderror
</div>
</div>
<div class="form-group{{ $errors->has('title') ? ' has-danger' : '' }}">
<label class="form-control-label" for="input-title">{{ __('SEO Title') }}</label>
<input type="text" name="title" id="input-title" class="form-control form-control-alternative{{ $errors->has('title') ? ' is-invalid' : '' }}" placeholder="{{ __('title') }}" value="{{ old('title', $seo->title) }}" required autofocus>
@if ($errors->has('title'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('title') }}</strong>
</span>
@endif
</div>
<div class="form-group{{ $errors->has('description') ? ' has-danger' : '' }}">
<label class="form-control-label" for="input-description">{{ __('Description') }}</label>
<input type="description" name="description" id="input-description" class="form-control form-control-alternative{{ $errors->has('description') ? ' is-invalid' : '' }}" placeholder="{{ __('Description') }}" value="{{ old('description', $seo->description) }}" required>
@if ($errors->has('email'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
<div class="text-center">
<button type="submit" class="btn btn-success mt-4">{{ __('Save') }}</button>
</div>
</div>
</form>
我可以做些什么来解决我的问题?有什么方法可以正确地做到这一点吗?
【问题讨论】:
-
提供控制器,该控制器返回视图是什么形式并提供该视图
-
@lagbox 好的,我会编辑一下
-
我将表单刀片放在我的问题中,请看一下@lagbox
-
以及返回此视图的控制器方法
-
也许你可以运行一个三元条件来设置值的标签!如果对象不为空,则可以将其设置为更新或将其设置为保存。