分享
三行代码  ›  专栏  ›  技术社区  ›  Sanae

SQLSTATE[42S22]:未找到列:1054“where子句”中的未知列“id”(SQL:select count(*)作为来自“posts”where“id”的聚合=

  •  0
  • Sanae  · 技术社区  · 2 天前

    我收到下面的错误信息。 这是我第一次用拉维。

    以下是我的电脑环境: 我的PHP版本:7.4.2 拉威尔版本:7.3.0 MySQL版本:5.7.26

    提前谢谢你的建议!

    SQLSTATE[42S22]: Column not found: 1054 Unknown column ' id' in 'where clause' (SQL: select count(*) as aggregate from `posts` where ` id` = 52)
    

    CommentsController.php

    <?php
    
    namespace App\Http\Controllers;
    
    use Illuminate\Http\Request;
    use App\Post;
    
    class CommentsController extends Controller
    {
        public function store(Request $request)
        {
            $params = $request->validate([
                'post_id' => 'required|exists:posts, id',
                'body' => 'required|max:2000',
            ]);
    
            $post = Post::findOrFail($params['post_id']);
            $post->comments()->create($params);
    
            return redirect()->route('posts.show', ['post' => $post]);
        }
    }
    

    <?php
    
    use Illuminate\Support\Facades\Route;
    
    
    Route::get('/', 'PostsController@index')->name('top');
    Route::resource('posts', 'PostsController', ['only' => ['create', 'store']]);
    Route::resource('posts', 'PostsController', ['only' => ['create', 'store', 'show']]);
    Route::resource('comments', 'CommentsController', ['only' => ['store']]);
    
    

    show.blade.php显示.blade.php

    @extends('layout')
    
    @section('content')
        <div class="container mt-4">
            <div class="border p-4">
                <h1 class="h5 mb-4">
                    {{ $post->title }}
                </h1>
    
                <p class="mb-5">
                    {!! nl2br(e($post->body)) !!}
                </p>
    
                <section>
                    <h2 class="h5 mb-4">
                        Comment
                    </h2>
    
                    <form class="mb-4" method="POST" action="{{ route('comments.store') }}">
                    @csrf
    
                        <input name="post_id" type="hidden" value="{{ $post->id }}">
    
                        <div class="form-group">
                            <label for="body">
                                Context
                            </label>
    
                            <textarea
                                id="body"
                                name="body"
                                class="form-control {{ $errors->has('body') ? 'is-invalid' : '' }}"
                                rows="4"
                            >{{ old('body') }}</textarea>
                            @if ($errors->has('body'))
                                <div class="invalid-feedback">
                                    {{ $errors->first('body') }}
                                </div>
                            @endif
                        </div>
    
                        <div class="mt-4">
                            <button type="submit" class="btn btn-primary">
                                Add comments
                            </button>
                        </div>
                    </form>
    
                        @forelse($post->comments as $comment)
                        <div class="border-top p-4">
                            <time class="text-secondary">
                                {{ $comment->created_at->format('Y.m.d H:i') }}
                            </time>
                            <p class="mt-2">
                                {!! nl2br(e($comment->body)) !!}
                            </p>
                        </div>
                        @empty
                            <p>No comments yet.</p>
                        @endforelse
                </section>
            </div>
        </div>
    @endsection
    

    This is posts table from my MySQL

    1 回复  |  直到 2 天前
        1
  •  1
  •   Sehdev    2 天前

    你得到这个错误是因为前面有一个空格 id 在你的确认中。

    要解决此错误,您必须

    改变

    'post_id' => 'required|exists:posts, id', // remove space from here
    

    'post_id' => 'required|exists:posts,id',