EN VI

Php - My post request keeps trying to be passed to the wrong migration file (laravel 10)?

2024-03-12 11:00:05
How to Php - My post request keeps trying to be passed to the wrong migration file (laravel 10)

I have 2 migration files, one for a book rating when a user leaves a book rating -- this post request will be passed to the book_ratings table. I have another migration file where its for when users leave comments on a book where it will be placed in the book_comment table.

Both post request do the same where it stores the rating/comment , the userId & bookId in a post request when passed to the API. my book rating request works good but my book comment request getd an 500 error & based on the postman error message it seems that the request is trying to passed to my book_rating table not my book_comment table -- but i am unsure why . Any assistance would be appreciated.

Model files :

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class BookRating extends Model
{

    use HasFactory;

    protected $fillable =
    [
        'Rating',
        'userId',
        'bookId'
    ];
}
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class CreateComment extends Model
{

    use HasFactory;

    protected $fillable =
    [
        'Comment',
        'userId',
        'bookId'
    ];
}

Controller file

 /**
     * Store a newly created resource in storage.
     */
    public function CreateRating( Request $request)
    {
       // create a function that inserts data to data base ( create a book rating)
       $bookrating = new BookRating; 
       $bookrating->Rating=$request->Rating;
       $bookrating->userId=$request->userId;
       $bookrating->bookId=$request->bookId;
       $bookrating->save();
       return ["Book Rating saved ! "];
       }

       public function CreateComment( Request $request)
       {
          // create a function that inserts data to data base ( create a book comment)
          $bookcomment = new BookRating; 
          $bookcomment->Comment=$request->Comment;
          $bookcomment->userId=$request->userId;
          $bookcomment->bookId=$request->bookId;
          $bookcomment->save();
          return ["Comment saved ! "];
          }

migration files

 public function up()
    {
        //
        Schema::create('book_ratings', function (Blueprint $table) {
            $table->id();
            $table->string('Rating');
            $table->string('userId');
            $table->string('bookId');
            $table->timestamps();
        });
    }
  public function up()
    {
        Schema::create('create_comments', function (Blueprint $table) {
            $table->id();
            $table->string('Comment');
            $table->string('userId');
            $table->string('bookId');
            $table->timestamps();
        });
    }

Route api file

Route::post('CreateRating',[BookRatingController::class, 'CreateRating']);//adding a bookrating to database API -- POST METHOD-- 
Route::post('CreateComment',[BookRatingController::class, 'CreateComment']);

The error I am getting on postman for my CreateComment request :<!-- Illuminate\Database\QueryException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Comment' in 'field list' (Connection: mysql, SQL: insert into **book_ratings** (Comment, userId, bookId, updated_at, created_at) values (This book is okay !, crism1515, fn006, 2024-03-12 01:52:20, 2024-03-12 01:52:20)) in file /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php on line 829

Solution:

The problem is you have the same code in the create comment function.

public function CreateComment( Request $request)
       {
          // create a function that inserts data to data base ( create a book comment)
          $bookcomment = new BookRating; 
          $bookcomment->Comment=$request->Comment;
          $bookcomment->userId=$request->userId;
          $bookcomment->bookId=$request->bookId;
          $bookcomment->save();
          return ["Comment saved ! "];
          }

It should be:

public function CreateComment( Request $request)
       {
          // create a function that inserts data to data base ( create a book comment)
          $comment = new CreateComment; 
          $comment->Comment=$request->Comment;
          $comment->userId=$request->userId;
          $comment->bookId=$request->bookId;
          $comment->save();
          return ["Comment saved ! "];
          }

I would also suggest you look into the documentation for migrations. You can utilize foreign key relationships, such as foreignId('userId')->constrained('users')

Typically for models, you want to have it a singular name such as Comment, or Book. You also want to follow the controller methods Index, Create, Store, Edit, Update and Destroy. In this instance, I would create two separate controllers and each would have these methods as the Store method.

I would also add a HasMany relationship on the book model for comments (or a MorphsMany if you have other models that you can comment on). Then you would be able to post a comment on a book using route model binding for example.com/book/1/comment and have your store method accept the public function store(Request $request, Book $book) and then you would be able to validate the comment, and use $book->comments()->create(['userId' => $request->userId, 'Comment' => $request->Comment) method to automatically.

You should also validate your input, so someone cannot comment as someone else. I would look into using auth()->id() to get the currently authenticated user's ID. The laravel docs are extensive, and I would recommend reading them fully.

Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login