Laravel 5.6 how to show few pictures in one article - laravel

Good afternoon,
I am working on the blog where I have detail of the article and want to show few pictures.
What I am getting at the moment:
As you can see I am getting the content and after the content pictures. Here is also the code of the blade.
<div class="container">
#foreach($articles as $article)
<article>
<h1 class="title is-1">{{$article->title}}</h1>
#foreach($article->images as $image)
<figure class="image is-128x128">
<img src="{{$image->path}}" alt="{{$image->title}}">
</figure>
#endforeach
<p>{{$article->content}}</p>
</article>
#endforeach
</div>
Because I am getting a object I cannot split the pictures.
.

It was rather hard to figure out what you want, but I'm quite sure I finally got the question.
What you wanna do is something like this:
<div class="container">
#foreach($articles as $article)
<article>
<h1 class="title is-1">{{$article->title}}</h1>
#if(count($article->images) > 0)
<figure class="image is-128x128">
<img src="{{$article->images->first()->path}}" alt="{{$article->images->first()->title}}">
</figure>
#endif
<p>{{$article->content}}</p>
#if(count($article->images) > 1)
#foreach($article->images->slice(1) as $image)
<figure class="image is-128x128">
<img src="{{$image->path}}" alt="{{$image->title}}">
</figure>
#endforeach
#endif
</article>
#endforeach
</div>
This will not only give you headline > image 1 > content > remaining images, it will also make sure you only print images when there are actually some available. The $article->images->slice(1) within the second #if() will ensure we are not using the first image a second time.

Make sure $image->path is the correct to the image file (if it's an url, skip this point).
You can check by echoing it or just dd($image->path).
For the image to be accessed, it needs to be on the public folder of laravel
If you wish to have the files in the storage folder and have it accessed publicly, you'll need to create a symlink
https://laravel.com/docs/5.6/filesystem

Related

Viewing all photos of post using UIKIT using Lightbox Panel Options "items"

I am using UIKIT Lightbox to display post images like Facebook as below:
Controller
$postImages = postsImages::where('postId', $post->postId)->limit(3)->get();
Blade
<div class="uk-child-width-1-3#m" uk-grid uk-lightbox="animation: slide">
#foreach ($postImages as $postImage)
<div>
<a class="uk-inline" href="{{$postImage->imageLink}}">
<img src="{{$postImage->imageLink}}">
#if ($loop->last && $post->hasImage > 3)
<div class="uk-overlay-default uk-position-cover more">
<div class="uk-position-center">
<h1>+{{$post->hasImage - 3}}</h1>
</div>
</div>
#endif
</a>
</div>
#endforeach
</div>
I limited the image thumbnails to 3 and the rest will be shown as +number_of_remaining_photos same like Facebook.
The problem is that when I click on a thumbnail to make it large and view the rest then I can see only those three images which I limited.
I went through UIKIT lighbox documentation and I found items option but I don't know how to use it in order to view all the images of that post by creating another query like below:
$postImages = postsImages::where('postId', $post->postId)->get();
to get all images there and showing it in the lightbox.
P.S. I am not sure, that is item option for that purpose or not. )
This is a practical solution.
1- Make another query without limit.
$postImagesAll = postsImages::where('postId', $post->postId)->get();
2- Add data-uk-lightbox="{group:'my-group'}" attribute to the result of both queries like below and everything will work fine:
<div class="uk-grid-collapse" uk-grid uk-lightbox="animation: slide;">
#foreach ($postImages as $postImage)
<div>
<a class="uk-inline" href='/storage/posts/{{$postImage->imageLink}}' data-uk-lightbox="{group:'my-group'}">
<img src="{{$postImage->imageLink}}"/>
#if ($loop->last && $post->hasImage > 3)
<div class="uk-overlay-default uk-position-cover more">
<div class="uk-position-center">
<h1>+{{$post->hasImage - 3}}</h1>
</div>
</div>
#endif
</div>
</a>
</div>
#endforeach
#foreach ($postImagesAll as $postImage)
<a class="uk-hidden" href="{{$postImage->imageLink}}" data-uk-lightbox="{group:'my-group'}"></a>
#endforeach
</div>

Views and downloads count - Update database on view with lightbox and download

