我正在使用 Postman 来使用 apiResource,我尝试获取特定项目(“服务”),通过 API 控制器更新和删除,但不起作用并返回空值。
详情如下:
服务模式
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Services extends Model
{
use HasFactory;
protected $fillable= [
'Service','Added_By'
];
}
服务控制器
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\Services;
use App\Http\Requests\StoreServicesRequest;
use App\Http\Requests\UpdateServicesRequest;
use App\Http\Resources\ServiceResource;
class ServiceController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
dd(ServiceResource::collection((Services::query()->orderBy('id','desc')->paginate(10))));
return ServiceResource::collection((Services::query()->orderBy('id','desc')->paginate(10))) ;
}
/**
* Store a newly created resource in storage.
*/
public function store(StoreServicesRequest $request)
{
$data=$request->validated();
$service=Services::create($data);
return response(new ServiceResource($service),201);
}
/**
* Display the specified resource.
*/
public function show(Services $services)
{
dump($services);
return new ServiceResource($services);
}
/**
* Update the specified resource in storage.
*/
public function update(UpdateServicesRequest $request, Services $services)
{
$data=$request->validated();
$services->update($data);
return new ServiceResource($services);
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Services $services)
{
$services->delete();
return response("", 204);
}
}
更新服务请求
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UpdateServicesRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
"Service"=>"required",
"Added_By"=>"nullable",
];
}
}
API路线
<?php
use App\Http\Controllers\api\apiController;
use App\Http\Controllers\Api\ServiceController;
use App\Http\Controllers\Api\UserController;
use App\Http\Controllers\AuthController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| 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')->group(function(){
Route::post('/logout',[AuthController::class,'logout']);
Route::get('/user', function (Request $request) {
return $request->user();
});
Route::apiResource('/users', UserController::class);
Route::apiResource("/services",ServiceController::class);
});
Route::post('login',[AuthController::class,'login']);
Route::post('register',[AuthController::class,'register']);
<?php
namespace App\Http\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class ServiceResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
"id"=>$this->id,
"Service"=>$this->Service,
"Added_By"=>$this->Added_By,
"Date"=>$this->created_at,
];
}
}
当我尝试通过 Postman 通过 http://127.0.0.1:8000/api/services/1 获取特定服务时,但给了我:
{
"id": null,
"Service": null,
"Added_By": null,
"Date": null
}
尝试更新时结果相同。 尝试时也没有删除任何内容。
请注意,索引和存储方法工作正常。
public function show(Services $services)
{
return new ServiceResource($services);
}
public function update(UpdateServicesRequest $request, Services $services)
{
$data = $request->validated();
$services->update($data);
return new ServiceResource($services);
}
在您的路由中,确保您使用正确的参数进行绑定:
Route::apiResource("/services", ServiceController::class);