tomasosho
1 month ago

I have been getting this error

Posted 1 month ago by tomasosho

Facade\Ignition\Exceptions\ViewException SQLSTATE[HY000]: General error: 1 no such table: video_comments (SQL: select * from "video_comments" where "video_comments"."videopost_id" = 2 and "video_comments"."videopost_id" is not null and "parent_id" is null) (View: C:\xampp\htdocs\laravelapps\Insta\resources\views\posts\showvideo.blade.php)

My View

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-8">
            <video src="/storage/public/video/{{$videopost->video}}" alt="" class="w-100">
            </video>
        </div>
        <div class="col-4">
            <div>
                <div class="d-flex align-items-center">
                    <div class="pr-3">
                        <img src="/storage/avatars/{{$videopost->user->avatar}}" class="rounded-circle w-100" style="max-width: 35px;">
                    </div>
                    <div>
                        <div class="font-weight-bold"> 
                            <a href="/profile/{{$videopost->user->id}}"> 
                                <span class="text-dark">
                                    {{$videopost->user->username}}
                                </span>
                            </a>
                        </div>
                    </div>
                </div>
                <hr>
                <p>
                    <span class="font-weight-bold">
                        <a href="/profile/{{$videopost->user->id}}">
                            <span class="text-dark">
                                {{$videopost->user->username}}
                            </span>
                        </a>
                    </span> 
                    {{$videopost->caption}}
                </p>
                @can ('update', $videopost->user->profile)
                @if(!Auth::guest())
                    @if(Auth::user()->id == $videopost->user_id)
                        {!!Form::open(['action' => ['[email protected]', $videopost->id], 'method' => 'POST', 'class' => 'pull-right'])!!}
                            {{Form::hidden('_method', 'DELETE')}}
                            {{Form::submit('Delete', ['class' => 'btn btn-danger'])}}
                        {!!Form::close()!!}
                    @endif
                @endif
                @endcan

                <hr />
                    <h4>Display Comments</h4>
  
                    @include('posts.videocommentsDisplay', ['comments' => $videopost->comments, 'videopost_id' => $videopost->id])
   
                    <hr />
                    <h4>Add comment</h4>
                    <form method="post" action="/videocomment">
                        @csrf
                        <div class="form-group">
                            <textarea class="form-control" name="body"></textarea>
                            <input type="hidden" name="videopost_id" value="{{ $videopost->id }}" />
                        </div>
                        <div class="form-group">
                            <input type="submit" class="btn btn-success" value="Add Comment" />
                        </div>
                    </form>

            </div>
        </div>
    </div>
</div>
@endsection

My view

@foreach($comments as $comment)
    <div class="display-comment" @if($comment->parent_id != null) style="margin-left:40px;" @endif>
        <strong>{{ $comment->user->name }}</strong>
        @if(!Auth::guest())
                    @if(Auth::user()->id == $comment->user_id)
                        {!!Form::open(['action' => ['[email protected]', $comment->id], 'method' => 'POST', 'class' => 'pull-right'])!!}
                            {{Form::hidden('_method', 'DELETE')}}
                            {{Form::submit('Delete', ['class' => 'btn btn-danger'])}}
                        {!!Form::close()!!}
                    @endif
                @endif
        <p>{{ $comment->body }}</p>
        <a href="" id="reply"></a>
        <form method="post" action="/videocomment">
            @csrf
            <div class="form-group">
                <input type="text" name="body" class="form-control" />
                <input type="hidden" name="videopost_id" value="{{ $videopost_id }}" />
                <input type="hidden" name="parent_id" value="{{ $comment->id }}" />
            </div>
            <div class="form-group">
                <input type="submit" class="btn btn-warning" value="Reply" />
            </div>
        </form>
        @include('posts.videocommentsDisplay', ['comments' => $comment->replies])
    </div>
@endforeach

Controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\VideoComment;

class VideoCommentController extends Controller
{
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'body'=>'required',
        ]);
   
        $input = $request->all();
        $input['user_id'] = auth()->user()->id;
    
        VideoComment::create($input);
   
        return back();
    }

     /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $comment = VideoComment::find($id);
        
        //Check if post exists before deleting
        if (!isset($comment)){
            return redirect('/')->with('error', 'No Post Found');
        }
        
        $comment->delete();
        return back()->with('success', 'comment Removed');
    }
}

Migration

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateVideopostsCommentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('video_comments', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->integer('videopost_id')->unsigned();
            $table->integer('parent_id')->unsigned()->nullable();
            $table->text('body');
            $table->timestamps();
            $table->softDeletes();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('videoposts_comments');
    }
}

Please sign in or create an account to participate in this conversation.