Skip to content

Commit e8e463e

Browse files
authored
Merge pull request #114 from Gustavinho/feature/use-a-model-class-in-the-data-views
feat: Use a model class as initial data
2 parents 97e458e + 0877a3d commit e8e463e

File tree

10 files changed

+103
-33
lines changed

10 files changed

+103
-33
lines changed

README.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ php artisan make:table-view UsersTableView
7575
```
7676
With this artisan command a UsersTableView.php file will be created inside the `app/Http/Livewire` directory.
7777

78-
The basic usage needs a data repository (Eloquent query), headers and rows, you can customize the items to be shown, and the headers and data for each row like this example
78+
The basic usage needs a model class, headers and rows, you can customize the items to be shown, and the headers and data for each row like this example
7979
```php
8080
<?php
8181

@@ -87,10 +87,7 @@ use App\User;
8787

8888
class UsersTableView extends TableView
8989
{
90-
public function repository(): Builder
91-
{
92-
return User::query();
93-
}
90+
protected $model = User::class;
9491

9592
public function headers(): array
9693
{
@@ -128,7 +125,7 @@ You could also use the `@livewire` blade directive.
128125

129126
At this point, you would be able to see a table with some data, the table view doesn't have any styled container or title as the image example, you can render the table view inside any container you want.
130127

131-
In the example above the view is using the User model created by default in every Laravel project, feel free to use any model you have, the method `row` is receiving a sinlge model object and you can use any property or public method you have difined inside your model.
128+
In the example above the view is using the User model created by default in every Laravel project, feel free to use any model you want, the method `row` is receiving a sinlge model object and you can use any property or public method you have difined inside your model.
132129

133130
This is the basic usage of the table view, but you can customize it with more features.
134131

@@ -183,7 +180,7 @@ Here's the plan for what's coming:
183180
### From 2.2 to 2.3
184181
- Clear your cached views `php artisan view:clear` since some of the internal components changed.
185182
- Update components
186-
- Update the renderIf() function in your action classes as follows:
183+
- Update the renderIf() function in your action classes as follows:
187184
```php
188185
<?php
189186

@@ -200,3 +197,4 @@ Here's the plan for what's coming:
200197
}
201198
}
202199
```
200+
- **(Optional)**, if your `repository` methods are returning the query object without any query applied like `User::query()`, you can define a `protected $model = User::class;` instead, this is the default behavior now, the `repository` method is still working so you don't need to change anything if you don't want to.

doc/grid-view.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,33 @@ With this artisan command a `ExampleGridView.php` file will be created inside `a
3131

3232
## Defining initial data
3333

34-
Return an `Eloquent` query with the initial data to be displayed on the grid view, it is important to return the query, not the data collection.
34+
The GridView class needs a model class to get the initial data to be displayed on the table, you can define it in the `$model` property.
3535

3636
```php
3737
use App\User;
3838

39+
protected $model = User::class;
40+
```
41+
42+
If you need an specific query as initial data you can define a `repository` method returning an `Eloquent` query with the initial data to be displayed on the grid view, it is important to return the query, not the data collection.
43+
44+
```php
45+
use App\User;
46+
use Illuminate\Database\Eloquent\Builder;
47+
48+
/**
49+
* Sets a initial query with the data to fill the table
50+
*
51+
* @return Builder Eloquent query
52+
*/
3953
public function repository(): Builder
4054
{
4155
return User::query();
4256
}
4357
```
4458

59+
If you define this method, the `$model` property is not needed anymore.
60+
4561
## Defining card data
4662

4763
You have to define a public function returning an array with the data that will be displayed on each card, this array has to include `photo`, `title`, `subtitle` and the `description`.

doc/list-view.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,31 @@ With this artisan command an `ExampleListView.php` file will be created inside t
3131

3232
## Defining initial data
3333

34-
You should return an `Eloquent` query with the initial data to be displayed on the list view, it is important to return the query, not the data collection.
34+
The ListView class needs a model class to get the initial data to be displayed on the table, you can define it in the `$model` property.
3535

3636
```php
3737
use App\User;
3838

39+
protected $model = User::class;
40+
```
41+
42+
If you need an specific query as initial data you can define a `repository` method returning an `Eloquent` query with the initial data to be displayed on the list view, it is important to return the query, not the data collection.
43+
44+
```php
45+
use App\User;
46+
use Illuminate\Database\Eloquent\Builder;
47+
48+
/**
49+
* Sets a initial query with the data to fill the table
50+
*
51+
* @return Builder Eloquent query
52+
*/
3953
public function repository(): Builder
4054
{
4155
return User::query();
4256
}
4357
```
58+
If you define this method, the `$model` property is not needed anymore.
4459

4560
## Defining data for each list item
4661

doc/table-view.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,33 @@ php artisan make:table-view UsersTableView
4444
With this artisan command a `UsersTableView.php` file will be created inside `app/Http/Livewire` directory, with this class you can customize the behavior of the table view.
4545

4646
## Defining initial data
47-
Return an `Eloquent` query with the initial data to be displayed on the table, it is important to return the query, not the data collection.
47+
The TableView class needs a model class to get the initial data to be displayed on the table, you can define it in the `$model` property.
4848

4949
```php
5050
use App\User;
5151

52+
protected $model = User::class;
53+
```
54+
55+
If you need an specific query as initial data you can define a `repository` method returning an `Eloquent` query with the initial data to be displayed on the table, it is important to return the query, not the data collection.
56+
57+
```php
58+
use App\User;
59+
use Illuminate\Database\Eloquent\Builder;
60+
61+
/**
62+
* Sets a initial query with the data to fill the table
63+
*
64+
* @return Builder Eloquent query
65+
*/
5266
public function repository(): Builder
5367
{
5468
return User::query();
5569
}
5670
```
5771

72+
If you define this method, the `$model` property is not needed anymore.
73+
5874
## Headers
5975
Return an array with all the headers you need
6076

src/Views/DataView.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,11 @@ protected function getRenderData()
9393
*/
9494
public function getItems(Searchable $searchable, Filterable $filterable, Sortable $sortable)
9595
{
96-
$query = $this->repository();
96+
if (method_exists($this, 'repository')) {
97+
$query = $this->repository();
98+
} else {
99+
$query = $this->model::query();
100+
}
97101

98102
$query = $searchable->searchItems($query, $this->searchBy, $this->search);
99103

stubs/grid-view.stub

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,9 @@ use Illuminate\Database\Eloquent\Builder;
88
class DummyClass extends GridView
99
{
1010
/**
11-
* Sets a initial query with the data to fill the grid view
12-
*
13-
* @return Builder Eloquent query
11+
* Sets a model class to get the initial data
1412
*/
15-
public function repository(): Builder
16-
{
17-
// return User::query();
18-
}
13+
protected $model = User::class;
1914

2015
/**
2116
* Sets the data to every card on the view

stubs/list-view.stub

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,9 @@ use Illuminate\Database\Eloquent\Builder;
88
class DummyClass extends ListView
99
{
1010
/**
11-
* Sets a initial query with the data to fill the table
12-
*
13-
* @return Builder Eloquent query
11+
* Sets a model class to get the initial data
1412
*/
15-
public function repository(): Builder
16-
{
17-
// return User::query();
18-
}
13+
protected $model = User::class;
1914

2015
/**
2116
* Sets the properties to every list item component

stubs/table-view.stub

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,13 @@
33
namespace DummyNamespace;
44

55
use LaravelViews\Views\TableView;
6-
use Illuminate\Database\Eloquent\Builder;
76

87
class DummyClass extends TableView
98
{
109
/**
11-
* Sets a initial query with the data to fill the table
12-
*
13-
* @return Builder Eloquent query
10+
* Sets a model class to get the initial data
1411
*/
15-
public function repository(): Builder
16-
{
17-
// return User::query();
18-
}
12+
protected $model = User::class;
1913

2014
/**
2115
* Sets the headers of the table as you want to be displayed

tests/Feature/TableViewTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use LaravelViews\Test\Mock\MockTableView;
77
use LaravelViews\Test\TestCase;
88
use Illuminate\Foundation\Testing\RefreshDatabase;
9+
use LaravelViews\Test\Mock\MockTableViewWithModelClass;
910
use Livewire\Livewire;
1011

1112
class TableViewTest extends TestCase
@@ -30,4 +31,12 @@ public function testSeeAllDataOnTheTable()
3031
Livewire::test(MockTableView::class)
3132
->assertSeeUsers($users);
3233
}
34+
35+
public function testSeeAllDataSettingAModelClass()
36+
{
37+
$users = factory(UserTest::class, 7)->create();
38+
39+
Livewire::test(MockTableViewWithModelClass::class)
40+
->assertSeeUsers($users);
41+
}
3342
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace LaravelViews\Test\Mock;
4+
5+
use LaravelViews\Test\Database\UserTest;
6+
use LaravelViews\Views\TableView;
7+
use Illuminate\Database\Eloquent\Builder;
8+
9+
class MockTableViewWithModelClass extends TableView
10+
{
11+
protected $model = UserTest::class;
12+
13+
public function headers()
14+
{
15+
return [
16+
'name',
17+
'email'
18+
];
19+
}
20+
21+
public function row(UserTest $user)
22+
{
23+
return [
24+
$user->name,
25+
$user->email
26+
];
27+
}
28+
}

0 commit comments

Comments
 (0)