#SOLVED - selecting a field from a request for Spatie Media Library

17 messages · Page 1 of 1 (latest)

late tulip
#

Good morning!!!
Silly question. I am working on Spatie Media Library building out the custom path generator. I have a line in there that looks like this:

            return 'photos/' . $media->id;
        }

What I need is something more like this

if ($media.attributes['colection_type'] == 'user_stories') {
            return 'stories/' . $media->id;
        }

In fact, IDEALLY I would want something more global so I am not writing 40 different if statements but if I have a static concept working I can refactor from there.

This obviously doesn't work. How would I structure this?
Thanks!

sweet obsidian
#

At the risk of confusing you it seems you might want to use 'single table inheritance':
https://github.com/calebporzio/parental

This is where you have a single table e.g. media and in that table you would have a type column.
Possible values for the type column could be e.g. photos , stories etc

With the above package you would have two model classes like below:

class Media extends Model {
  use HasChildren;
  
    protected $fillable = ['type']; // Add your other columns

    protected $childTypes = [
        'photos' => Photo::class,
        'stories' => Story::class,
    ];
}

class Photo extends Media {
   use HasParent;

   public function path(): Attribute {
     return 'photos/'.$this->id;
   }
}

class Story extends Media {
   use HasParent;

   public function path(): Attribute {
     return 'stories/'.$this->id;
   }
}

class YourController {
  public function show(Media $media) {
     // You can access the path and it will use the correct model
     $path = $media->path

     // ...
  }
}

The above is just an example of how it would work and obviously makes a lot of assumptions.

GitHub

Use single table inheritance in your Laravel app. Contribute to calebporzio/parental development by creating an account on GitHub.

late tulip
#

SO I kinda get where you are going with this. The issue is spatie has most of this in place with a single media table. You set a "collection_name" which in turn allows you to sort who does what and goes where.

So I always set the model name and ID to user/user_id because the media belongs to the user - then I would set the collection name to user_photos, user_stories, user_profile and so on.

So this means in a custom PathGenerator, I want to be able to store the content based on the collection. so it owuld be S3/public/user_photos/user_id/content

#

Im also reviewing your code to see if it gives me any ideas

sweet obsidian
#

I have never used media library, I guess I assumed you were using your own models with 'Photos'

late tulip
#

nah, media library does too much to NOT use it on a media heavy site

sweet obsidian
#

Im not arguing that, but i dont see any reference to a Photos model

late tulip
#

That is my fault for not being more clear. I take accountability for that. Basically under a common scenario you would assigned the model name and ID to the model in question and in a standard CMS you would most likely use the model uploading it.

The issue in my case is its all user based - so I am using the collection name to define it. which is a field on each item.

So when the $media request comes into the path generator, I need to use the collection_name found inside the request

sweet obsidian
#

Why isn't the collection_name on the Media model that is passed to the PathGenerator sufficient? Sorry if you already explained it..

late tulip
#

I think I just answered that as you were typing

#

Thats why I want to accomplish something like:

if ($media.attributes['colection_type'] == 'user_stories') {
            return 'stories/' . $media->id;
        }
sweet obsidian
#

Wouldn't it be easier if you just named your folders the same as your collection type?

return $media->collection_type.'/'.$media->id;
late tulip
#

yep

#

I just realized I was using Vue syntax

#

FML LOL

#

listen - im pretty LOL - me not so smrt somedays LOL

sweet obsidian
#

no worries have fun with it 😄