Laravel article image slideshow - php

I've got a Laravel project where you can create articles and so on. Of course I want to have an image slideshow with multiple images per article. I've managed to save multiple images in the database in one column via implode. PICTURE
How do I display the slideshow now? I have no idea.

The short answer is the one #Sang Nguyen posted: The opposite of implode() is explode().
However I am going to add a more "Laravel-like" way of doing it:
Assuming you have an Article model, add an accessor:
class Article extends Model {
...
public function getImagesListAttribute() {
return collect(explode('|', $this->images));
}
...
}
Then:
$article = Article::find(1);
var_dump($article->images_list); //would be a Collection of strings (which are your image names)
More about accessors: https://laravel.com/docs/5.6/eloquent-mutators#defining-an-accessor
More about collections: https://laravel.com/docs/5.6/collections

You just need to explode your data from database then show it
$images = explode('|', $post->image);
#foreach ($images as $image)
<img src="{{ url($image) }} alt="image" />
#endforeach
Then you can use OwlCarousel https://owlcarousel2.github.io/OwlCarousel2/ to create slideshow.
You can create a basic slider with this example https://owlcarousel2.github.io/OwlCarousel2/demos/basic.html
Example: In your blade view
<div class="owl-carousel owl-theme">
#foreach (explode('|', $post->image) as $image)
<div class="item"><img src="{{ url($image) }} alt="image" /></div>
#endforeach
</div>

Related

What is Laravel's similar method to PDO::FETCH_ASSOC?

I am updating my php PDO website using Laravel Framework 5.3. I am finding Laravel very easy to use but there are few functions that I don't know the syntax to achieve results.
I have a Laravel controller
public function index(){
$posts = Post::latest()->get();
return view('/home', compact('posts'));
}
which I use in the blade to fetch data using a foreach loop
#foreach ($posts as $row)
#endforeach
which is similar to fetchAll() method in PDO
$row = $posts->fetchAll();
now I want the logic to get fetch(PDO::FETCH_ASSOC) like result, so that I can assign
$row = $posts->fetch(PDO::FETCH_ASSOC);
I have to do this because I want to use for loop in the blade to show the data, if I use foreach loop then it creates duplication for iterated result. Following is the PDO code that I have to update with Laravel
for($i=0; $i<$count_photos; $i++){
$row_gallery = $get_photos->fetch(PDO::FETCH_ASSOC);
$pst_id = $row_gallery['post_id'];
$img = $row_gallery['img_name'];
echo '<div class="img_gallery"><div class="gallery'.$i.'"> <img class="img-responsive" src="uploads/'. $img . '"></div></div>';
}
The models will have already been fetched when you use get() so you'll be able to access the properties on the model straight away e.g.
#foreach ($posts as $post)
{{ $post->title }} //assuming you have a "title" column in the database
#endforeach
You might find this helpful: https://laracasts.com/series/laravel-from-scratch-2017/episodes/7
Edit
If you have the photos relationship set up in your Post model then you can eager load the relationship. Also, since you only want to load one post you can use first() instead of get() so you would end up with the following:
Controller:
public function index()
{
$post = Post::with('photos')->latest()->first();
return view('/home', compact('post'));
}
Blade file
#foreach($post->photos as $k => $photo)
<div class="img_gallery">
<div class="gallery{{ $k }}">
<a href="{{ url('uploads/'.$photo->img_name) }}" data-lightbox="{{ $post->id }}">
<img class="img-responsive" src="{{ url('uploads/'.$photo->img_name) }}">
</a>
</div>
</div>
#endforeach

how to loop a collection inside a collection in laravel?

