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