亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何只編輯一篇文章而不是迭代循環所有文章?

如何只編輯一篇文章而不是迭代循環所有文章?

PHP
撒科打諢 2023-07-21 18:19:09
我foreach($user->posts as $post)在輸入字段上使用,以便我能夠使用它,value="{{ old('about') ?? $post->about }}"因為如果沒有迭代,我在使用該值時會收到錯誤(試圖獲取對象的屬性)。但是,在嘗試編輯時,我不知道我的 edit.blade 看起來像這樣,我只需要一個輸入字段來表示我正在嘗試編輯的一篇特定帖子編輯.blade.php@extends('layouts.app')@section('content')<div class="container"><form action="/p/update/{{ $user->id}}" enctype="multipart/form-data" method="POST">@csrf@method ('PATCH')<div class="col-8 offset-2"><div class="form-group row">@foreach($user->posts as $post)    <label for="about" class="col-md-4 col-form-label text-md-right">{{ __(' post about') }}</label>    <div class="col-md-6">        <input id="about" type="text" class="form-control @error('about') is-invalid @enderror" name="about"  value="{{ old('about') ?? $post->about }}" required autocomplete="about" autofocus>    @error('about')        <span class="invalid-feedback" role="alert">            <strong>{{ $message }}</strong>        </span>    @enderror    </div>@endforeach    <label for="image" class="col-md-4 col-form-label text-md-right">{{ __(' post image') }}</label>    <input type="file", class="form-control-file" id ="image" name="image"  >    @error('image')            <div class="invalid-feedback" role="alert">                <strong>{{ $message }}</strong> </div>    @enderror      <div class="btn btn-primary">      <button> save </button>      </div>    </div> </div></form></div>@endsection路線Route::get('/post/edit/{user}', 'PostController@edit')->name('post.edit');Route::patch('/p/update/{user}', 'PostController@update')->name('post.update');后置控制器 public function edit(User $user){     return view('posts.edit', compact('user'));} public function update(User $user){$data = request()->validate([    'about' => 'required',    'image' => '',  ]);
查看完整描述

1 回答

?
寶慕林4294392

TA貢獻2021條經驗 獲得超8個贊

為了傳遞一篇文章進行編輯,您必須像這樣傳遞這篇文章:


路線


Route::get('/post/edit/{post}', 'PostController@edit')->name('post.edit');

控制器


 public function edit(Post $post)

{

   

  return view('posts.edit', compact('post'));

}

刀片銼刀


<label for="about" class="col-md-4 col-form-label text-md-right">{{ __(' post about') }}</label>


<div class="col-md-6">

<input id="about" type="text" class="form-control @error('about') is-invalid @enderror" name="about"  value="{{ old('about') ?? $post->about }}" required autocomplete="about" autofocus>


    @error('about')

        <span class="invalid-feedback" role="alert">

            <strong>{{ $message }}</strong>

        </span>

    @enderror

</div>


查看完整回答
反對 回復 2023-07-21
  • 1 回答
  • 0 關注
  • 119 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號