Hello Artisan

In this tutorial i am going to share that in laravel how you can generate tablular data. You know that every time we need to create a table for our application crud operation. So isn’t it good that we can create a helper function for that and we will just call it where we need it.

Yes, in this example tutorial i will share with you the example code of how we can show tabular data with dynamically using helper function. I think i will help us a lot to create and show table data dynamically in laravel.

See the example code:

First create a helper function like below and update it like below.

app/helpers.php

function toTable($items, $options = null)
{
    $header = $atts = '';

    $header_keys = array_keys($items[0]);

    if(!is_null($options)) {
        if(array_key_exists('only', $options)) {
            $header_keys = $options['only'];
        }

        if(array_key_exists('attributes', $options)) {
            $attr = $options['attributes'];
        }
        else {
            $attr = array('class' => 'table table-condensed');
        }
    }
    else {
        $attr = array('class' => 'table table-condensed');
    }

    if(is_null($options) || (!isset($options['header']) || isset($options['header']) && $options['header'] != false)) {
        $header = "<thead><tr>";
        foreach ($header_keys as $value) {
            $header .= "<th>" . ucwords(str_replace('_', ' ', $value)) . "</th>";
        }

        if(isset($options['action'])) {
            $header .= "<th style='text-align:right'>Actions</th>";
        }
        $header .= "</tr></thead>";
    }

    $tbody = "<tbody>";
    foreach ($items as $values) {
        $tbody .= "<tr>";
        foreach($header_keys as $key){
            $tbody .= "<td>" . $values[$key] . "</td>";
        }

        if(isset($options['action'])) {
            $action = '<td>'. View::make($options['action'], array('item' => $values)) . '</td>';
            $tbody .= "$action</tr>";
        }
    }
    $tbody .= "</tbody>";

    if(!is_null($options) && isset($options['table']) && $options['table'] == false) return $tbody;

    if(isset($attr)) {
        foreach ($attr as $key => $value) {
            $atts .= " " . $key . "='" . $value . "'";
        }
    }

    return "<table $atts>" . $header . $tbody . "</table>";
} 

Now open web.php and update it to show the example.

routes/web.php

Route::get('/', function () {
    $data = User::paginate(10);
    return view('test',compact('data'));
});

Now in my view just have a look

resources/views/welcome.blade.php

<!DOCTYPE html>
<html lang="">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Title Page</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
    </head>
    <body>

        @yield('content')

        <script src="//code.jquery.com/jquery.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
    </body>
</html> 

#laravel #php #web-development #programming #developer

How to Generate Table Data in Laravel using Helper Function
2.10 GEEK