使用 Laravel API 和 Postman 提供的 id 从数据库中提取数据

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

我尝试遵循上一篇文章的建议,但没有成功。我正在为在线书店开发 HTTP API 请求。我正在使用的数据库表有“bookId”、“userId”和“Comment”列。我的目标是在我的控制器类中创建一个函数,根据给定的“bookId”返回特定书籍的评论列表。

除非需要新的,否则应将下面提供的我的迁移、路由、控制器和模型类用于此功能/请求。我也不确定如何在 Postman 中正确测试这一点,特别是如何提供“bookId”来验证我的函数是否有效。

我对 Laravel 和 Postman 比较陌生,所以任何建议将不胜感激。我一直无法找到相关的在线文档来帮助我解决这个问题。

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

namespace App\Http\Controllers;

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


class BookRatingController extends Controller
{
    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 ! "];
          }

    public function ShowComments(string $bookId)
    {
       // This will be the function used to return list of comments given a bookId. 

    }   
}

型号

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'
    ];
}

迁移

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

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

    public function down(): void
    {
        Schema::dropIfExists('create_comments');
    }
};

路线

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.