PHP code example of waad / media

1. Go to this page and download the library: Download waad/media library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

waad / media example snippets

sh
php artisan vendor:publish --provider="Waad\Media\MediaServiceProvider" --tag="media-config"
sh
php artisan optimize
sh
php artisan vendor:publish --provider="Waad\Media\MediaServiceProvider" --tag="media-migrations"
sh
php artisan migrate
sh
php artisan media:link
js
$post = Post::create([
  ...........
]);

$files = $request->file('image'); // one image
$files = $request->file('images'); // many images

// version < 2 
// will return an array of file names
$media = $post->addMedia($files); 
$media = $post->addMedia($files, $index = 1, $label = 'cover'); 


// ***************************************************


// version >= 2 
// will return the Media model or array of Media models on the Relationship
$media = $post->addMedia($files)->upload();
$media = $post->addMedia($files)->label('cover')->index(3)->upload();
$media = $post->addMedia($files)->disk('public')->directory('posts/video')->label('cover')->index(3)->upload();


return $media;
js
$post = Post::find(1);
$post->update([
  ...........
]);

$files = $request->file('image'); // one image
$files = $request->file('images'); // many images

// version < 2 
// will return an array of file names
$media = $post->syncMedia($files);
$media = $post->syncMedia($files, $index = 2);


// ***************************************************


// version >= 2 
// will return the Media model or array of Media Models on the Relationship
$media = $post->syncMedia($files)->sync();
$media = $post->syncMedia($files, $ids = [1,3])->sync(); // delete only these $ids and upload new files
$media = $post->syncMedia($files)->label('cover')->index(3)->sync();
$media = $post->syncMedia($files)->disk('public')->directory('posts/video')->label('cover')->index(3)->sync();

return $media;
js
$post->media->approve();   // put approved = true

$post->media->disApprove();   // put approved = false
js
// if was HasOneMedia
1 - optional($post->media)->user;
2 - Post::with('media.user')->find(1);

//**********************************************

// if was HasManyMedia
1 - Post::with('media.user')->get();
2 - $post->media->load('user');
js
$post->media->approved();  // approved = true
js
protected function schedule(Schedule $schedule)
    {
        // .....................

        $schedule->command('media:prune')->daily();
    }
sh
php artisan media:prune