From 3b123d7f0885212e69982b522b77c2f17264d615 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Sun, 22 Jul 2018 16:07:27 +0200 Subject: [PATCH] instancecache trait --- app/Models/Concerns/InstanceCache.php | 37 ++++++++++++++++++++++ app/Models/Table.php | 6 ++++ app/Models/User.php | 2 ++ resources/views/user/_table-list.blade.php | 29 +++++++++++++++++ resources/views/user/view.blade.php | 31 +----------------- 5 files changed, 75 insertions(+), 30 deletions(-) create mode 100644 app/Models/Concerns/InstanceCache.php create mode 100644 resources/views/user/_table-list.blade.php diff --git a/app/Models/Concerns/InstanceCache.php b/app/Models/Concerns/InstanceCache.php new file mode 100644 index 0000000..7926cd9 --- /dev/null +++ b/app/Models/Concerns/InstanceCache.php @@ -0,0 +1,37 @@ +getKey()] = $instance; + }); + } + + /** + * Find entity by ID, either in DB, or in the internal cache. + * + * @param int $id + * @return Model|null + */ + public static function getCached($id) + { + return isset(self::$instanceCache[$id]) ? + self::$instanceCache[$id] : + (self::$instanceCache[$id] = self::find($id)); + } +} diff --git a/app/Models/Table.php b/app/Models/Table.php index f07047b..4cfaecb 100644 --- a/app/Models/Table.php +++ b/app/Models/Table.php @@ -37,6 +37,12 @@ class Table extends Model use Reportable; protected $guarded = []; + /** Get owner from the instance cache (use when building table lists) */ + public function cachedOwner() + { + return User::getCached($this->owner_id); + } + /** Owning user */ public function owner() { diff --git a/app/Models/User.php b/app/Models/User.php index 81403db..bc5e622 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -3,6 +3,7 @@ namespace App\Models; use AdamWathan\EloquentOAuth\OAuthIdentity; +use App\Models\Concerns\InstanceCache; use App\Models\Concerns\Reportable; use Illuminate\Database\Eloquent\Collection; use Illuminate\Notifications\Notifiable; @@ -38,6 +39,7 @@ class User extends Authenticatable { use Reportable; use Notifiable; + use InstanceCache; /** * The attributes that are mass assignable. diff --git a/resources/views/user/_table-list.blade.php b/resources/views/user/_table-list.blade.php new file mode 100644 index 0000000..8953ca4 --- /dev/null +++ b/resources/views/user/_table-list.blade.php @@ -0,0 +1,29 @@ +
+ @if(count($tables) == 0) + No tables yet. + @else + @foreach($tables as $table) + + + + {{ $table->title }} + {{-- + --}} + {{ $table->revision->row_count }} rows + + + + + + {{ ellipsis($table->description, 215) }} + {{-- + --}} + Last change {{ $table->updated_at->diffForHumans() }} + + + + @endforeach + {{ $tables->links() }} + @endif +
diff --git a/resources/views/user/view.blade.php b/resources/views/user/view.blade.php index e350ad8..fcfeb74 100644 --- a/resources/views/user/view.blade.php +++ b/resources/views/user/view.blade.php @@ -65,7 +65,6 @@ - {{-- Table list card --}}
@@ -83,35 +82,7 @@ @endif
- + @include('user._table-list')