Comments (9)
I think what you're looking to do is only fetch from the database a single Media
model per Post
, rather than fetching all attached Media
and iterating through the collection to filter out all but one. In which case I think you will need to use the query builder to do something like:
$PostsWithOneMedia = Post::with(['media' => function($q) {
$q->latest()->limit(1);
}])->get();
Or for a specific tag
$tag = "uploaded";
$PostsWithOneMedia = Post::with(['media' => function($q) use ($tag) {
$q->wherePivot('tag', $tag)->latest()->limit(1);
}])->get();
from laravel-mediable.
Hi @borisdamevin,
If I understand correctly, you have a posts
collection, with media loaded and want to access a single media record attached to any one of the posts at a given tag?
If all posts have media attached to that tag, then $posts->first()->firstMedia($tag)
will do. Otherwise you would need to filter down the posts to only include ones that do, either by adding conditions to the Post
query or filtering the resulting collection first.
$media = Post::whereHasMedia($tag) // restrict to results with that media
->withMedia($tag) // eager load the media at that tag
->first() //only load one post result
->firstMedia($tag); //get the first media record attached to this post
or
$posts = Posts::withMedia()->get(); // load all posts
$media = $posts->filter(function($post) use ($tag) {
return $post->hasMedia($tag); //restrict to those that have media for that tag
})->firstMedia($tag);
from laravel-mediable.
Yes.
But firstMedia()
work only for get media in one post
, not for a collection of posts
.
The best way will not to create a scope
for get only the first media in collection ?
from laravel-mediable.
Sorry, the second example above is missing a call to first()
on the collection. It should be $posts->filter(...)->first()->firstMedia($tag)
.
from laravel-mediable.
The filter don't work. He get the same media for all posts.
You don't have an idea for filter $posts = Posts::withMedia()->get();
directly on the select for get one media and not overload the database?
from laravel-mediable.
Hi @borisdamevin, I not clear what your intention is here. From your description it sounds like you are trying to get a single media from a collection of mediables, but if that is not the case, can you be more descriptive of your needs?
Are you trying to create a collection with one media record for each post?
from laravel-mediable.
Hi,
Yes I try to get only one media on each post.
from laravel-mediable.
Ok, that wasn't clear from your initial message. This isn't really a Mediable concern, but with Laravel collection handling.
$media = $posts
->filter(...) //as above, use if you want to exclude posts that do not have media at that tag
->map(function($post) use ($tag){
return $post->firstMedia($tag);
}
from laravel-mediable.
I finally find the good with your help.
$posts = Post::with(['media' => function($q){
$q->wherePivot('tag', 'thumbnail')->wherePivot('order', '1');
}])->get();
Thank for your help :).
Sorry if my request was not clear.
from laravel-mediable.
Related Issues (20)
- Documentation is not clear how to use the package HOT 4
- SourceAdapter\UploadedFileAdapter bad mime_type from client HOT 2
- PHPStan - Add template to MediableCollection HOT 3
- How to build filament component as plank does not exposes any interface like HasMedia HOT 3
- Move media to root directory HOT 2
- File gets renamed upon upload? HOT 1
- Call to undefined method Plank\Mediable\ImageManipulation::toPngFormat() HOT 4
- Files are not removed from disk on deleting the variants HOT 3
- The Chinese file name is automatically changed to pinyin,I want a Chinese name HOT 2
- Import changes visility on original file HOT 4
- Support `intervention/image` ^3.0 HOT 10
- Upload Media on AWS S3 private Bucket HOT 4
- detach relations does not work on delete HOT 1
- base64 uploading HOT 5
- Add Support for Laravel 11.0
- Change Facade HOT 1
- New migration is not published when using the provider. HOT 4
- Intervention Image Error HOT 14
- URL Attribute issue with Private Buckets HOT 2
- Can not upgrade to new version, I've read all upgrade guides HOT 16
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-mediable.