Display RSS feed with Laravel PHP using SimpleXML - php

I want to display a RSS feed, keeping it as simple as possible.
I am using Laravel 4.
Here is my controller :
public function getHome()
{
$content = file_get_contents('http://rss.leparisien.fr/leparisien/rss/paris-75.xml');
$flux = new SimpleXmlElement($content);
return View::make('frontend/homepage', compact('flux'));
}
And here is my view :
#foreach ($flux as $flu)
<article class="entry-item">
<img src="{{utf8_decode((string)$flu->item->enclosure['url'])}}" alt="">
<div class="entry-content">
{{ $flu->item->title }}
{{ $flu->item->description }}
</div>
</article>
#endforeach
This is working fine, but only one article (item) is shown.
I tried to find a way to get several items displayed with SimpleXMLElement::attributes
I can't find any clear tutorial about it.

try this
#foreach ($flux[0]->item->link as $item)
<article class="entry-item">
<img src="{{utf8_decode((string)$item->enclosure['url'])}}" alt="">
<div class="entry-content">
{{ $item->title }}
{{ $item->description }}
</div>
</article>
#endforeach
because you have mutliple items

#foreach ($flux->channel->item as $flu)
<article class="entry-item">
<img src="{{utf8_decode((string)$flu->enclosure['url'])}}" alt="">
<div class="entry-content">
{{ $flu->title }}
{{ $flu->description }}
</div>
</article>
#endforeach

Related

I'm working on a project in Laravel and I'm facing an Undefined variable $post error in post-card.blade.php

post.blade.php
The main post file
<main class="max-w-6xl mx-auto mt-6 lg:mt-20 space-y-6">
<x-post-featured-card :post="$posts[0]" />
<div class="lg:grid lg:grid-cols-2">
#foreach ($posts->skip(1) as $post)
<x-post-card :post="$post" />
#endforeach
</div>
</main>
post-card.blade.php
This file is throwing the undefined variable $post error
<div class="mt-4">
<h1 class="text-3xl">
{{ $post->title }}
</h1>
</div>
#props(['post'])
#foreach($posts as $post)
<div class="mt-4">
<h1 class="text-3xl">
{{ $post->title }}
</h1>
</div>
#endforeach
You can try a few steps
change the name of the file of post-card.blade.php
remove the other commented component from the file post-card.blade.php if have any
use props in the file that give error, like #props(['post'])

laravel foreach and if else loop errors both in count and variables something

code of 1st error :
#extends('layouts.app')
#section('content')
<h1> posts</h1>
#if(count($posts)>1)
#foreach($posts as $post)
<div class="well">
<h3>{{$post->title}}</h3>
</div>
#endforeach
#else
<p> no posts found</p>
#endif
#endsection
and image also attached .
[enter image description here][1]
after i follow the instructions of the chrome this occurs and code will be .. even i have removed the # and try if() like this ..
code 2 ;
#extends('layouts.app')
#section('content')
<h1> posts</h1>
#if(count($posts ?? '')>1)
#foreach($posts ?? '' as $post)
<div class="well">
<h3>{{$post->title}}</h3>
</div>
#endforeach
#else
<p> no posts found</p>
#endif
#endsection
Error images:
https://i.stack.imgur.com/21weC.png
https://i.stack.imgur.com/RDdF2.png
A shorter approach for this would be:
#forelse($posts as $post)
<div class="well">
<h3>{{$post->title}}</h3>
</div>
#empty
<p> no posts found</p>
#endforelse
You can find more loops available in the documentation:
https://laravel.com/docs/7.x/blade#loops
Use forelse instead of foreach, that should do the job

How to check foreach loop for a property. Laravel

I have been trying to figure out how to filter my foreach loop and only display an image if my post has one. So far I have been trying different functions and #ifs but to no avail.
Here is my controller code:
<div class="container">
<div class="row">
<div class="col-sm-6">
#foreach($posts as $post)
<div>
<h1>{{$post->title}}</h1>
<p>{{$post->body}}</p>
<img src="{{url('img', $post->image)}}">
</div>
<hr>
#endforeach
</div>
</div>
</div>
You can use a simple #if:
#if ($post->image)
<img src="{{ url('img/' . $post->image) }}">
#endif
Or #isset:
#isset ($post->image)
<img src="{{ url('img/' . $post->image) }}">
#endisset
https://laravel.com/docs/5.5/blade#control-structures
You can use a simple #if statement to see if the image property of the current $post is set. This could look a little something like this:
#foreach($posts as $post)
<div>
<h1>{{$post->title}}</h1>
<p>{{$post->body}}</p>
#if(!empty($post->image))
<img src="{{url('img', $post->image)}}">
#endif
</div>
<hr>
#endforeach
By including this, the <img> element will only be displayed if the property is not empty.
As also mentioned in another answer, you can replace the #if(!empty(...)) by #isset(...) to achieve the same result.

