I am trying to grab collections form many to many relationships in my application.
To explain very quickly: Lets say I have categories that themselves have dedicated hashtags. Those hashtags again have photos.
Now, I want to grab one category and its dedicated hashtags. But I also want to grab all photos that are associated with those hashtags.
So here is what I have tried but still, this does not work:
$all_related_hashtags = $categorie->hashtags()->get();
$photos = foreach ($all_related_hashtags as $hashtag) {
$hashtag->photos;
}
Is there any way to fix this?
Any help would be awesome.
Thanks.
EDIT
#foreach ($all_related_hashtags as $hashtag)
#foreach(array_chunk($hashtag->photos, 4) as $row)
<div class="row">
#foreach($row as $photo)
SECOND EDIT
#foreach ($all_related_hashtags as $hashtag)
#foreach ($hashtag->photos as $photo)
{{ $photo->title }}
#endforeach
#endforeach
Try this instead
$all_related_hashtags = $categorie->hashtags()->get();
$photos = array();
foreach ($all_related_hashtags as $hashtag) {
$photos[] = $hashtag->photos;
}
If you just want to display in your view the photos for each hashtag you can actually call the photos in your view using nested loop such as:
foreach ($all_related_hashtags as $hashtag) {
foreach ($hashtag->photos->take($limit) as $photo){
echo $photo->url
}
}
Try this:
$all_related_hashtags = $categorie->hashtags()->get();
$categorie_photos = array();
foreach ($all_related_hashtags as $hashtag) {
$current_photos = array();
foreach ( $hashtag->photos as $photo ) {
$current_photos[] = $photo;
}
array_merge($categorie_photos, $current_photos);
}
Related
Using the php SDK, I would like to get all the content for a landing page, this will include sections with different content types / components with different fields per entry.
eg.
Section 1 - hero
Section 2 - article
Section 3 - image
Q. How do I loop through and get the linked section content.
If an entry contains a link to an asset or another
entry, the SDK will automatically load it.
I am struggling to understand the documentation. How exactly do I break into these values?
<?php
$query = new \Contentful\Delivery\Query();
$query->setContentType('page_landing')
->where("fields.urlHandle[match]", $slug);
$products = $client->getEntries($query);
foreach ($products as $product) {
echo $product['name'];
// Example of what I am trying to achieve
if($product['sections']['id']=='hero'){
$title= $product['sections']['hero']['title'];
}
if($product['sections']['id']=='article'){
$text = $product['sections']['article']['text'];
}
}
?>
I would love to know if there is a better way, the documentation does not have many examples. Below is where I ended up but feel there must be a simpler way.
<?php
require_once 'vendor/autoload.php';
require_once 'vendor/credentials.php';
// if we need to use rich text
$renderer = new \Contentful\RichText\Renderer();
// Get the section entry id's
foreach ($products as $product) {
$dataJson = json_encode($product);
$revertJson[] = json_decode($dataJson, true);
foreach ($revertJson as $Json) {
foreach ($Json['fields']['sections'] as $entries) {
$entryID[] = $entries['sys']['id'];
}
}
}
// Loop out the sections
foreach ($entryID as $entry) {
// Get individual linked entries
$entry = $client->getEntry($entry);
$type = $entry->getSystemProperties()->getcontentType();
$json = json_encode($type);
$comp = json_decode($json, true);
if ($comp['sys']['id'] == 'Hero') {
// field
echo $entry['urlHandle'];
// rich text
echo $renderer->render($entry['text']);
// asset
$asset = $client->getAsset($entry['imageId']);
echo $asset->getFile()->getUrl();
} elseif ($comp['sys']['id'] == 'Landmark') {
echo $entry['title'];
}
}
I have written a code to view data but I'm having some problems.
From the code, I wrote it's viewing the data from one table which is the $details. But I also wanna view the data from the $detailsAns.
How can I do that? also, can I foreach two data from that two tables?
my code below:
public function queries($companyID, $entityType, $entityValue)
{
$data = [];
$details = DiraQuestion::where('company_id', $companyID)->where('eType', $entityType)->where('eVal', $entityValue)->get();
$detailsAns = DiraResponses::where('company_id', $companyID)->where('eType', $entityType)->where('eVal', $entityValue)->get();
foreach($details as $key => $detailsValue)
{
if(!array_key_exists($detailsValue->intent, $data))
{
$data[$detailsValue->intent] = [];
}
if(!array_key_exists($detailsValue->reply, $data[$detailsValue->intent]))
{
$data[$detailsValue->intent]['answer'][$detailsValue->reply] = $detailsValue->id;
}
if(!array_key_exists($detailsValue->queries, $data[$detailsValue->intent]))
{
$data[$detailsValue->intent]['question'][$detailsValue->queries] = $detailsValue->id;
}
}
ksort($data);
return view('AltHr.Chatbot.queries', compact('data','entityType','entityValue','companyID'));
}
so as you can see I can return the data from foreach for $details but now I want to return data for $detailsAns also. how can I do that?
my database structure:
table1:
table2:
so to get the data it first has to select the company id, eType, eVal, and intent so from that now i need to display the reply and queries.
The output that i want is as below:
So as you can see i can output the questions table in the foreach that i did. and if i change the foreach to $detailsAns then i display the reply.. but i want to view the both now
It isn't very clear from the data provided how the tables relate to each other. If the answer has the same id value as the question, it is easy to relate them by structuring the arrays so that they are keyed by the common value.
$data = [];
$details = DiraQuestion::where('company_id', $companyID)->where('eType', $entityType)->where('eVal', $entityValue)->get();
foreach ($details AS $datum) {
if (!isset($data[$datum->intent])) $data[$datum->intent] = ['question' => [], 'answer' => []];
$data[$datum->intent]['question'][$datum->id] = $datum->queries;
}
$detailsAns = DiraResponses::where('company_id', $companyID)->where('eType', $entityType)->where('eVal', $entityValue)->get();
foreach ($detailsAns AS $datum) {
if (!isset($data[$datum->intent])) $data[$datum->intent] = ['question' => [], 'answer' => []];
$data[$datum->intent]['answer'][$datum->id] = $datum->reply;
}
At this point, since you said that there is no relation between the data sets, just pass the data array to the view the way you already are:
return view('AltHr.Chatbot.queries', compact('data','entityType','entityValue','companyID'));
And loop through both elements of data, printing out values in the view:
#foreach ($data['approval-document']['question'] as $id=>$question)
<p>Question #{{ $id }}: {{ $question }}</p>
#endforeach
#foreach ($data['approval-document']['answer'] as $id=>$answer)
<p>Answer #{{ $id }}: {{ $answer }}</p>
#endforeach
I am trying to get all data i need in one variable
--Subject
---Chapters related to subject
----Topics related to chapter
And this is what i have done!
$subjects = Subject::all();
$chapters = Chapter::all();
$topics = Topic::all();
foreach ($subjects as &$subject)
{
$i = 0;
$subject->related_chapters = array();
$chapters_reltn = array();
foreach ($chapters as $chapter)
{
if ($chapter->subject_id == $subject->id)
{
$chapters_reltn[$i]['id'] = $chapter->id;
$chapters_reltn[$i]['name'] = $chapter->name;
$j = 0;
foreach ($topics as $topic)
{
if ($topic->chapter_id == $chapter->id)
{
$chapters_reltn[$i]['related_topics'][$j]['id'] = $topic->id;
$chapters_reltn[$i]['related_topics'][$j]['name'] = $topic->name;
$j++;
}
}
$i++;
}
};
$subject->related_chapters = $chapters_reltn;
}
When i dd() in laravel, i see all the data arranged in structure i wanted.
The problem comes when accessing a specific data like so,
#foreach($subjects as $subject)
{{ $subject->name }}
{{ $subject->related_chapters[0]['name'] }}
#endforeach
i get an error:
Undefined offset: 0
Is there a better way of structuring my array, and getting data correctly. Please help!
Undefined offset is an notice that comes when you try to access an array which does not exist . Make sure that a value exists in that index or you can do something like this just before accessing the value
If (isset ( $subject->related_chapters[0]['name']))
I have the code below. $related is array result of db query and contains data.
Queried data consist of posts belonging to different groups (post_type). I want to group the queried objects within their post_type. The code below works BUT... what I want is create different ULs for each post_type. As it is below, there will be a UL element foreach post found in the query. So UL should stay out of foreach, but on the other hand how can i get post_type out of foreach? I am a bit lost here :(
$related = p2p_type( 'accommmodation-to-places' )->get_connected( get_queried_object_id() );
foreach($related->posts as $post) {
echo '<ul class="related-'.$post->post_type.'">'; // this shouldn't be here
if($post->post_type == 'hotels') {
echo '<li>'.get_the_post_thumbnail($post->id, '48').$post->post_title.'</li>';
}
echo '</ul>'; // this shouldn't be here
}
wp_reset_query();
So first, group the posts by their type:
$groups = array();
foreach ($related->posts as $post) {
$groups[$post->post_type][] = $post;
}
Then loop through the groups and output the lists:
foreach ($groups as $type => $posts) {
printf('<ul class="%s">', htmlspecialchars($type));
foreach ($posts as $post) {
printf('<li>...</li>', ...);
}
echo '</ul>';
}
I wish to display the passions for all the user that was displayed in the search results. Currently, this isnt dynamic, how do I make this dynamic? As I have to include the [0],[1],[2].. manually. I am using cakephp. The following code is located in my view.ctp page.
foreach ($data as $user) {
$cell .= $user['Passion'][0]['tag'].' '.$user['Passion'][1]['tag'].' '.$user['Passion'][2]['tag'];
}
what about a double foreach loop?
foreach ($data as $user) {
foreach ($user['Passion'] as $passion) {
$cell.= $passion['tag'];
}
}
The following code iterates through every tag, and also puts a space between each tag.
foreach ($data as $user) {
$passions = array(); //Reset the line
foreach ($user['Passion'] as $passion) {
array_push($passions, $passion['tag']); // Add each tag
}
$cell.= implode($passions, ' '); // Put a space between each tag
}