Giter VIP home page Giter VIP logo

Comments (4)

arthurkirkosa avatar arthurkirkosa commented on June 13, 2024

I'd recommend you use the same key in the Repository as the name of the function in your User model.

return [ 'userfriends' => HasMany::make('userfriends', UserRepository::class)]; if you relation in User is public function userfriends(): HasMany

from laravel-restify.

wasiqaftab avatar wasiqaftab commented on June 13, 2024

@arthurkirkosa i am doing same please see the snippets

Table structure is

users have user id as uuid
contacts table have
user_id as uuid and friend_id as uuid

  1. User Model

public function userfriends(): HasMany { return $this->hasMany(Contact::class,'friend_id', 'id'); }

  1. ContactsRepository Relation

Key on contacts repository is public static string $uriKey = 'contacts';

public static function related(): array { return [ 'userfriends' => HasMany::make('userfriends', UserRepository::class), ]; }

  1. Contact Model

public function userfriends(): BelongsTo { return $this->belongsTo(User::class, 'friend_id'); }

API call if is like that

{{host}}/api/restify/contacts?related=userfriends

Result i am expecting is all friend_ids with relation but now i am getting error of un defined function maps

"message": "Call to undefined method App\\Models\\User::map()",

Please guide am i doing something wrong

from laravel-restify.

wasiqaftab avatar wasiqaftab commented on June 13, 2024

Also if i use belongsTo relation like that
return [ 'userRelationship' => BelongsTo::make('userfriends', UserRepository::class), ];

it will only return first record as relation output something like that

Screenshot 2023-02-14 at 9 45 19 pm

Please guide as it just show relation for first record

from laravel-restify.

arthurkirkosa avatar arthurkirkosa commented on June 13, 2024

Yes, as Contact has a BelongTo with User, that's the relation you have to use in your ContactRepository as well.

Because a Contact Belongs To a User, it's normal for you to have just one item of User loaded with that Contact

If you what the full list of contacts start from the User repo
GET /api/restify/users?related=userfriends

For clarity I would rename the methods in each model to the corresponding entity

// User

public function contacts(): HasMany {}
// or
public function friends(): HasMany {}

// Contact

public function user(): BelongsTo {}
// or
public function friend(): BelongsTo {}

Naming them in singular and plural would outline how many object I should receive when using the relation in the code

from laravel-restify.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.