我正在使用邮递员使用 apiResouce,我尝试获取特定项目(“服务”),通过 api 控制器更新和删除,但不起作用并返回空值,
详情请看这里:
**Services model**
<?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'
];
}
**ServiceController**
<?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);
}
}
**UpdateServiceRequest**
<?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 route**
<?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,
];
}
}
当我尝试通过邮递员通过 http://127.0.0.1:8000/api/services/1 获取特定服务时,但给了我: { “id”:空, “服务”:空, “添加者”:空, “日期”:空 }
尝试更新时结果相同 尝试时也没有删除任何内容
**注意,索引和存储方法工作正常**
谢谢您的帮助
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);