Skip to content

Commit

Permalink
Render standard queries properly in more use cases
Browse files Browse the repository at this point in the history
  • Loading branch information
gbrock committed Oct 14, 2015
1 parent b0e0f76 commit eaf5e75
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
2 changes: 1 addition & 1 deletion resources/views/table.blade.php
Expand Up @@ -48,7 +48,7 @@
</tbody>
</table>

@if(class_basename(get_class($rows)) == 'LengthAwarePaginator')
@if(is_object($rows) && class_basename(get_class($rows)) == 'LengthAwarePaginator')
{{-- Collection is paginated, so render that --}}
{!! $rows->render() !!}
@endif
14 changes: 13 additions & 1 deletion src/BlankModel.php
Expand Up @@ -2,7 +2,9 @@

namespace Gbrock\Table;

class BlankModel {
use Illuminate\Contracts\Support\Arrayable;

class BlankModel implements Arrayable {
private $attributes;

public function __construct($attributes)
Expand All @@ -19,4 +21,14 @@ public function __call($name, $arguments = [])
{
return;
}

/**
* Get the instance as an array.
*
* @return array
*/
public function toArray()
{
return $this->attributes;
}
}
13 changes: 8 additions & 5 deletions src/Table.php
Expand Up @@ -21,7 +21,7 @@ public function __construct($models = [], $columns = [])

if (!$columns && $columns !== false) {
// Columns were not passed and were not prevented from auto-generation; generate them
$columns = $this->getFieldsFromModels($models);
$columns = $this->getFieldsFromModels($this->models);
}

$this->setColumns($columns);
Expand Down Expand Up @@ -191,12 +191,15 @@ public function setModels($models)
{
if (!is_object($models)) {
if (is_array($models)) {
foreach ($models as $k => $v) {
$models[$k] = new BlankModel($v);
}
$models = collect($models);
}
}

$models = collect($models);
if(class_basename($models->first()) == 'stdClass')
{
$models = $models->map(function ($array) {
return new BlankModel($array);
});
}

$this->models = $models;
Expand Down

0 comments on commit eaf5e75

Please sign in to comment.