From 756271e8addcda1278179fc2f916328f03d2290a Mon Sep 17 00:00:00 2001 From: Mauro Nidola Date: Tue, 30 Apr 2019 17:30:29 +0200 Subject: [PATCH 1/3] fix issue with index length on utf8mb4 --- database/migrations/2017_08_30_101111_create_users_table.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database/migrations/2017_08_30_101111_create_users_table.php b/database/migrations/2017_08_30_101111_create_users_table.php index 8dccaf0..864d055 100644 --- a/database/migrations/2017_08_30_101111_create_users_table.php +++ b/database/migrations/2017_08_30_101111_create_users_table.php @@ -17,7 +17,7 @@ public function up() Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); - $table->string('email')->unique(); + $table->string('email', 191)->unique(); $table->timestamps(); }); } From d18f1b8903e3078edf135d4b036b396e2a316575 Mon Sep 17 00:00:00 2001 From: Mauro Nidola Date: Tue, 30 Apr 2019 17:40:27 +0200 Subject: [PATCH 2/3] put sortable links in table head --- resources/views/user.blade.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/resources/views/user.blade.php b/resources/views/user.blade.php index 697bb2f..2bf5192 100644 --- a/resources/views/user.blade.php +++ b/resources/views/user.blade.php @@ -1,12 +1,14 @@
- @sortablelink('detail.phone_number', trans('column-sortable.phone')) - @sortablelink('id', trans('column-sortable.id'), ['joe' => 'doe', 'jane' => 'doe'], ['class' => 'abc', 'rel' => 'nofollow', 'disabled' => 'disabled']) - @sortablelink('name', 'Name') - @sortablelink('nick_name', 'nick') - @sortablelink('email') - @sortablelink('address') + + + + + + + + @foreach ($users as $user) From 611f9bedd4a5958ca609ae208900c09aad15c41c Mon Sep 17 00:00:00 2001 From: Mauro Nidola Date: Tue, 30 Apr 2019 18:00:58 +0200 Subject: [PATCH 3/3] add projects and demonstrate issue with withCount --- app/Http/Controllers/UserController.php | 12 ++++++- app/Project.php | 17 ++++++++++ app/User.php | 14 +++++++- database/factories/Factories.php | 8 +++++ ...019_04_30_153250_create_projects_table.php | 34 +++++++++++++++++++ database/seeds/UsersTableSeeder.php | 1 + resources/views/user.blade.php | 4 +++ 7 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 app/Project.php create mode 100644 database/migrations/2019_04_30_153250_create_projects_table.php diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 9198a09..fbf6047 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -10,7 +10,17 @@ class UserController extends Controller public function index(User $user) { try { - $users = $user->with('detail')->select(['*', 'name as nick_name'])->sortable()->paginate(10); + $users = $user + ->with('detail') + ->select(['*', 'name as nick_name']) + ->withCount([ + 'projects', + 'projects as top_rating_projects_count' => function ($query) { + $query->where('rating', 5); + }, + ]) + ->sortable() + ->paginate(10); return view('user', ['users' => $users]); } catch (\Kyslik\ColumnSortable\Exceptions\ColumnSortableException $e) { diff --git a/app/Project.php b/app/Project.php new file mode 100644 index 0000000..f978477 --- /dev/null +++ b/app/Project.php @@ -0,0 +1,17 @@ +belongsTo(User::class); + } +} diff --git a/app/User.php b/app/User.php index 13efca8..72f6307 100644 --- a/app/User.php +++ b/app/User.php @@ -22,7 +22,11 @@ class User extends Model * * @var array */ - public $sortableAs = ['nick_name']; + public $sortableAs = [ + 'nick_name', + 'projects_count', + 'top_rating_projects_count', + ]; /** * The attributes that are mass assignable. @@ -64,4 +68,12 @@ public function details() { return $this->hasMany(UserDetail::class); } + + /** + * @return \Illuminate\Database\Eloquent\Relations\HasMany + */ + public function projects() + { + return $this->hasMany(Project::class); + } } diff --git a/database/factories/Factories.php b/database/factories/Factories.php index dc5c1f6..cf2a641 100644 --- a/database/factories/Factories.php +++ b/database/factories/Factories.php @@ -1,5 +1,6 @@ $faker->address ]; }); + +$factory->define(App\Project::class, function (Faker $faker) { + return [ + 'name' => $faker->sentence, + 'rating' => rand(1,5) + ]; +}); diff --git a/database/migrations/2019_04_30_153250_create_projects_table.php b/database/migrations/2019_04_30_153250_create_projects_table.php new file mode 100644 index 0000000..978fe4b --- /dev/null +++ b/database/migrations/2019_04_30_153250_create_projects_table.php @@ -0,0 +1,34 @@ +bigIncrements('id'); + $table->unsignedInteger('user_id'); + $table->string('name'); + $table->unsignedInteger('rating'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('projects'); + } +} diff --git a/database/seeds/UsersTableSeeder.php b/database/seeds/UsersTableSeeder.php index 00f989e..a5d97c0 100644 --- a/database/seeds/UsersTableSeeder.php +++ b/database/seeds/UsersTableSeeder.php @@ -13,6 +13,7 @@ public function run() { factory(App\User::class, 50)->create()->each(function ($u) { $u->detail()->save(factory(App\UserDetail::class)->make(['user_id' => $u->id])); + $u->projects()->saveMany(factory(App\Project::class, rand(5, 50))->make(['user_id' => $u->id])); }); } } diff --git a/resources/views/user.blade.php b/resources/views/user.blade.php index 2bf5192..ed10cbb 100644 --- a/resources/views/user.blade.php +++ b/resources/views/user.blade.php @@ -8,6 +8,8 @@ + + @foreach ($users as $user) @@ -17,6 +19,8 @@ + + @endforeach
@sortablelink('detail.phone_number', trans('column-sortable.phone'))@sortablelink('id', trans('column-sortable.id'), ['joe' => 'doe', 'jane' => 'doe'], ['class' => 'abc', 'rel' => 'nofollow', 'disabled' => 'disabled'])@sortablelink('name', 'Name')@sortablelink('nick_name', 'nick')@sortablelink('email')@sortablelink('address')
{{ $user->detail->phone_number }}@sortablelink('nick_name', 'nick') @sortablelink('email') @sortablelink('address')@sortablelink('projects_count')@sortablelink('top_rating_projects_count')
{{ $user->name }} {{ $user->email }} {{ $user->detail->address }}{{ $user->projects_count }}{{ $user->top_rating_projects_count }}