我foreach($user->posts as $post)
在我的输入字段上使用,以便我能够使用它,value="{{ old('about') ?? $post->about }}"
因为没有迭代我在使用该值时出现错误(试图获取对象的属性)。但是,当我尝试编辑时,我不知道我的 edit.blade 看起来像这样我只需要一个输入字段,我只需要一个特定的帖子,我正在尝试编辑
@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' => '',
]);
if(request('image')){
$imagePath = request('image')->store('uploads','public');
$image = Image::make(public_path("storage/{$imagePath}"))->fit(500,500);
$image->save();
$imageArray = ['image' => $imagePath];
}
auth()->user()->posts()->update(array_merge( $data, $imageArray ?? [] ) );
return redirect("/user/{$user->id} ");
}
为了通过一个帖子进行编辑,您必须通过以下帖子:
路线
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>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句