我试图让用户编辑帖子,但是我一直得到这个错误:
Property [caption] does not exist on this collection instance. (View: C:xampphtdocsmyprojectnameresourcesviewspostsedit-post.blade.php)
我在Post Controller中的编辑功能是:
use AppModelsPost;
public function edit(Post $post)
{
return view('posts.edit-post', ['post' => $post]);
}
我在Post Controller中的更新功能是:
public function update(Post $post, Request $request)
{
$data = request()->validate([
'caption' => 'nullable',
'url' => 'nullable',
'image' => 'nullable',
]);
$updateData = [
'caption' => $data['caption'],
'url' => $data['url'],
];
if (request('image')) {
$imagePath = request('image')->store('uploads', 'public');
$updateData['image'] = $imagePath;
}
auth()->user()->posts()->id()->update($updateData);
return redirect('/users/' . auth()->user()->id);
}
我的edit-post.blade.php文件代码是:
@section('content')
<body class="create_body">
<div class="create_container_div">
<form action="{{('/users/' . auth()->user()->id)}}" enctype="multipart/form-data" method="post">
@csrf
@method('PATCH')
<div class="form-group">
<label for="caption" class="create_caption_label">Post Caption</label>
<div class="create_caption_div">
<input id="caption"
type="text"
class="form-control @error('caption') is-invalid @enderror"
name="caption"
value="{{ old('caption') ?? auth()->user()->posts->caption }}"
autocomplete="caption" autofocus>
@error('caption')
<div class="invalid-feedback-div">
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
</div>
@enderror
</div>
</div>
<div class="form-group">
<label for="url" class="edit_title_label">URL</label>
<div class="edit_url_div">
<input id="url"
type="text"
class="form-control @error('url') is-invalid @enderror"
name="url"
value="{{ old('url') ?? auth()->user()->posts->url }}"
autocomplete="url" autofocus>
@error('url')
<div class="invalid-feedback-div">
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
</div>
@enderror
</div>
</div>
<div class="create_post_image_div">
<label for="image" class="create_image_label">Post Image</label>
<input type="file" class="form-control-file" id="image" name="image">
@error('image')
<div class="invalid-feedback-div">
<strong>{{ $message }}</strong>
</div>
@enderror
<div class="create_post_btn_div">
<button class="create_post_btn">Save Changes</button>
</div>
</div>
</form>
</div>
</body>
@endsection
我的create_posts_table迁移文件是:
<?php
use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->string('caption');
$table->string('url');
$table->string('image');
$table->timestamps();
$table->index('user_id');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
我的Post.php模型文件是:
<?php
namespace AppModels;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
class Post extends Model
{
use HasFactory;
protected $guarded = [];
public function user()
{
return $this->belongsTo(User::class);
}
}
最后我在web.php中的路由是:
use AppHttpControllersPostsController;
Route::get('/posts/{post}/edit', [PostsController::class, 'edit']);
Route::patch('/posts/{post}', [PostsController::class, 'update'])->name('posts.update');
我如何解决这个问题,而且,我如何允许用户编辑帖子?
这一行是不正确的,我不知道你是否来自其他语言,这对我来说根本没有意义。
auth()->user()->posts()->id()->update($updateData);
你有Post
已经在你的上下文中,由于它是模型绑定在你的控制器方法public function update(Post $post, Request $request)
,你可以用它来更新它。fill()
将您的$updateData
应用于模型,save()
将其写入数据库。
$post->fill($updateData);
$post->save();
如注释所述,编辑函数缺少id,您已经将模型绑定到该方法,那么只需使用它。
public function edit(Post $post)
{
return view('posts.edit-post', ['post' => $post]);
}