Skip to content

Commit

Permalink
Merge pull request #14 from FarhanShares/eager-load
Browse files Browse the repository at this point in the history
Update migrations
  • Loading branch information
FarhanShares authored Mar 4, 2021
2 parents e4d79be + 680f989 commit 110c6f9
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ class CreateMediaManCollectionMediaTable extends Migration
*/
public function up()
{
Schema::table(config('mediaman.tables.collection_media'), function (Blueprint $table) {
$table->increments('id');
$table->integer('collection_id');
$table->integer('media_id');
Schema::create(config('mediaman.tables.collection_media'), function (Blueprint $table) {
$table->unsignedBigInteger('collection_id');
$table->unsignedBigInteger('media_id');

$table->foreign('collection_id')
->references('id')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php

use App\Models\MediaCollection;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
Expand All @@ -15,7 +14,7 @@ class CreateMediaManCollectionsTable extends Migration
public function up()
{
Schema::create(config('mediaman.tables.collections'), function (Blueprint $table) {
$table->id();
$table->bigIncrements('id');
$table->string('name');
$table->timestamps();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public function up()
Schema::create(config('mediaman.tables.media'), function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('disk');
$table->string('display_name');
$table->string('name');
$table->string('file_name');
$table->string('mime_type');
$table->unsignedInteger('size');
$table->json('data')->nullable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class CreateMediamanMediablesTable extends Migration
public function up()
{
Schema::create(config('mediaman.tables.mediables'), function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('media_id')->index();
$table->unsignedBigInteger('mediable_id')->index();
$table->string('mediable_type');
Expand Down
2 changes: 1 addition & 1 deletion src/MediaManServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function boot()
*
*/

protected function getMigrationFileSource(string $name, string $ext = '.stub.php')
protected function getMigrationFileSource(string $name, string $ext = '.stub')
{
return __DIR__ . '/../database/migrations/' . $name . $ext;
}
Expand Down
15 changes: 8 additions & 7 deletions src/Traits/HasMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ trait HasMedia
*/
public function media(): MorphToMany
{
// todo: with('collections')
return $this
->morphToMany(config('mediaman.models.media'), 'mediable', config('mediaman.tables.mediables'))
->withPivot('channel');
Expand All @@ -43,10 +42,13 @@ public function hasMedia(string $channel = 'default')
* @param string $group
* @return mixed
*/
public function getMedia(string $channel = 'default')
public function getMedia(?string $channel = 'default')
{
// todo: if channel is false/null -> exclude where clause
return $this->media->where('pivot.channel', $channel);
if ($channel) {
return $this->media->where('pivot.channel', $channel);
}

return $this->media;
}

/**
Expand All @@ -55,9 +57,8 @@ public function getMedia(string $channel = 'default')
* @param string $channel
* @return mixed
*/
public function getFirstMedia(string $channel = 'default')
public function getFirstMedia(?string $channel = 'default')
{
// todo: with('collections')
return $this->getMedia($channel)->first();
}

Expand All @@ -68,7 +69,7 @@ public function getFirstMedia(string $channel = 'default')
* @param string $conversion
* @return string
*/
public function getFirstMediaUrl(string $channel = 'default', string $conversion = '')
public function getFirstMediaUrl(?string $channel = 'default', string $conversion = '')
{
if (!$media = $this->getFirstMedia($channel)) {
return '';
Expand Down

0 comments on commit 110c6f9

Please sign in to comment.