Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get only one media #83

Closed
borisdamevin opened this issue Oct 7, 2017 · 9 comments
Closed

Get only one media #83

borisdamevin opened this issue Oct 7, 2017 · 9 comments
Labels

Comments

@borisdamevin
Copy link
Contributor

borisdamevin commented Oct 7, 2017

Hi,

I try to find a method to get only one media on posts collection and not all media attach on each post.

For exemple :

A post has many media, all with same tag. I wanna get only one of them. I suppose we need to use the join method, but how ?

Now a use $posts = Post::with('media')->get();, but I dont wanna all media.

Thank you.

@frasmage
Copy link
Collaborator

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);

@borisdamevin
Copy link
Contributor Author

borisdamevin commented Oct 15, 2017

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 ?

@borisdamevin borisdamevin changed the title Select only one media Get only one media Oct 15, 2017
@frasmage
Copy link
Collaborator

Sorry, the second example above is missing a call to first() on the collection. It should be $posts->filter(...)->first()->firstMedia($tag).

@borisdamevin
Copy link
Contributor Author

borisdamevin commented Oct 16, 2017

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?

@frasmage
Copy link
Collaborator

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?

@borisdamevin
Copy link
Contributor Author

Hi,

Yes I try to get only one media on each post.

@frasmage
Copy link
Collaborator

frasmage commented Oct 16, 2017

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);
}

@pet1330
Copy link
Contributor

pet1330 commented Oct 16, 2017

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();

@borisdamevin
Copy link
Contributor Author

borisdamevin commented Oct 17, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants