php Laravel:如何编写一个路由来获得hasMany关系?

vohkndzv  于 12个月前  发布在  PHP
关注(0)|答案(1)|浏览(125)

在Laravel中,我有这些模型来描述人们参与活动的一天事件。每个事件都有许多活动,但一个活动只属于一个事件。我无法编写一个路由来获取给定事件的活动。

模型事件

namespace App\Models;
use App\Models\Activity;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Event extends Model
{
    use HasFactory;
    protected $table = 'events';
    protected $fillable = [
        'label',
        'date',
        'activities',
        'cassetteOperations',
        'bankOperations'
    ];

    /**
     * Get all of the event's activities.
    */
    public function activities(): HasMany
    {
        return $this->hasMany(Activity::class);
    }
}

字符串

模型活动

namespace App\Models;
use App\Models\Participation;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;

class Activity extends Model
{
    use HasFactory;
    protected $fillable = [
        'label',
        'event_id',
        'memberPrice',
        'otherPrice',
        'memberEntryFee',
        'otherEntryFee',
        'type',
        'participations'
    ];

    /**
     * Get the participations for the activity
     */
    public function participations(): HasMany
    {
        return $this->hasMany(Participation::class);
    }

    /**
     * Get the event associated with the activity
     */
    public function event(): HasOne
    {
        return $this->hasOne(Event::class);
    }

    /**
     * Get the persons that participates to this activity
     *
     */
    public function participants(): HasManyThrough
    {
        return $this->hasManyThrough(Person::class, Participation::class);
    }
}


App/Http/API/ActivityController中,

namespace App\Http\Controllers\Api;

use App\Models\Event;
use App\Models\Activity;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Resources\ActivityResource;
use App\Http\Requests\StoreActivityRequest;

class ActivityController extends Controller
{
    public function index($event_id)
    {
        return ActivityResource::collection(Activity::where('event_id', $event_id)->orderBy('id')->get());

    }

    public function store(StoreActivityRequest $request)
    {
        Activity::create($request->validated());
        //return only activities bound to the event
        return ActivityResource::collection(Activity::where('event_id', $request->event_id)->orderBy('id')->get());
    }

}


routes/API中,

Route::middleware(['auth:sanctum'])->get('/user', function (Request $request) {
    return $request->user();
});
Route::apiResource('person',PersonController::class)->middleware(['auth'])->middleware('App\Http\Middleware\Allowed');
Route::apiResource('event',EventController::class)->middleware(['auth'])->middleware('App\Http\Middleware\Allowed');

Route::get('activity/{event_id}',[ActivityController::class,'index'])->middleware(['auth'])->middleware('');
Route::apiResource('activity',ActivityController::class)->middleware(['auth'])->middleware('App\Http\Middleware\Allowed');


在我的vue项目中,我的目标如下

async apiFetchEventActivities(event_id) {
    try {
        await axios.get('sanctum/csrf-cookie')
        this.activityErrors = []
        const { data } = await axios.get('/api/activity/' + event_id)
        this.activities = data.data

    } catch ({ response }) {
        this.activityErrors = [response.data.message, response.status]
        return ('failure')
    }
}


我得到一个错误状态500,并带有“message”:“Target class [] does not exist”。
我的路由(routes/API中倒数第一个)是否不正确,为什么?我应该如何编写它?

rkue9o1l

rkue9o1l1#

routes/api.php文件中,您有以下路由:

Route::get('activity/{event_id}', [ActivityController::class, 'index'])
    ->middleware(['auth'])
    ->middleware('');

字符串
问题似乎出在第二个中间件方法调用上,它是空的。如果你没有任何特定的中间件要应用,你可以删除该行。同时确保你已经导入了控制器
你在这里了解更多关于Laravel中间件https://laravel.com/docs/10.x/middleware

相关问题