Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reproducing bug 49 #1

Open
wants to merge 3 commits into
base: L5.8
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
17 changes: 17 additions & 0 deletions app/Project.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Kyslik\ColumnSortable\Sortable;

class Project extends Model
{
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function projects()
{
return $this->belongsTo(User::class);
}
}
14 changes: 13 additions & 1 deletion app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
}
8 changes: 8 additions & 0 deletions database/factories/Factories.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use App\UserDetail;
use Faker\Generator as Faker;

/*
Expand All @@ -26,3 +27,10 @@
'address' => $faker->address
];
});

$factory->define(App\Project::class, function (Faker $faker) {
return [
'name' => $faker->sentence,
'rating' => rand(1,5)
];
});
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}
Expand Down
34 changes: 34 additions & 0 deletions database/migrations/2019_04_30_153250_create_projects_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateProjectsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('projects', function (Blueprint $table) {
$table->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');
}
}
1 change: 1 addition & 0 deletions database/seeds/UsersTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]));
});
}
}
18 changes: 12 additions & 6 deletions resources/views/user.blade.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<div class="container">
@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')
<table>
<tr>
<th>@sortablelink('detail.phone_number', trans('column-sortable.phone'))</th>
<th>@sortablelink('id', trans('column-sortable.id'), ['joe' => 'doe', 'jane' => 'doe'], ['class' => 'abc', 'rel' => 'nofollow', 'disabled' => 'disabled'])</th>
<th>@sortablelink('name', 'Name')</th>
<th>@sortablelink('nick_name', 'nick')</th>
<th>@sortablelink('email')</th>
<th>@sortablelink('address')</th>
<th>@sortablelink('projects_count')</th>
<th>@sortablelink('top_rating_projects_count')</th>
</tr>
@foreach ($users as $user)
<tr>
<td>{{ $user->detail->phone_number }}</td>
Expand All @@ -15,6 +19,8 @@
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
<td>{{ $user->detail->address }}</td>
<td>{{ $user->projects_count }}</td>
<td>{{ $user->top_rating_projects_count }}</td>
</tr>
@endforeach
</table>
Expand Down