简体   繁体   English

Laravel return 有很多关系

[英]Laravel return Has many relationship

The relationship is User who can have many Events they are associated to.关系是用户,他们可以拥有许多与之关联的事件。

I want to be able to make an API call to get all the events associated by the user.我希望能够进行 API 调用以获取用户关联的所有事件。 I have changed my primary key from id to uuid.我已将主键从 id 更改为 uuid。 I have made the foreign key association to the Events table as well.我也将外键关联到 Events 表。 The primary key in the events table is also a column called UUID.事件表中的主键也是一个名为 UUID 的列。

I am getting error我收到错误

Return value of App\Http\Controllers\UsersController::getAllEvents() must be an instance of App\Http\Resources\UserResource, instance of Illuminate\Database\Eloquent\Relations\HasMany returned

My routes table:我的路线表:

Route::apiResource('/users', 'UsersController');
Route::apiResource('/events', 'EventsController');
Route::get('/users/allevents/{user}', 'UsersController@getAllEvents');

So the URL im hitting is:所以我点击的网址是:

http://127.0.0.1:8000/api/users/allevents/0a0jqZ7qzuhemKnzB3wOPUc2Ugp2

0a0jqZ7qzuhemKnzB3wOPUc2Ugp2 is the UUID of a user. 0a0jqZ7qzuhemKnzB3wOPUc2Ugp2是用户的 UUID。 I want to get all the events associated to that user.我想获取与该用户关联的所有事件。

My User model:我的用户模型:

class User extends Model
{
    use Notifiable;
    use SoftDeletes;

    protected $dates = ['deleted_at'];

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'uuid', 'email', 'firstName', 'lastName', 'level', 'venmo'
    ];

    public function getRouteKeyName()
    {
        return 'uuid';
    }

    public function events() {
        return $this->hasMany(Event::class);
    }


}

My Event Model:我的事件模型:

class Event extends Model
{
    use SoftDeletes;
    protected $table = 'events';
    protected $dates = ['deleted_at'];
//    public function getRouteKeyName()
//    {
//        return 'uuid';
//    }

    protected $fillable = [
        'id', 'availableSpots', 'uuid', 'chosenDate', 'date', 'epochTime', 'level', 'price', 'time', 'created_at', 'updated_at', 'user_uuid'
    ];
    public $incrementing = false;
    protected $primaryKey = 'uuid';
    protected $keyType = 'string';


    public function user(){
        return $this->belongsTo(User::class, 'user_uuid', 'uuid');
    }
}

My UsersController:我的用户控制器:

class UsersController extends Controller
{

    public function show(User $user): UserResource
    {

        return new UserResource($user);
    }



    /**
     * @param Request $request
     * @param User $user
     * @return UserResource
     */
    public function update(Request $request, User $user): UserResource
    {
        $user->update($request->all());


        return new UserResource($user);

    }


    /**
     * @param User $user
     * @return UserResource
     * @throws \Exception
     */
    public function destroy(User $user): UserResource
    {
        $user->delete();

        return new UserResource($user);
    }

    public function getAllEvents(User $user): UserResource {
        return $user->events();
    }
}

You are using wrong type of the method return it doesn't return UserResource it return events array so by removing the wrong type it will work您使用了错误类型的方法返回它不返回 UserResource 它返回事件数组因此通过删除错误的类型它将起作用

   public function getAllEvents(User $user) {
        return $user->events;
    }
public function getAllEvents(User $user)
{
    return $user->events;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM