Livewire data tables can't access public property - laravel

I'm working with Livewire Datatables (https://github.com/mediconesystems/livewire-datatables)
In my component, I have:
<?php
namespace App\Http\Livewire;
use App\Job;
use Mediconesystems\LivewireDatatables\Http\Livewire\LivewireDatatable;
use Mediconesystems\LivewireDatatables\Column;
use Mediconesystems\LivewireDatatables\NumberColumn;
use Mediconesystems\LivewireDatatables\DateColumn;
class JobsTable extends LivewireDatatable
{
public $model = Job::class;
public function columns()
{
return [
Column::checkbox(),
NumberColumn::name('id')->label('ID')
->linkTo('cl/jobs/edit'),
Column::name('Entry_id')->label('Entry ID')
->defaultSort('asc')->filterable(),
Column::name('state')
->label('State')->filterable(),
Column::name('title')
->label('Title'),
Column::name('description')
->label('Description')->truncate(),
];
}
public function render()
{
return view('livewire.jobs-table');
}
public function store()
{
}
}
In my routing, I'm calling this as a full page component:
Route::get('/cl/jobs', JobsTable::class);
Everything is there, but the component doesn't actually render. It only renders the default content from app.blade.php.
If I add the {{slot}} variable to my jobs-table blade file, I get an saying it does not exist. I can see in the debug bar that component is loading, but not rendering to the page.
Updated
Based on feedback, I tried getting it to render to a custom slot:
''' return view('livewire.jobs-table')->layout('livewire.jobs-table')->slot('JobsTable');
'''
Then in the blade file:
{{ $JobsTable }}
</main>
I still get an error, telling me that $JobsTable is not defined.
]1
Any suggestions as to what I'm doing wrong?

It sounds that you have a seperate löayout for this component. Take a look what layouts you have. and then try to assign the layout with ->layout('layouts.job-layout');in your render funktion.
public function render()
{
return view('livewire.jobs-table')->layout('layouts.job-layout');
}

Related

How to change layout for a full page render inline livewire component

I would like to use an inline livewire component for guest and app pages. By default, I understand that livewire reverts back to layout.app and I know you can update the default layout for all full page renders.
I am reading this docs https://laravel-livewire.com/docs/2.x/rendering-components and was able to get it working with regular approach of just having blade and .PHP file.
public function render()
{
return <<<'HTML'
<div>
example page view
</div>
HTML;
}
Is it possible to do this thing from the docs in an inline component where we return the HTML directly?
public function render()
{
return view('livewire.show-posts')
->layout('layouts.guest');
}
public function render()
{
return <<<'HTML'
<div>
example page view
</div>
HTML; ->layout('layouts.guest'); // something along the lines of this
}
No it's not possible. You have to use view()->layout() arrow function
maybe you can use it this way:
public function render()
{
$datas = [
'data' => 'example page view'
];
return view('livewire.show-posts', $data)
->layout('layouts.guest');
}

render data from controller before view

I have a project that needs to render data from the controller before pass to view on every page
Ex. controller --data--> {do something} --> view.blade
I tried middleware, but it's not working, because it ??? before the controller.
Does anyone know how to achieve this?
You might want to use the render method from a Illuminate\View\View object:
class QuestionsController
{
public function show(Question $question)
{
$content = view('questions.show', compact('question'))->render();
// Do something with the $content string.
}
}

Larvel Livewire emitting to listener from component

In Laravel Livewire as you know we can emit any listener with clicking events or from a component, my issue is i can emit listener with clicking on any Html tags, but i can't do that from mount or render function
this code work fine:
wire:click="$emit('changeMenuItems',-1,1)"
now doesn't work in this functions:
class CategoriesComponent extends Component
{
#[NoReturn] public function mount()
{
$this->emit('changeMenuItems', -1, 1);
}
public function render(): Factory|View|Application
{
$this->emit('changeMenuItems', -1, 1);
return view('livewire.backend.pages.categories-component');
}
}
Emitting listener from mount and render doesn't worked and i'm not sure whats problem but creating a simple function as model into Livewire component resolved my problem:
public function manageSidebarMenuRoutes(int $parent_menu, int $sub_menu, string $route_name)
{
// $this->emit('changeMenuItems', -1, 1);
return redirect()->route($route_name, app()->getLocale());
}
and Html code:
<li wire:click="manageSidebarMenuRoutes(1,1)}},'administrator')" class="nav-item">
...
</li>

Rendering a partial view in Zend for adding html to routing action layout

I am rendering a page with a lot of frames (XHR contentpanes via dojo). This is done through a request to IndexController which sets up regions 'header,left,right,center,footer' with the exception, that center is not filled in with contents. This in turn is set by calling PaneController in menu.onclick. Caveat; search engines indexing service does not get center region contents.. I wish to bypass AJAX loading of center, if user enters via /index/index.
Relevant snippets from IndexController:
class IndexController extends Zend_Controller_Action {
public function indexAction() {
$this->indexModel = $this->view->indexModel = new Application_Model_Index();
// Goal is to render "/pane/main/" action and capture the HTML
$this->view->mainPane = (string) $this->renderPaneMain();
return $this->render();
}
public function renderPaneMain() {
// ActionStack ?
// action() ?
return $HTML;
}
}
Relevant stuff in Pane
class PaneController extends Zend_Controller_Action {
public function preDispatch() {
// will only return a contentpane, dont render layout
if ($this->getRequest()->isXmlHttpRequest()) {
$this->_helper->layout()->disableLayout();
$this->view->doLayout = true;
}
}
public function mainAction() {
this.render("main.phtml");
}
public function init() {
$this->panesModel = new Application_Model_Panes();
$variant = $this->getRequest()->getParam('variant', '');
// routing variables need to be set, how?
if (empty($variant))
$this->_redirect('/');
}
}
Basically, i need the PaneController to _not render the global layout but call its .phtml view file, once it has been setup with relevant model entries and such.
Any ideas as to how I can achieve this in its most efficient form?
Very well, ill attach the workaround im using here
The forms and the fork-logic i have moved to the model that is coexisting with PanesController. For the IndexController, which will present the default Pane as inline HTML without AJAX - there is a couple of duplicated initializations going on.
So, IndexModel extends the PanesModel - without initializing it. In my index.phtml view (for Index action) i have following code to render the inline html from a pane.
in index controller
$this->view->model = new IndexModel(); // extends PanesModel
$this->view->model->setDefaultProperties($variant, $pagination, ...);
in index view:
$this->partial("panes/main/main.phtml", array("model", $this->model);
and from pane view:
<?php if($this->model->goThisDirection()): ?>
Switch 1 HTML contents
<?php endif; ?>
Caveat: I also had to not render any form of layout within the pane (dojox contentpanes allows for <script> and <style> tags) - and this ofc ripples to any other pane action of mine.

How to display view without template?

I have view (frontend) in my own component (view.html.php):
class MevViewMev extends JView{
function display($tpl = null){
parent::display($tpl);
}
}
And template:
<?php defined('_JEXEC') or die('Restricted access'); ?>
<div>
ASFADSFDSF
</div>
How to display it without joomla template (head section, styles, etc). I want to call this part of jquery onclick method in the window.
To display the component only add "tmpl=component" parameter to url.
If need to display something besides component's view it can be customized - create "component.php" file in template's root folder and include in it whatever you need.
More templates can be done in the same way - create "some_template.php" in template's root folder and add "tmpl=some_template" parameter to url.
Start Edit
OK so the below works, but I found a better way. In your controller do ...
if (JRequest::getVar('format') != 'raw') {
$url = JURI::current() . '?' . $_SERVER['QUERY_STRING'] . '&format=raw';
header('Location: ' . $url);
// or, if you want Content-type of text/html just use ...
// redirect($url);
}
End Edit
You can set 'tmpl' to 'component', as suggested by Babur Usenakunov, in which case scripts and css may be loaded, like ...
JRequest::setVar('tmpl','component');
However if you want to create raw output you can add &format=raw or in your component make a view of type 'raw' ...
Unfortunately the only functional way I can find to make a viewType of raw render correctly is to call exit() after the view class calls parent::display() ...
In your controller.php ...
class com_whateverController() extends JController
{
function __construct()
{
// the following is not required if you call exit() in your view class (see below) ...
JRequest::setVar('format','raw');
JFactory::$document = null;
JFactory::getDocument();
// or
//JFactory::$document = JDocument::getInstance('raw');
parent::__construct();
}
function display()
{
$view = $this->getView('whatever', 'raw');
$view->display();
}
}
then in views/whatever/view.raw.php ...
class com_whateverViewWhatever extends JView
{
public function display($tpl = null)
{
parent::display();
exit; // <- if you dont have this then the output is captured in and output buffer and then lost in the rendering
}
}
I know this comes in very late, but for future readers, here's how I did it for my extension, without editing the template, or adding anything in the URL (since I have control over neither of those):
jimport('joomla.application.component.view');
use \Joomla\CMS\Factory;
// Comp stands for the Component's name and NoTmpl stands for the View's name.
class CompViewNoTmpl extends \Joomla\CMS\MVC\View\HtmlView {
// Force this view to be component-only
public function __construct() {
$app = Factory::getApplication();
$app->input->set('tmpl', 'component');
parent::__construct();
}

Resources