skip to Main Content

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


  1. 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.

    public function showComments() {
        $allComments = CreateComment::all();
        return $allCOmments;
    }
    
    Login or Signup to reply.
  2. If you want to get all the comments

     public function showComments() {
        $comments = CreateComment::all();
        return view('your-view',compact('comments'));
    } 
    

    If you want to get the specific user’s comments then

       public function showComments($id) {
            $comments = CreateComment::where('userId',$id)->get();
            return view('your-view',compact('comments'));
        }
    

    For this route might need some changings like

    Route::get('showComments/{id}',[BookRatingController::class,'showComments']);
    
    Login or Signup to reply.
  3. For your future, delete the CreateComment model and call it Comment this cause Create is a action and not follow the standard naming convention.

    Anyway to retrive all the Comments you shuld use the all() method

    CreateComment::all();
    

    If you want to get only one CreateComment::find($id); or better CreateComment::findOrFail($id);

    From the moment that CreateComment seems to be relatated to a Reservation, 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search