I've made a site with laravel and have a page where there are pictures that can be seen in hi-resolution via fancybox or downloaded. In the database i have counters for each action. and I do already have GET routes set up for the counting since they are used in other situations too.
for example: www.mysiste.com/somecode/someothercode/image/viewed
the simplified structure of each image is this.. This is called inside a for each loop.. i removed all styling and classes for better reading...
<div >
<div class="card" style="background-color: lightgray">
<div >
<a data-fancybox="gallery" href="/images/......../{{$photo->filename}}">
<img class="card-img" src="/images/......../thumb/thumb_{{$photo->filename}}">
</a>
</div>
<div class="card-footer">
<div>
<div>
<a download="{{$photo->filename}}" href="/images/......../{{$photo->filename}}">
<span>Download (Hi-Res)</span>
</a>
</div>
<div>
<a download="social_{{$photo->filename}}" href="/images/......../social_{{$photo->filename}}">
<span>Download (Social-Res)</span>
</a>
</div>
</div>
</div>
</div>
</div>
I would need that when they hit the download button it calls a certain URL in the background for upcounting.. (most important)
If possible I would like to hit another URL when the images are viewed fullscreen through fancybox.
how can this be done?
You can do something in the lines of:
<a data-fancybox="gallery" href="{{ route('photo.fancybox', ['photo' => $photo])}}">
<img class="card-img" src="/images/......../thumb/thumb_{{$photo->filename}}">
</a>
...
<a href="{{ route('photo.download', ['photo' => $photo])}}">
<span>Download (Hi-Res)</span>
</a>
PhotoController
public function download(Photo $photo){
$photo->downloadCount->increment();
return redirect('/images/......../'. $photo->filename);
}
public function fancybox(Photo $photo){
$photo->fancyboxCount->increment();
return redirect('/images/......../social_'. $photo->filename);
}
And make sure to register your new routes accordingly:
Route::get('photo/download', 'PhotoController#download')->name('photo.download');
Route::get('photo/fancybox', 'PhotoController#fancybox')->name('photo.fancybox');

Display image of storage in Laravel

Any ideas on how can I show the image from storage. I can't get the format right. I notice that using post.media in image source makes the whole record dispear in inspection. But once i remove it or place a random image url from the internet, post.media echos the imagine file name on h3
<div v-for="post in posts" v-bind:key="post.id" class="py-3">
<div class="card">
<img src="{{ Storage::url('public/uploads/{{ post.media }})' }}" class="card-img-top">
<div class="card-body">
<h3>{{ post.media }}</h3>
<p>{{ post.description }}</p>
</div>
</div>
</div>
So you are using vue, I dont think it will recognize Storage::url(). First, create a symbolic link from storage to public. php artisan storage:link
Second, create a computed property link.
computed: {
link(){
return '/uploads/' + this.post.media;
}
}
And simply,
<img src="post.link">

unable to retrieve images from storage folder - path from database

I'm using Laravel 5.7 and I wish to upload images and place the path into database, it's easy and I did it, all is cool. now trying to retrieve the image, the path is correct and I tried to change the settings of the folders and the storage, I tried to find every single solution out there for 7 hours and still i can't find the issue, please advise.
I tried to change the storage from local to public and tried to use different methods to recall the image but all failed.
<div class="card">
<img class="card-img-top" src="{{ asset('storage/'.$post->background_image) }}">
<img class="card-img-top" src="/storage//{{$post['first_image']}}" alt="Card image cap">
<div class="card-body">
<h4 class="card-title">{{$post['post_name']}}</h4>
<p class="card-text">{{$post['description']}}</p>
</div>
<ul class="list-group list-group-flush">
<li class="list-group-item">Item 1</li>
<li class="list-group-item">Item 2</li>
<li class="list-group-item">Item 3</li>
</ul>
</div>
//as for saving the file i tried the below options:
$path1 = $request->file('background_image')->store('background_image');
if ($validatedImages) {
$fileNamewithExt= $request->file('background_image')
->getClientOriginalName();
$filename= pathinfo($fileNamewithExt, PATHINFO_FILENAME);
$extension = $request ->file('background_image')->getClientOriginalExtension();
$fileNameToStore = $filename.'_'.time().'.'.$extension;
$path1 = $request->file('background_image')
->storeAs('/background_image',$fileNameToStore);
}
I expect the image to load instead of appearing unavailable, the path loads from the database, but for some reason the image doesn't load.

Laravel included template is not parsing / running foreach loop

I have included a couple of files.
my home.blade.php has the following:
#include('includes.header')
my header.blade.php in includes folder has the following
#include('elements.pendingTasksLi', array('tasks'=>$tasks))
and my elements/pendingTasksLi.php file has the following
#foreach ($tasks as $task)
<li>
<a class="todo-actions" href="javascript:void(0)">
<i class="fa fa-square-o"></i>
<span class="desc" style="opacity: 1; text-decoration: none;">{{{$task\['title'\]}}}</span>
<span class="label label-danger" style="opacity: 1;"> {{{$task\['deadLineTime'\]}}}</span>
</a>
</li>
#endforeach
This code in the pendingTakstsLi file is never executed as a loop but only as a simple text.
This is what the output looks like...
#foreach ($tasks as $task)
{{{$task['title']}}} {{{$task['deadLineTime']}}}
#endforeach
I am not sure what to do... Please advise...
Check a screen here
http://i.stack.imgur.com/8emZk.png
Thanks
Jyot
You mentioned that your "pendingTasksLi" view ends in ".php" and not ".blade.php" which would prevent it from being parsed as a blade view.
I had the same problem and as mentioned above you forgot the .blade before the .php
If you want to use expressions like this: {{ $variable}} and want them to be parsed you always have to add .blade in the name pefore the .php extension of the file.

Resources