Sending data from function to view template? - laravel

I have a blade template that looks something like this:
<div class="comment">
<div class="username"></div>
<div class="message"></div>
<div class="children">
</div>
</div>
I need to be able to call this view from a function and insert data into it.
I have a class, Helpers.php, with a recursive function that looks like this:
function getComments(array $comments)
{
foreach ($comments as $comment)
{
echo $comment;
if (!empty($comment->children))
{
getComments($comment->children);
}
}
}
What this is meant to do is print out a comment, and then check if that comment has any children comments and recurse if it does.
How can I modify my view/function so that I can send data to the view, such that I end up with something like this:
<div class="comment">
<div class="username">Username1</div>
<div class="message">Hello, world!</div>
<div class="children">
<div class="comment">
<div class="username">Username1</div>
<div class="message">Hello, world!</div>
<div class="children">
<div class="comment">
<div class="username">Username1</div>
<div class="message">Hello, world!</div>
<div class="children">
</div>
</div>
</div>
</div>
</div>
</div>
<div class="comment">
<div class="username">Username1</div>
<div class="message">Hello, world!</div>
<div class="children">
</div>
</div>
Thanks!

You don't have to use a function in this particular case.
To achieve your goal, create a view comments.blade.php. Depending on your Laravel version and setup you can put it into app/views or resources/views directory.
The content of this file has to be like follows:
#foreach ($comments as $comment)
<div class="comment">
<div class="username">{{ $comment->username }}</div>
<div class="message">{{ $comment->message }}</div>
<div class="children">
#include('comments', ['comments' => $comment->children])
</div>
</div>
#endforeach
Then render this view and pass the same argument you were passing to your getComments() function.
This will hopefully give the desired result.
Good luck!

you can send the data to view from function you can use by three method compact,with and session. compact of with method while your are redirecting of loading any view you can use compact or ->with(array());

Related

Foreach loop returns all results

I have a column of album covers. I want to take me to the album details after click on the cover. Actually it takes me to the album details but it shows all results, not only this one which was clicked.
AlbumDetailsController
public function index(): View
{
return View("details.index", [
'albums' => Album::paginate(1)
]);
}
AlbumDetails Blade view
#extends('layouts.app')
#section('content')
<div class="container">
#foreach ($albums as $album)
<h1 class="my-4">{{ $album->name }}</h1>
<div class="row">
<div class="col-lg-6 mb-4">
<div class="card h-100">
<div class="card-body">
<h4 class="card-title">
</h4>
{{ $album->artist }}
</div>
</div>
</div>
<div class="col-lg-6 mb-4">
<div class="card h-100">
<img src="{{asset('storage/' . $album->image_path)}}" alt="" class="img-fluid card-img-top">
<div class="card-body">
<h4 class="card-title">
{{ $album->artist }}
</h4>
{{ $album->description }}
</div>
</div>
</div>
</div>
#endforeach
#endsection
I've tried to solve this in differents ways but everything is not working. I think problem is with my controller but I'm really don't know how to modify it to show only this item which was clicked.

Laravel Blade foreach combine

I have two separate divs, one where the images are loaded and the other where the data is displayed.
<div class="banner-slider-image">
<div class="swiper-container">
<div class="swiper-wrapper">
<!-- Slide Item -->
<div class="swiper-slide">
<div class="bg" style="background-image: url(include/assets/images/main-slider/1.jpg);">
</div>
</div>
</div>
</div>
</div>
<div class="banner-slider-content">
<div class="side-text">AVANT</div>
<div class="swiper-container banner-slider">
<div class="swiper-wrapper">
#if(count($listings) > 0)
#foreach($listings as $listing)
<!-- Slide Item -->
<div class="swiper-slide">
<div class="content-outer">
<div class="content-box">
<div class="inner">
<h5>{{$listing->first_name}} {{$listing->last_name}}</h5>
<h1><span>{{$listing->name}}</span> </h1>
<div class="text">{{$listing->property_description}}.</div>
<div class="link-box">View Listing</div>
</div>
</div>
</div>
</div>
#endforeach
#endif
</div>
</div>
</div>
So the first div will also be in a foreach loop, but I dont know how I can combine the two where they show the same related data.
Use 2 foreach loops . It is the easiest and desired solution for you. or you can define two variable and concat the contents using one forloop and then display them later like
#php
$first='';
$second='';
#endphp
#if(count($listings) > 0)
#foreach($listings as $listing)
#php
$first.='first div content';
$second.='second div content';
#endphp
#endforeach
#endif
<div class="banner-slider-image">
<div class="swiper-container">
<div class="swiper-wrapper">
{!!$first!!}
</div>
</div>
</div>
<div class="banner-slider-content">
<div class="side-text">AVANT</div>
<div class="swiper-container banner-slider">
<div class="swiper-wrapper">
{!!$second!!}
</div>
</div>
</div>
<html>
<body>
#section('sidebar')
This is the master sidebar.
#show
<div class="container">
#yield('content')
</div>
</body>
use for yield

