Larasou
6 months ago

Retrieve a polymorphic relationship with Vue.js

Posted 6 months ago by Larasou

Hi people!

I just realized that I do not know how to recover a polymorphic rally by looping with Vue.js

{,…}
activities: [{id: 2, user_id: 1, subject_id: 31, subject_type: "App\Serie", type: "created_serie",…},…]
    0: {id: 2, user_id: 1, subject_id: 31, subject_type: "App\Serie", type: "created_serie",…}
    1: {id: 1, user_id: 1, subject_id: 1711, subject_type: "App\Comment", type: "created_comment",…}
         created_at: "2019-03-04 19:47:01"
         id: 1
         subject: {id: 1711, user_id: 1, commentable_id: 169, commentable_type: "App\Episode",…}
         body: "Provident corrupti voluptatum quidem placeat quidem qui. Repudiandae consequuntur harum"
         commentable_id: 169
         commentable_type: "App\Episode"
         created_at: "2019-03-04 19:47:01"
         id: 1711
         reply_id: 0
         updated_at: "2019-03-04 19:47:01"
         user: {id: 1, rank: 5, name: "Larasou", slug: "larasou", email: "[email protected]",…}
         user_id: 1
         subject_id: 1711
         subject_type: "App\Comment"
         type: "created_comment"
         updated_at: "2019-03-04 19:47:01"
         user: {id: 1, rank: 5, name: "Larasou", slug: "larasou", email: "[email protected]",…}
         user_id: 1

I can not seem to recover the relationship for commentable. With Laravel no problem but with Vue.js, I suiscoincé.

Here is my code:

Activity::where('user_id', auth()->user()->id)
            ->latest()->with(['subject'])->limit(10)->get();

I am very good at recovering belongsTo relationships with the property $with

Can you help me please ?

Please sign in or create an account to participate in this conversation.