skip to Main Content

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 :

enter image description here

2

Answers


  1. Chosen as BEST ANSWER
    APP_NAME=Laravel
    APP_ENV=local
    APP_KEY=base64:0dl59YTqYE7f2447S1c/rCXtuqFzRwEhbQ3GucLNdHI=
    APP_DEBUG=true
    APP_URL=http://group7-project.test
    
    LOG_CHANNEL=stack
    LOG_DEPRECATIONS_CHANNEL=null
    LOG_LEVEL=debug
    
    DB_CONNECTION=mysql
    DB_HOST=http://127.0.0.1:8000/
    DB_PORT=3306
    DB_DATABASE=group7_project
    DB_USERNAME=root
    DB_PASSWORD=password
    
    BROADCAST_DRIVER=log
    CACHE_DRIVER=file
    FILESYSTEM_DISK=local
    QUEUE_CONNECTION=sync
    SESSION_DRIVER=file
    SESSION_LIFETIME=120
    
    MEMCACHED_HOST=127.0.0.1
    
    REDIS_HOST=redis
    REDIS_PASSWORD=null
    REDIS_PORT=6379
    
    MAIL_MAILER=smtp
    MAIL_HOST=mailpit
    MAIL_PORT=1025
    MAIL_USERNAME=null
    MAIL_PASSWORD=null
    MAIL_ENCRYPTION=null
    MAIL_FROM_ADDRESS="[email protected]"
    MAIL_FROM_NAME="${APP_NAME}"
    
    AWS_ACCESS_KEY_ID=
    AWS_SECRET_ACCESS_KEY=
    AWS_DEFAULT_REGION=us-east-1
    AWS_BUCKET=
    AWS_USE_PATH_STYLE_ENDPOINT=false
    
    PUSHER_APP_ID=
    PUSHER_APP_KEY=
    PUSHER_APP_SECRET=
    PUSHER_HOST=
    PUSHER_PORT=443
    PUSHER_SCHEME=https
    PUSHER_APP_CLUSTER=mt1
    
    VITE_APP_NAME="${APP_NAME}"
    VITE_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
    VITE_PUSHER_HOST="${PUSHER_HOST}"
    VITE_PUSHER_PORT="${PUSHER_PORT}"
    VITE_PUSHER_SCHEME="${PUSHER_SCHEME}"
    VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
    
    SCOUT_DRIVER=meilisearch
    MEILISEARCH_HOST=http://meilisearch:7700
    
    MEILISEARCH_NO_ANALYTICS=false
    

  2. Change this:

    $bookRating= $bookRating->save();
    

    to this:

    $bookrating->save();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search