如果数据存在,提交按钮变为更新功能(Laravel)

如果数据存在,提交按钮变为更新功能(Laravel),laravel,forms,Laravel,Forms,我有这个表单,我想做的是如果这个id的数据存在,我的提交按钮变成更新功能 我仍然为我的控制器使用存储数据功能,因为我不知道如何执行逻辑 public function store(Request $request) { $request->validate([ 'title' => 'required', 'description' => 'required', 'category

我有这个表单,我想做的是如果这个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">&times;</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>

@csrf
@方法('put')
{{{{('PAGE SEO')}
@if(会话(“状态”))
{{session('status')}
&时代;
@恩迪夫
页
@foreach($categorypages->slice(0,1)作为$key=>$value)
id){{“选定的”}@endif>{{{$value->name}
@endforeach
@错误('category\u page\u id')
{{$message}}
@恩德罗
{{{{('SEO Title')}
@如果($errors->has('title'))
{{$errors->first('title')}
@恩迪夫
{{{('Description')}
@如果($errors->has('email'))
{{$errors->first('email')}
@恩迪夫
{{{('Save')}

我能做些什么来解决我的问题?有什么方法可以正确地做到这一点吗?

有很多方法可以做到

   @if($seo_data != null) //here you can check the data is present or not 

    <form action="{{ route('seo_data.store')" method="post" >
      @csrf
      <input type="text" value="{{ isset($seo_data) ? $seo_data->title : '' }}">
      <button type="submit" class="btn btn-primary">submit </button>
  </form>

  @else //if data is present then it goes to here

  <form action="{{ route('seo_data.update',encrypt($seo_data->id))" method="post">
      @csrf
      @method('PATCH')
      <input type="text" value="{{ isset($seo_data) ? $seo_data->title : '' }}">
      <button type="submit" class="btn btn-primary">Update </button>
  </form>
 @endif
@if($seo\u data!=null)//这里您可以检查数据是否存在
@csrf
提交
@else//如果数据存在,则转到此处
@csrf
@方法('补丁')
更新
@恩迪夫

否则,如果您不想点击另一条路线,则将其签入存储功能

提供控制器,该控制器返回视图的形式并提供view@lagbox好的,我马上编辑我把我的表格放在我的问题里,请看一下@lagbox,返回此视图的控制器方法可能是您可以运行三元条件来设置值的标签!如果对象不是空的,则可以将其设置为“更新”或“保存”。
<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">&times;</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>