How to display image from an RSS Feed description using PHP Laravel - php

Im working in PHP Laravel to build an RSS FEED READER
I have read the XML file and it displays the title, description and link.
But the description contains image which i don't know how to display as an image.
Now the image part comes in html format as shown below:
Instead i want it to be displayed like this:
Here is my html code where the rss feed content display happens:
#foreach ($feed->item as $item)
<tbody>
<div class="card">
<div class="card-header">
{{ $item->title}}
</div>
<div class="card-header">
{{ $item->thumbnail}}
</div>
<div class="card-body">
{{$item->description}}
</div>
</div>
</tbody>
#endforeach
Can somebody please help me with this.
Thanks in advance.

You have HTML in your $item->description using the double Mustache{{ }} You are telling Laravel escape the data.
To render the HTML, you need to use instead {!! !!}
<div class="card-body">
{!! $item->description !!}
</div>
You can see more in the doc here: https://laravel.com/docs/7.x/blade#displaying-data

Related

Add a block field from a specific article

I have an article, each article has article_blocks, the blocks have a video_link field
And I need to display this field in the list of articles
This is how the article list output is roughly implemented
<div class="blog-list">
#foreach($articles as $article)
<div class="blog-article">
<div class="video-button video-modal-button-blog" data-video="https://www.youtube.com/embed/{{ $article_block->video_link }}">
<span>Watch video</span>
</div>
<h2 class="blog-article__title">{{ $article->title }}</h2>
<span>{{ date('d F Y', strtotime($article->published_at)) }}</span>
<span>{{ $article->getTotalViews() }} Views</span>
</div>
#endforeach
</div>
Each article has a button to open a video, but this video field itself is not in the article itself, but in the article blocks
I now take this field from the block and output it, it turns out like this
<?php
use App\Models\ArticleBlock;
$article_block = ArticleBlock::whereNotNull('video_link')->first();
?>
<div class="blog-list">
#foreach($articles as $article)
<div class="blog-article">
#if ($article_block->video_link !== 'null')
<div class="video-button video-modal-button-blog" data-video="https://www.youtube.com/embed/{{ $article_block->video_link }}">
<span>Watch video</span>
</div>
#endif
<h2 class="blog-article__title">{{ $article->title }}</h2>
<span>{{ date('d F Y', strtotime($article->published_at)) }}</span>
<span>{{ $article->getTotalViews() }} Views</span>
</div>
#endforeach
</div>
As a result, I get this field with video and it is displayed, but the same video is displayed for all articles in the list, and each article should have its own field and its own video. How can this be fixed?
I probably need to look for something like blocks by article id
$article_block = ArticleBlock::where('article_id', $article->id)->whereNotNull('video_link')->first();
But I can't get id
Cant write comment, under 25 rep :(
data-video="https://www.youtube.com/embed/{{ $article_block->video_link }}"
This looks wrong. You should eager load article blocks (eager loading is generally faster than lazy loading) for all articles with a with statement in your eloquent query see: https://laravel.com/docs/8.x/eloquent-relationships#eager-loading
Then if everything is correct you should be able to do just {{ $article->block->video_link }} or something similar.
Above is right if article has only one article block (one to one relationship).
But it seems that article has multiple blocks (one to many relationship) so you would need to iterate over each of the article block as well with another #foreach statement.
#foreach($articles as $article)
#foreach($article->article_blocks as $block)
<p>URL of video is: https://www.youtube.com/embed/{{ $block->video_link }}</p>
#endforeach
#endforeach
Now lets answer your question why video is same everywhere? Because you've coded it as so. You should NEVER write custom queries in your blade files.
Here i am referring to this part of your code:
<?php
use App\Models\ArticleBlock;
$article_block = ArticleBlock::whereNotNull('video_link')->first();
?>
Queries should be either in controllers/services or repositories.
$article_block = ArticleBlock::whereNotNull('video_link')->first();
will just give you first article block defined in ur database where video_link is not null. It won't guarantee that ArticleBlock belongs to given Article.
Lets hope that i put you on the right path ^.^

print icon obtained from db - laravel

I want to put together a menu and get an icon dynamically.
the result i want is something like this:
Database table:
code:
<div class="card card-dashboard-eight">
<label class="main-content-label mb-1">Categoryes</label>
<span class="d-block mg-b-20 text-muted">Stores All</span>
<div class="list-group">
#foreach($tipos as $tipo)
<div class="list-group-item">
{{$tipo->icono }}
<p>{{$tipo->tipo }}</p>
</div>
#endforeach
</div>
</div>
but it prints the label not the icon
What should I do to show my icons?
I dont know the template engine you are using, but it seems to escape the html chars. maybe it just works by removing the double brackets ("{$tipo->icono}" instead of "{{$tipo->icono }}").
But for app-design reasons: i'd suggest to store just the type (or class) in the database. not an html-tag. meaning: the column "icono" should just contain f.e. "glass-martini" - instead of a complete html-tag. put the html-stuff in your template and add the class from your column there.
You have to use {!! !!} to display the raw unescaped HTML of your icon:
{!! $tipo->icono !!}
This way it doesn't get escaped with htmlspecialchars.
Basically {{ $var }} will be compiled to <?php htmlspecialchars($var); ?> and {!! $var !!} to <?php echo $var; ?>.
See the docs on how to display data in Blade templates for more information.

Show textarea content with CKEditor in laravel

I use CKEditor in my application for edit text
when i store my text in data base it will save with tags like this :
<p>Hello world</p><p>how are you ?</p>
and in my blade when i want to display this text, it will show with tags so i use strip_tags :
<div id="detail_textarea">
{{ strip_tags($letter->text) }}
</div>
but when i look at the page, styles does not work and all the text are sticky like this :
Hello worldhow are you ?
in this case seems to <br> or <p> does not work correctly.
what can i do ?
Please use {!! !!} instead of {{ }}
Update
<div id="detail_textarea">
{!! $letter->text !!}
</div>

Pagination Link

What is the correct way to call the "Links" function after this "Foreach"?
I don't know how to handle the variable to put in function.
#inject('usuarios', 'App\User')
#foreach($usuarios->getIndicados() as $user)
#endforeach
<div class="row">
<div class="col-12 text-center">
{{ $usuarios->getIndicados()->links() }}
</div>
</div>
Maybe it's just an editing error, but in your output the -tags don't seem to be closed again. Also, there should be no space like < a> at the beginning of the tag. And < a hr_ef= ... is obviously wrong.
In order to style them, you can add a class attribute to the tags while building the string and do the style-stuff in css.
This is what laravel document provides. You need to add links in the collection.
<div class="container">
#foreach ($users as $user)
{{ $user->name }}
#endforeach
{{ $users->links() }}

How can I display the data stored by summernote on laravel?

I implement summernote (https://github.com/summernote/summernote) on the laravel 5.3
If I input data then save, on table seems like this :
<p>1. chelsea</p><p>2. mu</p><p>3. city</p><p>4. liverpool</p><p>5. arsenal</p>
How can I display it without tag p?
So when it displayed will seems like this :
1. chelsea
2. mu
3. city
4. liverpool
5. arsenal
How can I do it?
First edit the text accordingly to your desired format inside summernote text editor, and save it.
Then you can use this blade syntax to show the text formated:
{!! message !!}
Simplest way to show:
<?php echo $variable or html code ; ?>
because in PHP, echo is used to interpret html code.
$text = "<p>1. chelsea</p><p>2. mu</p><p>3. city</p><p>4. liverpool</p><p>5. arsenal</p>";
echo strip_tags($text);
Use strip_tags php method to convert html to string.
Refernce link
#foreach ($GuideCont as $item )
<input type="text" value="{{$item->id}}" hidden>
<div class="card">
<div class="card-header border-bottom border-info">
<h3 class="card-title">{{$item->title }}</h3>
</div>
<div class="card-body">
{!!$item->description !!} {{--// SummerNote Data --}}
</div>
</div>
#endforeach

Categories