给定提供的 id ,从数据库中提取属于该 ID 的数据(LARAVEL、API、Postman)

问题描述 投票:0回答:1

我尝试按照我之前相关帖子上一篇文章中给出的建议进行操作,但没有成功。

我正在为一家在线书店处理对 API 的 HTTP 请求。我使用的数据库表包含“bookId”、“userId”和“Comment”列。我想在我的控制器类中创建一个函数,它返回给定特定 bookId 的书籍的评论列表。即返回属于所提供的 bookId 的评论列表。

下面是我应该为此函数/请求使用的迁移、路由、控制器和模型类(除非我需要根据我想要的函数类型创建一个新类)。

另外,我将如何在邮递员中正确测试这一点,这就是我如何在邮递员中提供 bookId 以查看我的功能是否有效。

抱歉,因为我对 laravel 和 postman 都比较陌生,所以任何建议将不胜感激 - 我无法在网上找到可以帮助我解决具体问题的相关文档。

控制器--正在使用的功能是“ShowComments”

<?php

namespace App\Http\Controllers;

use App\Models\CreateComment;
use App\Models\BookRating;
use Illuminate\Http\Request;


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->save();
       return ["Book Rating saved ! "];
       }

       public function CreateComment( Request $request)
       {
          // create a function that inserts data to data base ( create a book rating)
          $bookcomment = new CreateComment; 
          $bookcomment->Comment=$request->Comment;
          $bookcomment->userId=$request->userId;
          $bookcomment->bookId=$request->bookId;
          $bookcomment->save();
          return ["Comment saved ! "];
          }



    /**
     * Display the specified resource.
     */
    public function ShowComments(string $bookId)
    {
    
       // This will be the function used to return list of comments given a 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'
    ];
}

迁移

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up()
    {
        Schema::create('create_comments', function (Blueprint $table) {
            $table->id();
            $table->string('Comment');
            $table->string('userId');
            $table->string('bookId');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('create_comments');
    }
};

路线

<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\BookRatingController;
use App\Http\Models\BookRating;
use App\Http\Models\CreateComment;

/*
|--------------------------------------------------------------------------
| 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', [CommentController::class, 'ShowComments']);

php laravel database api postman
1个回答
0
投票

要获取特定书籍的评论,您需要修改

ShowComments
中的
BookRatingController
函数以接受
bookId
参数,然后使用它来查询
CreateComment
模型。

public function ShowComments($bookId)
{
    $comments = CreateComment::where('bookId', $bookId)->get();

    return response()->json($comments);
}

此函数将返回一个 JSON 响应,其中包含

bookId
的所有评论。接下来,您需要修改路线以接受
bookId
参数:

Route::get('ShowComments/{bookId}', [BookRatingController::class, 'ShowComments']);

您可以在 Postman 中测试这一点,方法是向

http://your-app-url/ShowComments/{bookId}
发出 GET 请求,将
{bookId}
替换为您要获取评论的图书的实际 ID。

© www.soinside.com 2019 - 2024. All rights reserved.