Laravel API - 通过 o2m 关系显示单个文件

Laravel API - Showing individual file through o2m relationship

所以我有两个模型用户和文件,它们以一对多的关系连接。

我已经对 API 路由和控制器进行了排序,以索引所有用户,显示特定用户并索引该特定用户上传的所有文件。我不知道如何编写允许此路由 127.0.0.1:8001/api/2/files/1 显示第二个用户上传的第一个文件的逻辑。 So/2(seconduser)/files(显示全部)/1(仅显示 1 个文件)

这是我的 API 代码:

Route::group(["prefix" => "/"], function () {
    Route::get("", [Users::class, "index"]); //show all users

    Route::group(["prefix" => "{user}"], function () {
        Route::get("", [Users::class, "show"]); //show specific user

        Route::group(["prefix" => "/files"], function () {
            Route::get("", [Files::class, "index"]); //show all files

            Route::group(["prefix" => "{file}"], function () {
                Route::get("", [Files::class, "show"]); //trying to show specific file
            });
        });
    });
});

文件控制器

<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;
use App\Models\File;

class Files extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(User $user)
    {
        return $user->files;    
    }


    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show(User $user, File $file)
    {
        
    }
}

用户控制器

<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;

class Users extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return User::all();
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show(User $user)
    {
        return $user;
    }
}

这是典型的路由声明的样子。请注意,用户 ID 与文件请求无关,因此文件端点与用户端点分开。

Route::get("/users",              [Users::class, "index"]);
Route::get("/users/{user}",       [Users::class, "show"]);
Route::get("/users/{user}/files", [Files::class, "index"]);
Route::get("/files/{file}",       [Files::class, "show"]);

然后在您的控制器方法中,您只需返回一个列表或单个项目,这主要与您的原始代码相同。请注意,如果您要返回 API 数据,则应明确 return JSON.

<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;
use App\Models\File;

class Files extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(User $user)
    {
        return response()->json($user->files);    
    }


    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function show(File $file)
    {
        return response()->json($file);
        // or perhaps something like this?
        return response()
            ->download($file->path, $file->name, ["Content-Type" => $file->type]);
    }
}
<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;

class Users extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function index()
    {
        return response()->json(User::all());
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function show(User $user)
    {
        return response()->json($user);
    }
}