class('table default-table'); $this->setHeaders($headers); $this->setRows($rows); $this->setStyle($style); } /** * Set table headers. * * @param array $headers * * @return $this */ public function setHeaders($headers = []) { $this->headers = $headers; return $this; } /** * @param int $depth * * @return $this */ public function depth(int $depth) { $this->depth = $depth; return $this; } /** * Set table rows. * * @param array $rows * * @return $this */ public function setRows($rows = []) { if ($rows && ! Arr::isAssoc(Helper::array($rows))) { $this->rows = $rows; return $this; } $noTrPadding = false; foreach ($rows as $key => $item) { if (is_array($item)) { if (Arr::isAssoc($item)) { $borderLeft = $this->depth ? 'table-left-border-nofirst' : 'table-left-border'; $item = static::make($item) ->depth($this->depth + 1) ->class('table-no-top-border '.$borderLeft, true) ->render(); if (! $noTrPadding) { $this->class('table-no-tr-padding', true); } $noTrPadding = true; } else { $item = json_encode($item, JSON_UNESCAPED_UNICODE); } } $this->rows[] = [$key, $item]; } return $this; } /** * Set table style. * * @param array $style * * @return $this */ public function setStyle($style = []) { if ($style) { $this->class(implode(' ', (array) $style), true); } return $this; } /** * Render the table. * * @return string */ public function render() { $vars = [ 'headers' => $this->headers, 'rows' => $this->rows, 'attributes' => $this->formatHtmlAttributes(), ]; return view($this->view, $vars)->render(); } /** * @return $this */ public function withBorder() { $this->class('table-bordered', true); return $this; } }