Laravel Commenting System Only Working on most recent Post

I recently created a commenting system for a blog. When there is one post in the blog, the commenting sytem works fine. However, when there are two posts on the blog, and I comment on the FIRST post, the comment shows up on the SECOND post. Similarily, if I add another post, so there are three posts in my blog, when I comment on the first post, the comment shows up under the third post. In the same situation, when I comment on the second post, the comment shows up under the third post. Here is my code:
/index.blade.php
#section('content')
<div class="col-md-8 col-md-offset-2">
<hr>
#foreach($posts as $post)
<h3>{{ $post->title}}</h3>
<p>by {{ $post->user->name }}</p>
<img src='{{ asset($post->image_path) }}' class="img-responsive" id="blogpic"/>
<p>{{ Str::words($post->body) }}</p>
<p>Read More...</p>
<hr>
{{ Form::open(array('action' => 'CommentController#newComment')) }}
{{ Form::hidden('post_id', $post->id) }}
<div class="row">
<div class="col-md-10">
{{ Form::text('body', null, array('class'=>'form-control')) }}
</div>
<div class="col-md-2">
{{ Form::submit('Post', array('class'=>'btn btn-info')) }}
</div>
</div>
<br>
#foreach($post->comments as $comment)
<div class="comment">
<p><span class="bold">{{ $comment->user->name }}</span>: {{ $comment->body }}</p>
</div>
#endforeach
#endforeach
</div>
#stop
Here is my new comment controller:
public function newComment()
{
$comment = New Comment();
$comment->user_id = Auth::user()->id;
$comment->post_id = $post_id = Input::get('post_id');
$comment->body = Input::get('body');
Post::find($post_id)->comments()->save($comment);
return Redirect::action('BlogController#index');
}
I am pretty sure the problem is in my index file, and I can provide more code if needed. My comment only seems to get the most recent id in this line
{{ Form::hidden('post_id', $post->id) }}
What could be going wrong?
Thanks in advance

Blog posts from category isn't displayed

I've created a page and then put this code
<section class="cont_pad">
<div class="container_12">
<article class="grid_8">
{{ blog:posts limit="5" offset="5" category="adultos" }}
<section class="post">
{{ if imagen_portada }}
<div class="postimg"><img src="{{ url:site }}files/thumb/{{ imagen_portada.id }}/610/220" class="pic2" alt="{{title}}" title="{{title}}"/></div>
{{ endif }}
<div class="entry-date">
<div class="posttime">
<h3>{{ helper:date timestamp=created_on }}</h3>
</div>
<div class="entry-utility">
{{ asset:image file="blog/icon1.png" }} {{ user:display_name user_id=author_id }}
<br/>
{{ if category }}
<span>{{ asset:image file="blog/icon2.png" }} {{ category:title }}</span>
{{ endif }}
{{ if keywords }}
<span>{{ asset:image file="blog/icon2.png" }} {{ keyword }}</span>
{{ endif }}
</div>
</div>
<div class="entry-text">
<h3 class="posttitle">{{ title }}</h3>
<div class="entry-content">
{{ intro }}
<p>{{ helper:lang line="blog:read_more_label" }}</p>
</div>
</div>
</section>
{{ /blog:posts }}
{{ pagination }}
</article>
<article class="grid_4 last-col">
<aside id="sidebar">
{{ widgets:area slug="widgets_blog_adultos" }}
</aside>
</article>
</div>
</section>
inside to show posts from category "adultos" but I'm not getting nothing. I have one post and it's LIVE in this category. What is wrong? I read Blog Plugin Docs also check the Blog Plugin Code at package "PyroCMS\Core\Modules\Blog\Plugins" and can't find where it fails. Also and related to this same question, can I paginate trough all the posts? I need some help here because I can't find what is wrong
Is the timezone on the computer set up wrong? If PyroCMS thinks the "Publish Date" is in the future it wont show it on the frontend.

Categories