Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

DataSet

Felice Ostuni edited this page Sep 21, 2015 · 5 revisions

DataSet can paginate results starting from query, eloquent collection or multidimensional array.
It adds the ability to order result and keep persistence of all params in query string.

i.e.:

/dataset/uri?page=2&ord=-name   will get page 2 order by "name" descending
/dataset/uri?page=3&ord=name&other=xx  will get page 3 order by "name"

in a controller

   //using table name
   $dataset = \DataSet::source("tablename")->paginate(10)->getSet();

   //or using query
   $dataset = \DataSet::source(DB::table('users')->select('name', 'email'))
                      ->paginate(10)->getSet();

   //or using eloquent model or eloquent builder 
   $dataset = \DataSet::source(new Article)->paginate(10)->getSet();
   $dataset = \DataSet::source(Article::with('author'))->paginate(10)->getSet();

   //or using array
   $dataset = \DataSet::source($multidimensional_array)->paginate(10)->getSet();

in a view you can use

<p>
    //cycle
    @foreach ($dataset->data as $item)

        {{ $item->title }}<br />
        {{ $item->author->name }}<br />

    @endforeach

    {{ $dataset->links() }} <br />

    //sort link
    {{ $dataset->orderbyLink('title', 'asc') }} <br />
</p>

As you see you can build a dataset using "one row of code" (with chaining), however I suggest you this alternative syntax to be more in "standard" with other widgets:

   $set = \DataSet::source(Article::with('author'));
   $set->paginate(10);
   $set->addOrderBy(['title','id']);
   $set->orderBy('title','asc');
   $set->build();
   
   view('articles', compact('set'))

the code you see will build an eloquent collection:

  • of articles with relation of author
  • will be paginated a 10 row per page
  • will accept orderby 'title' and order by 'id' (through ?ord={field})
  • will order by default by title ascending
Clone this wiki locally