I want to retrieve list of comments from my CreateComments
model. I only want to retrieve the list of Comments
from my data base using the GET request to the API.
Below are my controller, model & route classes (FYI there is already a route for the get request i want for testing purposes).
Controller class:
namespace AppHttpControllers;
use AppModelsCreateComment;
use AppModelsBookRating;
use IlluminateHttpRequest;
class BookRatingController extends Controller
{
/**
* Display the specified resource.
*/
public function showComments()
{
return "This is the showComments method";
} // return list of comments
/**
* Update the specified resource in storage.
*/
public function update(Request $request, BookRating $bookRating)
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy(BookRating $bookRating)
{
//
}
}
Model class:
namespace AppModels;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
class CreateComment extends Model
{
use HasFactory;
protected $fillable =
[
'Comment',
'userId',
'bookId'
];
}
Route Class:
use IlluminateHttpRequest;
use IlluminateSupportFacadesRoute;
use AppHttpControllersBookRatingController;
use AppHttpModelsBookRating;
use AppHttpModelsCreateComment;
/*
|--------------------------------------------------------------------------
| 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--
Route::post('CreateComment',[BookRatingController::class, 'CreateComment']);
Route::get('showComments',[BookRatingController::class,'showComments']);
3
Answers
Since
CreateComment
model has already been declared, you can use the below function in able to get all the comments.This is using the eloquent way. Check here for detailed explanation.
If you want to get all the comments
If you want to get the specific user’s comments then
For this route might need some changings like
For your future, delete the
CreateComment
model and call itComment
this causeCreate
is a action and not follow the standard naming convention.Anyway to retrive all the Comments you shuld use the
all()
methodIf you want to get only one
CreateComment::find($id);
or betterCreateComment::findOrFail($id);
From the moment that
CreateComment
seems to be relatated to aReservation
, you will need tyo prepare db and software to use relationship, couse one Reservation will have many Comments.You can find many informations here: https://laravel.com/docs/11.x/eloquent-relationships
If you want to get an idea on how laravel work with the naming under the hood check this article: laravel naming convention