I am trying to test a controller function that would store specific data to the mySQL database , that is given specific input this input will be stored to the data base using an API & will be done through Postman .
for some reason I am getting an internal 500 server error when testing this request oute in Postman. I am fairly new to using laravel & MySQL data base . If I can get any advise on what i have to fix as I am having issues on this request being done successfully. Below I will show my controller, migration , route & model files . I am on a MAC using tableplus & docker for the MySql database.
Controller :
use AppModelsBookRating;
use IlluminateHttpRequest;
class BookRatingController extends Controller
{
/**
* 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= $bookRating->save();
return ["Data has been saved"];
}
Model:
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
class BookRating extends Model
{
use HasFactory;
protected $fillable =
[
'Rating',
'userId',
'bookId'
];
}
Migration :
use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up()
{
//
Schema::create('bookRating', function (Blueprint $table) {
$table->id();
$table->string('Rating');
$table->string('userId');
$table->string('bookId');
$table->timestamps();
});
}
Route :
use IlluminateHttpRequest;
use IlluminateSupportFacadesRoute;
use AppHttpControllersBookRatingController;
use AppHttpModelsBookRating;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "api" middleware group. Make something great!
|
*/
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});
Route::post("CreateRating",[BookRatingController::class, 'CreateRating']);//adding a bookrating to database API -- POST METHOD--
Here is the request I am submitting through postman (where i input the values) but am getting a error 500 :
2
Answers
Change this:
to this: