-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Using a custom collection for a model #17
Comments
Hi @mikemand I must confess I've not tried it with a custom collection. The newCollection method is important, as we use that to tell each model in the collection that they are part of the collection (out of the box, a model in a Laravel collection doesn't actually have any knowledge of the collection it's a member of). If you can send over some sample code that shows how you're trying to use it I'll have a play with it and get back to you, |
Hi @liam-wiltshire, It's pretty generic, I think. Here's the code for my model: <?
namespace App\Entities;
use App\Entities\Collections\SaleCollection;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use LiamWiltshire\LaravelJitLoader\Concerns\AutoloadsRelationships;
class Sale extends Model
{
use SoftDeletes, AutoloadsRelationships;
/**
* @return \App\Entities\Collections\SaleCollection
*/
public function newCollection(array $models = []): Collection
{
return new SaleCollection($models);
}
} My Sale model also has a few relationships to things like I should say that I am already using the If you have any other questions or need more clarification, just let me know. Thanks for looking into this! |
Assuming you are not doing anything in the collection around the public function newCollection(array $models = []): Collection
{
$collection = new SaleCollection($models);
unset($models);
foreach ($collection as $model) {
$model->parentCollection = $collection;
}
return $collection;
} The important bit in there as I think I mentioned above is making sure each model in the collection knows it is part of the collection - otherwise when a relationship is called, it will not know to call it on the collection. |
Hi Liam, Awesome, good to know. Thank you for taking the time to look into this for me. I only have a few more questions for you, but these are probably more because I'm not super conscious of high-level optimization:
|
Hey @mikemand, No problem at all. In answer to your questions:
Depending on how you are using each, it should be exactly the same. I say this because if you are doing something like The caveat to this is the currently the JIT loader only partially supports nested relationships (so if inside your
Yes it almost certainly could, the gc would pick it up when the function all ends, however it doesn't have a negative impact, and I like to keep things tidy :-) Hope this helps :-) |
Hi Liam, I was more wondering about within the |
Hello,
I have a model that I use a custom collection with, to do some aggregations and such. Currently, I have to set my return typehint to
\Illuminate\Database\Eloquent\Collection
instead of my custom collection, because of:laravel-jit-loader/src/Concerns/AutoloadsRelationships.php
Line 143 in 6f0091e
Not a deal-breaker, as I can use the phpdoc to tell my IDE what the real return type is. However, with me overwriting the
newCollection
method, what does that do to autoloading for this model? As far as I can tell, the only important line is 145, but I don't know what it does exactly...Are there some side-effects I am not aware of with using my own custom collection with your package?
The text was updated successfully, but these errors were encountered: