php - Laravel 7 - Component without view
Get the solution ↓↓↓I want to create a Component that provides only data without a view.
For example:
<x:wg.items.listing :items="$items->items()" :filter="$obj->settings['filter']">
@foreach($items as $item)
{{ $item->title }}
@endforeach
</x:wg.items.listing>
The Blade Component gets a collection of:items
and a:filter
.
Between the tags should be a filtered collection available.
Listing.php:
class Listing extends Component
{
public $items;
public $filter;
/**
* Create a new component instance.
*
* @return void
*/
public function __construct($filter="0", $items="0")
{
$this->filter = $filter;
$this->items = $items;
}
/**
* Get the view / contents that represent the component.
*
* @return \Illuminate\View\View|string
*/
public function render()
{
$this->items = app()->make("App\\Http\\Controllers\\Frontend\\BlogController")->generateList($this->items, $this->filter);
/*return view('components.wg.items.list');*/
}
}
TheListing.php
should not render a view, because so is the items collection only available incomponents.wg.items.list
and not between the Blade x tags above.
Is that even possible? Thank you in advance!
Answer
Solution:
You can't do such a thing; Every variable you declare in the component is only available in the component itself.
... Unless you want to do it the reeaally dirty way. Then you can use global variables:
<x:wg.items.listing :items="$items->items()" :filter="$obj->settings['filter']">
@php
global $filtered_items;
@endphp
@foreach($filtered_items as $item)
{{ $item->title }}
@endforeach
</x:wg.items.listing>
public function render()
{
global $filtered_items;
$filtered_items = app()->make("App\\Http\\Controllers\\Frontend\\BlogController")->generateList($this->items, $this->filter);
return "{{ $slot }}";
}
P.S: And btw, you should not put your filtering logic inside your controller. The model should be responsible for it.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: fastcgi sent in stderr: "primary script unknown" while reading response header from upstream
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.