【发布时间】:2021-06-11 12:08:11
【问题描述】:
我正在尝试允许用户编辑帖子,但是我不断收到此错误:
Property [caption] does not exist on this collection instance. (View: C:\xampp\htdocs\myprojectname\resources\views\posts\edit-post.blade.php)
我在 Post Controller 中的编辑功能是:
use App\Models\Post;
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 Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
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 App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory;
protected $guarded = [];
public function user()
{
return $this->belongsTo(User::class);
}
}
最后我在 web.php 中的路由是:
use App\Http\Controllers\PostsController;
Route::get('/posts/{post}/edit', [PostsController::class, 'edit']);
Route::patch('/posts/{post}', [PostsController::class, 'update'])->name('posts.update');
如何解决此问题,以及如何允许用户编辑帖子?
【问题讨论】:
-
Post::where('id')->first();...where('id')什么?缺少一个参数。而且,你已经通过方法参数得到$post -
use App\Http\Controllers\PostsController缺少;,应该抛出错误 -
" 使用 App\Http\Controllers\PostsController 缺少一个 ;" 抱歉,我复制并粘贴了错误,省略了 ' ; ' 当我真的已经在文件中正确输入它时。
-
" Post::where('id')->first(); ... where('id') 什么?缺少参数。而且,您已经通过方法参数获得了 $post ” 。我想了很多,尽管我试图提出错误即将出现的建议,因为我必须遍历一系列帖子来获取要编辑的帖子的 id。因此,此集合实例上不存在 Property [caption] 的错误。现在我不确定该尝试什么...
标签: php laravel model-view-controller eloquent laravel-8