How can I eager load a resource collection relationship? I've made a resource which calls gravel_pits relationship
class GravelTypeResource extends Resource
{
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'deleted_at' => $this->deleted_at,
'gravel_pits' => $this->gravel_pits,
];
}
}
On the model M:M relationship:
public function gravel_pits()
{
return $this->belongsToMany('App\GravelPit');
}
And from the API I am getting it back like this:
public function index()
{
return GravelTypeResource::collection(GravelType::all());
}
I can eager load it by doing
public function index()
{
return GravelTypeResource::collection(GravelType::with('gravel_pits'));
}
which works...but I can't control then what properties of gravel pits I actually want back, instead, eager load fetches them all. Is there a simple workaround to this?
you can use Resource Collections
GravelTypeResourceCollection::make($collection);
and since you can use load and loadMissing on eloquent collections you can do this
class GravelTypeResourceCollection extends ResourceCollection
{
$collects = GravelTypeResource::class;
public function __construct($resource){
$resource->loadMissing(['gravel_pits']);
parent::__construct($resource);
}
}
You can pass in a select to get just the fields you want. Just make sure you get the fields that the relationship is based on:
return GravelTypeResource::collection(GravelType::with('gravel_pits'=>function($query) {
$query->select(['id', 'gravel_type_id', 'column3', 'column4']);
});
Related
I'm trying to implement the repository pattern and save a relationship using the create method as shown below.
abstract class EloquentRepository implements Repository {
public function create($data)
{
return $this->model->create($data);
}
}
Within my controller I have injected the repository:
public function __construct(SubscriberRepository $subscriberRepository,
SubscribableRepository $subscribableRepository)
{
$this->subscriberRepository = $subscriberRepository;
$this->subscribableRepository = $subscribableRepository;
}
My store method looks like:
public function store(CreateSubscriberRequest $request): JsonResponse
{
$subscribable = $this->subscribableRepository->findByIdentifier($request->input('type'))
->firstOrFail();
$attributes = [
'name' => $request->input('name'),
'email' => $request->input('email')
];
$subscriber = $this->subscriberRepository->create($attributes);
}
Subscriber Model
public function subscribable()
{
return $this->belongsTo(Subscribable::class, 'subscribable_id');
}
Subscribable Model
public function subscribers()
{
return $this->hasMany(Subscriber::class);
}
My issue General error: 1364 Field 'subscribable_id' doesn't have a default value is because the subscribable_id is a foreign key and not set in the create method.
How do I relate the subscribable model, setting the subscribable_id? I don't think setting the subscribable_id in the fillable property is the way to go with this.
Many thanks in advance.
Laravel gives functionality to save relations using the related model instances.
So You can save relation by calling create method on relation like this:
public function store(CreateSubscriberRequest $request): JsonResponse
{
$subscribable = $this->subscribableRepository->findByIdentifier($request->input('type'))
->firstOrFail();
$attributes = [
'name' => $request->input('name'),
'email' => $request->input('email')
];
$subscribable->subscribers()->create($attributes);
}
See laravel doc on relationship
In Laravel, if I want to create a self-referential relationship I can do the following:
class Post extends Eloquent
{
public function parent()
{
return $this->belongsTo('Post', 'parent_id');
}
public function children()
{
return $this->hasMany('Post', 'parent_id');
}
}
How can I make a Laravel Nova resource display this connection?
public function fields(Request $request)
{
return [
Text::make('Autor', 'author'),
Select::make('Type', 'type')->options([
'News' => 'news',
'Update' => 'update',
]),
BelongsToMany::make('Post') // does not work
];
}
You can achieve what you want like this:
BelongsTo::make('Parent', 'parent', \App\Nova\Post::class),
HasMany::make('Children', 'children', \App\Nova\Post::class),
This will allow to choose a parent post when you create or update a post. When you are in the detail page of a post, you can see all its children.
public function fields(Request $request)
{
return [
Text::make('Author', 'author'),
Select::make('Type','type')->options([
'News' => 'news',
'Update' => 'update',
]),
BelongsTo::make('Parent', 'parent', \App\Nova\Post::class),
HasMany::make('Children', 'children', \App\Nova\Post::class),
];
}
Note: Please note that the third param to BelongsTo::make() and HasMany::make() is a reference to the Post Resource, not Post model.
There is another situation, where you will find same issue, if you have parent column name parent and also relationship parent like
$table->bigIncrements('id');
$table->string('category');
$table->unsignedBigInteger('parent')->nullable();
and
In model
public function parent()
{
return $this->belongsTo(SELF::class, 'parent');
}
It will be unable to recognize the parent property and you will face this problem again, in that case, you can change the relationship name or column name, and it will work fine.
Also remember the arguments for Nova BelongsTo relationship
Argument 1. Name to display (e.g. Parent)
Argument 2. Name of the relationship as used in the model (e.g. parent)
Argument 3. The Nova Resource (e.g. App\Nova\Category)
Laravel 5.7. I have a model Audio, with the fields id and title. An Audio can have many AudioVersions, where AudioVersion has id, audio_id (referring to Audio) and url.
Now, I have two parent models, Foo and Bar, which can have many Audio models.
Audio:
class Audio extends Model
{
public function versions()
{
return $this->hasMany('App\AudioVersion', 'audio_id');
}
}
AudioVersion:
class AudioVersion extends Model
{
public function audio()
{
return $this->belongsTo('App\AudioContent');
}
}
Foo:
class Foo extends Model
{
public function audioContents()
{
return $this->morphToMany('App\Audio', 'audio_contentable', 'audio_contentable');
}
}
I have an Eloquent API resource, FooResource, which returns its Audio objects:
FooResource:
class FooResource extends JsonResource
{
public function toArray($request)
{
return [
'audio' => AudioResource::collection($this->audioContents),
];
}
}
AudioResource:
class AudioResource extends JsonResource
{
public function toArray($request)
{
return [
'urls' => $this->versions,
];
}
}
My problem is that in the audio key of my FooResource I only want to return Audios which have AudioVersions related to them. I.e. if I have an Audio with no AudioVersions, I do not want that Audio included in the Foo's audio key. I can't find a way to do this deep conditional logic in Eloquent / Resources.
In the FooResource class, you could filter() the collection before passing it to the collection method.
class FooResource extends JsonResource
{
public function toArray($request)
{
$audioContents = $this->audioContents()->filter(function($audio, $key) {
return $audio->versions->count();
}
return [
'audio' => AudioResource::collection($audioContents),
];
}
}
You need to do something like this. This is the example, you only need to add join inside with so that only those audios will be fetched which have audio versions.
$audioContents = AudioContents::with([
'audio' => function ($query) use ($SpecificID) {
return $query->join("audio_versions")
->on("audio_versions.audio_id", "=", "audios.id");
}
])->get();
Try this and let me know if you are facing any issue.
In the end I added a scope to Audio:
public function scopeHasVersions($query)
{
return $query->whereHas('versions');
}
Then in FooResource:
return [
'audio' => AudioResource::collection($this->audioContents()->hasVersions()->get()),
];
I'm trying to retrieve data from two tables with relationship one-to-many. I need it to return in one response since it will be displayed simultaneously. However, I don't understand how to return extended object. For now I have something like this in controller
public function show(Site $id)
{
foreach ($id->features() as $feature) {
$id->features[] = $feature;
}
return $id;
}
And this is my model
class Site extends Model
{
protected $fillable = ['path', 'site_link'];
public $timestamps = false;
public function features() {
return $this->hasMany('App\SiteFeature');
}
}
For now it returns an empty array of features property.
If you are using implicit model binding then Lazy eager Load features relationship in controller action because model is already loaded
public function show(Site $site)
{
$site->load('features');
return $site;
}
If no implicit model binding then Eager load features using with at time of model loading
public function show($id)
{
$site = Site::with('features')->find($id);
return $site;
}
Check details for loading relationship https://laravel.com/docs/5.6/eloquent-relationships#constraining-eager-loads
This question was already asked here but it received no answer. Now I face the same problem but in laravel 5.4. I have a model Book, a model ReadingSession and a model Comment. A book has many reading sessions and has many comments but the reading session can also have comments. So I have my relations defined like this:
Book.php
protected $with = [
'author',
'readingSessions',
'userRating',
'ratings',
'comments'
];
public function users()
{
return $this->belongsToMany(User::class, 'user_book');
}
public function author()
{
return $this->belongsTo(Author::class);
}
public function allReadingSessions()
{
return $this->hasMany(ReadingSession::class);
}
public function readingSessions()
{
return $this->hasMany(ReadingSession::class)
->where('user_id', Auth::user()->id);
}
public function ratings()
{
return $this->hasMany(Rating::class);
}
public function userRating()
{
return $this->hasMany(Rating::class)
->where('user_id', Auth::user()->id);
}
public function comments()
{
return $this->morphMany('App\Models\Comment', 'commentable');
}
ReadingSession.php
protected $with = ['comments'];
public function user()
{
return $this->belongsTo(User::class);
}
public function book()
{
return $this->belongsTo(Book::class);
}
public function comments()
{
return $this->morphMany('App\Models\Comment', 'commentable');
}
Comment.php
public function commentable()
{
return $this->morphTo();
}
These seems to create an infinite loop. Can anyone hint me on what I'm doing wrong?
The main reason you might have an infinite loop there is if you are trying to load automatically a relationship that in turn tries to do the same with the previous model.
Putting it into an example:
Book.php
protected $with = [
'author',
];
public function author()
{
return $this->belongsTo(Author::class);
}
Author.php
protected $with = [
'books',
];
public function books()
{
return $this->hasMany(Book::class);
}
In this case, every time you fetch an author it will fetch automatically his books that in turn will try to fetch the author and on and on...
One other thing that might happen and it's harder to realize is when using the $appends property on some accessors. If you are trying automatically had a variable into a model through the $appends and if that accessor fetches a relation or uses a relation in some way you might get an infinite loop again.
Example:
Author.php
protected $appends = [
'AllBooks',
];
public function books()
{
return $this->hasMany(Book::class);
}
public function getAllBooksAttribute() {
return $this->books->something...
}
In this case, every time the app tries to resolve your Author model it will fetch the books, that in turn will fetch the Author, that in turn will fetch the books again and on and on...
From your snippets, is not clear what is causing the problem but this answer might give some leads where to search for it.
To solve it, you might remove the relation from the $with and load it manually: $author->load('books') or Author::with('books')->where...
You can also load a relation of a relation in this way, for example: $author->load('books', 'books.comments') or Author::with('books', 'books.comments')->where...
It all comes down what you are trying to achieve. So you have to evaluate what and what not you should auto-load.
Be careful when loading automatically relations on your models and when adding accessors to $appends, especially if they use relations. It is an awesome feature but can bite hard sometimes.