i want to list all the images for each project, i tried to do it like this, but it always return 1 image only.
public function home()
{
$projects = Project::all(); //result : 1
foreach($projects as $project){
$images = $project->images()->get();
// dd($images); //result : 4
foreach($images as $image){
return '<img src="'.$image->image_path.'">';
}
}
}
the code above is just for test purpose, what i want to do is to display it in a table, something like this in my controller :
<tbody>
'
$images = $project->images()->get();
foreach($images as $image){
'
<tr>
<td><img src="'.$image->image_path.'"></td>
</tr>
'
}
'
</tbody>
Since a function can only return once, you probably need to do something different on the inner foreach like concatenating the image html to an existing string.
That said, the right way to do this would generally be to return $images to be used directly in a template where you would build out the html.
I'm not sure what all is going on here, but it looks like you want to do something that is relatively simple in Laravel if you are able to use Controllers and Blade Templates.
First, you start with your controller (keep all of your application logic there):
// ProjectController.php
public function home()
{
//-- grab all the projects, with all of their images
// Using with() will allow you to eager load those images
// from the database if they are Eloquent relationships.
$projects = Project::with('images')->get();
//-- send those $projects to your view
// Using with() here will send that variable to your
// view accessible by the key.
return view('projects')->with(['projects' => $projects]);
}
Then, in your blade template, you can easily iterate over the projects and images:
// views/projects.blade.php
#foreach($projects as $project)
<table>
...
<tbody>
#foreach($project->images as $image)
<tr>
<td><img src="{{ $image->image_path }}"></td>
</tr>
#endforeach
</tbody>
...
</table>
#endforeach
</tbody>
Hope this helps. If none of this makes sense, you can always check out the docs, they are really well written!
Laravel Docs
Be sure to check out the Controllers, Blade Templates and Eloquent ORM - Relationships sections :)

Issue in fetching data from db to the views in laravel 5.2

Im new to laravel and studying it by creating some test projects myself in laravel 5.2. But i got stuck now with some issue in fetching data correctly from db in laravel 5.2 and display the result. I have a menu table in my db with fields -> id, menutype, itemname, itemprice, itemimage with some datas in it. I want to display it on my webpage in a specific way like as seen on the below given screenshot.
See:
And this one is my db table screenshot with the values on it. See:
i added the below codes in my Controller (GuestController.php)
public function menu() {
$result=DB::table('menu')->select('menutype')->distinct()->get();
return view('guest.menu')->with('data',$result);
}
and in the View (menu.blade.php), i had given the below code:
<div class="row">
#foreach($data as $row)
<div class="col-1-3">
<div class="wrap-col">
<h3>{{$row->menutype}}</h3>
<?php
$item=DB::table('menu')->where('menutype', $row->menutype)->get();
?>
#foreach($item as $row)
<div class="post">
<img src="assets/images/{{$row->itemimage}}"/>
<div class="wrapper">
<h5>{{$row->itemname}}</h5>
<span>Rs.{{$row->itemprice}}/-</span>
</div>
</div>
#endforeach
</div>
</div>
#endforeach
</div>
This works perfectly and i am getting the desired output as seen on the products page screenshot given above. But i know this method is not correct, because i am giving the query statement on the View itself like as given below to fetch data and its against the MVC concept:
<?php $item=DB::table('menu')->where('menutype', $row->menutype)->get(); ?>
So is there any other simple and better way i can implement to get the above said desired output along with keeping the MVC Standards?? Please help! Thanks in advance...
Laravel's Collection can really help you out with this. Specifically, the groupBy method. First, you get all of the menu items with all the data. Then, you use the groupBy method on the Collection to group the menu items into separate arrays based on their menutype. You can then use this one Collection to do all the work in your view.
The code is shown below. You can combine a couple of the lines into one if you'd like, but it is broken out into multiple lines to show all the steps:
public function menu() {
// get all the menu items
$menuArray = DB::table('menu')->get();
// create a Laravel Collection for the items
$menuCollection = collect($menuArray);
// group the Collection on the menutype field
$groupedMenu = $menuCollection->groupBy('menutype');
/**
* Note that Eloquent queries (using Models) will automatically return
* Collections, so if you have your Menu model setup, your first two
* lines would just be:
* $menuCollection = Menu::get();
* or, all three lines could be combined into:
* $groupedMenu = Menu::get()->groupBy('menutype');
*/
// pass the grouped Collection to the view
return view('guest.menu')->with('data', $groupedMenu);
}
Now, in your view, your outer foreach will iterate through the groups. The inner foreach will iterate through the items in each group:
<div class="row">
#foreach($data as $type => $items)
<div class="col-1-3">
<div class="wrap-col">
<h3>{{$type}}</h3>
#foreach($items as $item)
<div class="post">
<img src="assets/images/{{$item->itemimage}}"/>
<div class="wrapper">
<h5>{{$item->itemname}}</h5>
<span>Rs.{{$item->itemprice}}/-</span>
</div>
</div>
#endforeach
</div>
</div>
#endforeach
</div>

