Giter Site home page Giter Site logo

Get only one media about laravel-mediable HOT 9 CLOSED

plank avatar plank commented on May 16, 2024
Get only one media

from laravel-mediable.

Comments (9)

pet1330 avatar pet1330 commented on May 16, 2024 1

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.

frasmage avatar frasmage commented on May 16, 2024

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.

borisdamevin avatar borisdamevin commented on May 16, 2024

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.

frasmage avatar frasmage commented on May 16, 2024

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.

borisdamevin avatar borisdamevin commented on May 16, 2024

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.

frasmage avatar frasmage commented on May 16, 2024

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.

borisdamevin avatar borisdamevin commented on May 16, 2024

Hi,

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

from laravel-mediable.

frasmage avatar frasmage commented on May 16, 2024

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.

borisdamevin avatar borisdamevin commented on May 16, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.