How to get last child while parsing HTML with Goutte in Laravel

<div class="table">
<div class="table-head">
<div class="table-head-title">Ranking Equipos</div>
</div>
<div class="table-body">
<div class="table-body-row active">
<div class="col-key">Mark</div>
<div class="col-value">9233</div>
</div>
<div class="table-body-row">
<div class="col-key">Amanda</div>
<div class="col-value">7216</div>
</div>
<div class="table-body-row">
<div class="col-key">Mark</div>
<div class="col-value">6825</div>
</div>
<div class="table-body-row">
<div class="col-key">Paul</div>
<div class="col-value">6184</div>
</div>
<div class="table-body-row">
<div class="col-key">Amanda</div>
<div class="col-value">5866</div>
</div>
</div>
</div>
This is my HTML and I want to get last child of .table-body.
I tried to use JavaScript like logic and used indexing like this
$lastChild = $node->filter('.table-body .table-body-row')[4]; but it shows error. Cannot use object of type "Symfony\Component\DomCrawler\Crawler" as array
I was stuck in similar situation recently and I resolve this by using last() method. Syntax is here: $node->filter('.table-body .table-body-row')->last();

how to not display duplicated data?

i want to show the categorie list without showing duplicated ones
my controller code :
public function showActualite(){
$actualites=DB::table('actualites')->distinct('categorie')->orderBy('created_at', 'DESC')->paginate(6);
return view ('AllActualite',['actualites' => $actualites]);
}
My view :
#foreach ($actualites as $act)
<span style="text-transform: capitalize;" > {{$act->categorie}} </span>
<span style="text-transform: capitalize;"> </span>
#endforeach
</div>
<div class="row">
#foreach($actualites as $act)
<div class="col-lg-6">
<div class="box wow fadeInLeft">
<div class="icon"><i class="fa fa-newspaper-o"></i></div>
<h4 class="title">{{$act->categorie}}</h4>
<p class="description">{{$act->titre}}</p>
</div>
</div>
#endforeach
I believe your code should work, but in general i would not use DB logic. A more Laravel version of this would be. Latest is a syntax sugar for the same order by logic. See of this solves the problem.
$actualites = Actualites::query()->distinct('categorie')->latest()->paginate(6);
All your answer can be found in this post
$actualites= Actuality::distinct()->get(['categorie']);
Eloquent is used.

Xpath keeps selecting all objects of the given class instead of the first

This one has me stumped., I'm trying to select the first class = csb-quantity-listbox object of the below using the XPATH //select[#class='csb-quantity-listbox'][1], but instead of selecting the first quantity listbox it's selecting ALL the listboxes on the page with that class (see image below).
What am I doing wrong?
<div class="gwt-product-detail-products-container">
<div class="gwt-product-detail-products-header-column">
</div>
<div id="gwt-product-detail-widget-id-12766" class="gwt-product-detail-widget">
<div class="gwt-product-detail-widget-image-column ui-draggable" title="12766">
<div class="gwt-product-detail-widget-options-column">
</div>
<div class="gwt-product-detail-widget-price-column">
</div>
<div class="gwt-product-detail-widget-quantity-panel">
<select class="csb-quantity-listbox" name="quantity_12766"></select>
</div>
<div class="gwt-bundle-add-to-cart-btn">
</div>
</div>
</div>
<div id="gwt-product-detail-widget-id-10617" class="gwt-product-detail-widget">
<div class="gwt-product-detail-widget-image-column ui-draggable" title="10617">
<div class="gwt-product-detail-widget-options-column">
</div>
<div class="gwt-product-detail-widget-price-column">
</div>
<div class="gwt-product-detail-widget-quantity-panel">
<select class="csb-quantity-listbox" name="quantity_10617"></select>
</div>
<div class="gwt-bundle-add-to-cart-btn">
</div>
</div>
</div>
</div>
Image:
You just need to put brackets around the statement before the [1]
Like so:
(//select[#class='csb-quantity-listbox'])[1]

Resources