Laravel ForEach Into Separate DIVs

Im working on a project that will display articles based on a user's interest. I have a user_table, follower_table, and article_table. Currently i am able to get the user_id and grab all their associated interests from the follower_table and each article produced under their interest. Im using Foreach to loop through all their articles. However, now i want to be able to put each article into a different div based on their type. In order to be able to have varying sizes and color. What is the best way to filter the foreach loop? Here is my code below.
follower_table is set up:
user_id:(this is the person/interest you follow)
follower_id: (this is your id);
article_table is set up:
id
user_id
type: (music,fashion,events, dance)
title
body
photo
The UserController:
class UserController extends Controller
{
public function index ()
{
$ilike = Auth::id();
$ifollow = DB::table('followers')->where('follower_id', $ilike)->lists('user_id');
$articles = DB::table('articles')
->whereIn('user_id', $ifollow)->get();
//$articles = DB::table('followers')->lists('title', 'name');
//$articles = DB::table('followers')->where('follower_id', $ilike)->pluck('user_id');
//$articles = Article::all();
return view('pages.user', compact('articles'));
}
}
user.blade.php:
<div class = "grid">
#foreach($articles as $article)
<div class="grid-item ">
<img src="/image/article/detail/{{$article->id}}.jpg"/>
</div>
#endforeach
Since you are passing an Article request, you can use the $article->type attribute you declared, to use it to call css div classes.
#foreach($articles as $article)
<div class="grid-item {{$article->type}}">
<img src="/image/article/detail/{{$article->id}}.jpg"/>
</div>
And then create the desired colors in your app.css (or less/scss files):
.music {
}
.fashion{} .events{} .dance{};

Laravel displaying all posts with WHERE clause and pagenation in laravel

so i want to display post in my index page but only the posts that are reviewed by the admin. so i added another column in my database table post which is called "review" which is integer. So this is what i have in hand.
in my controller i have this
public function index()
{
$this->layout->content = View::make('partials.index',
array(
'posts' => Post::paginate(9)
));
}
and in my index page i have this
#section('content')
<div class="dashboard">
#foreach (array_chunk($posts->getCollection()->all(),2) as $row)
<div class="row">
#foreach($row as $post)
<article class="col-md-4 effect4" id="dash-box">
<p></p>
<c>{{$post->content}}</c><br>
<b>{{$post->title}}</b><br>
<d>posted..{{$post->created_at->diffForHumans()}}</d>
<hr>
</article>
#endforeach
</div>
#endforeach
<div class="page">
{{$posts->appends(Request::only('difficulty'))->links()}}
</div>
</div>
#stop
Help im newbie here i hope someone can help me out with this one hoping for a reply thanks
Just call:
Post::whereReview(1)->paginate(9);
Please read the documentation (and also the one for the query builder since these pages apply to Eloquent as well)
You can just add a where() call to the query:
$posts = Post::where('review', '=', 1)->paginate(9);
Or a shorter version (= is the default operator)
$posts = Post::where('review', 1)->paginate(9);
Or even with a dynamic method name:
$posts = Post::whereReview(1)->paginate(9);
Also you can use true instead of 1. Laravel will convert it:
$posts = Post::whereReview(true)->paginate(9);
There is also no need to do chunking that complicated:
#foreach (array_chunk($posts->getCollection()->all(),2) as $row)
You can just use the chunk method on any collection:
#foreach ($posts->chunk(2) as $row)

Categories