Я хочу получить список комментариев из моей модели CreateComments. Я хочу получить только список комментариев из своей базы данных, используя запрос GET к API.
Ниже приведены классы моего контроллера, модели и маршрута (к вашему сведению, они уже есть). маршрут для запроса на получение, который мне нужен для целей тестирования).
Класс контроллера:
namespace App\Http\Controllers;
use App\Models\CreateComment;
use App\Models\BookRating;
use Illuminate\Http\Request;
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)
{
//
}
}
Класс модели:
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\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',[BookRatingController::class,'showComments']);
Подробнее здесь: https://stackoverflow.com/questions/782 ... laravel-10
Как получить конкретное значение из модели с помощью запроса GET (Laravel 10) ⇐ Php
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение