Comments (4)
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.
@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
- User Model
public function userfriends(): HasMany { return $this->hasMany(Contact::class,'friend_id', 'id'); }
- ContactsRepository Relation
Key on contacts repository is public static string $uriKey = 'contacts';
public static function related(): array { return [ 'userfriends' => HasMany::make('userfriends', UserRepository::class), ]; }
- 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.
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
Please guide as it just show relation for first record
from laravel-restify.
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)
- How to get the validate errors in repository and response errors. HOT 1
- Store on a repository ignores the $fillable on the Model HOT 1
- Unauthorized to view repository products. Check \"allowRestify\" policy. HOT 1
- GET /users is always public - it ignores the auth:sanctum middleware HOT 3
- "Class \"Binaryk\\LaravelRestify\\Tests\\Fixtures\\User\\User\" not found HOT 3
- Relationships values should always be an array HOT 2
- Docs for media is missing a function call to store the media HOT 1
- Issue action with not an actual database column HOT 1
- nullable belongsto relationship HOT 1
- Laravel 10 Support? HOT 2
- Docs say you should use "Post" on "Getters", but you need to use a "Get" HOT 2
- API quietly fails if policy does not exist HOT 2
- Store/Update requests aren't working with JSONAPI payload format HOT 3
- spatie/data-transfer-object is abandoned HOT 1
- Run command: route:cache (Error)
- Does not re-give relationships in query type a.b.c HOT 7
- I installed the package and followed the instructions and got an error 404 HOT 1
- unwarranted error in json HOT 1
- RestifyJs can only be accessed if authenticated. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravel-restify.