Split String to get an Array - laravel

In Database :- "["http://localhost//image/aeefb050911334869a7a5d9e4d0e1689.jpg","http://localhost//image/959776b99b006e5785c3a3364949ce47.jpg"]"
After Json_decode = ["http://localhost//image/aeefb050911334869a7a5d9e4d0e1689.jpg","http://localhost//image/959776b99b006e5785c3a3364949ce47.jpg"]
Output i Want:- [0] - http://localhost//image/aeefb050911334869a7a5d9e4d0e1689.jpg
[1] - http://localhost//image/959776b99b006e5785c3a3364949ce47.jpg
Code:-
#if(is_array(json_decode($data->item_image)))
#php $image = json_decode($data->item_image);
#endphp
#foreach($image as $key=>$row)
#if($key == 0)
<div class='carousel-item active'>
<!-- <img class='img-size img-responsive' src="{{ asset('image/'. $row) }}" /> -->
<img class='img-size img-responsive' src="{{ $row }}" />
</div>
#else
<div class='carousel-item'>
<!-- <img class='img-size' src="{{ asset('image/'. $row) }}" /> -->
<img class='img-size' src="{{ $row }}" />
</div>
#endif
#endforeach
#else
#endif

you can use this
$str = '["http://localhost//image/aeefb050911334869a7a5d9e4d0e1689.jpg",
"http://localhost//image/959776b99b006e5785c3a3364949ce47.jpg"]';
eval("\$myarray = $str;");
print_r($myarray);

#if(json_decode($data->item_image) != '')
#php $image = json_decode($data->item_image);
eval("\$myarray = $image;");
#endphp
#foreach($myarray as $key=>$row)
#if($key == 0)
<div class='carousel-item active'>
<!-- <img class='img-size img-responsive' src="{{ asset('image/'. $row) }}" /> -->
<img class='img-size img-responsive' src="{{ $row }}" />
</div>
#else
<div class='carousel-item'>
<!-- <img class='img-size' src="{{ asset('image/'. $row) }}" /> -->
<img class='img-size' src="{{ $row }}" />
</div>
#endif
#endforeach
#else
#endif

Related

Convert Blade If Else to Vue If Else

I have an if else condition in Blade and I want to convert it to a Vue if else
#if (Auth::user()->avatar != null)
<img src="{{ URL::to('storage/app/public') . '/' . Auth::user()->avatar }}"
class="img-circle elevation-2" alt="User Image">
#endif
#if (Auth::user()->avatar == null)
#if (Auth::user()->gender == 'Male')
<img src="{{ URL::asset('public/bower_components/admin-lte/dist/img/fb-male.jpg') }}"
class="img-circle elevation-2" alt="User Image">
#else
<img src="{{ URL::asset('public/bower_components/admin-lte/dist/img/female-fb.jpg') }}"
class="img-circle elevation-2" alt="User Image">
#endif
#endif
To convert the #if branches in your blade file, use <template>s with v-if/v-else:
<template v-if="{{ Auth::user()->avatar != null }}">
<img src="{{ URL::to('storage/app/public') . '/' . Auth::user()->avatar }}"
class="img-circle elevation-2" alt="User Image">
</template>
<template v-else>
<template v-if="{{ Auth::user()->gender == 'Male' }}">
<img src="{{ URL::asset('public/bower_components/admin-lte/dist/img/fb-male.jpg') }}"
class="img-circle elevation-2" alt="User Image">
</template>
<template v-else>
<img src="{{ URL::asset('public/bower_components/admin-lte/dist/img/female-fb.jpg') }}"
class="img-circle elevation-2" alt="User Image">
</template>
</template>

Else Statement returned Blank in Laravel

I want to implement a simple search on my Laravel Application
public function search()
{
$search = request()->query('search');
if ($search) {
$books = Book::where('name', 'LIKE', "%{$search}%")->simplepaginate(12);
}
else {
echo "<h2>Book Not Found, please try using another search term</h2>";
$books = Book::orderBy('created_at', 'desc')->simplepaginate(12);
}
return view('search')->with('books', $books);
}
But the else returned a blank screen when the search terms can't be found
UPDATE
Here is my view file
<div class="container">
<div class="row">
<div class="col-sm-12 col-md-10 offset-md-1">
<div class="row">
#foreach($books as $book)
<div class="col-md-3">
<div class="home-catalog-image">
<a href="{{ route('book', $book->id) }}" target="_blank">
<!-- <img src="{{ $book->image }}" alt="trending image" /> -->
<img src="{{ $book->image_url }}" class="img-responsive" alt="{{$book->image_url}}">
</a>
</div>
<p class="author">{{ $book->author->name }}</p>
<h1 class="book-title">{{str_limit($book -> name, 20) }}</h1>
</div>
#endforeach
</div>
<p style="text-align:center;>"> {!! $books->render() !!} </p>
</div>
</div>
</div>
Check how to use the request() helper to get the input.
Also you can use when() method for conditional clauses instead if else.
public function search()
{
$search = request('search', null);
$books = Book::when($search, function ($query, $search) {
return $query->where('name', 'LIKE', "%{$search}%");
})
->orderBy('created_at', 'desc')
->simplePaginate(12);
return view('search')->with('books', $books);
}
Then in Blade you can use #forelse directive to loop the collection, or if it's empty, show the message.
<div class="container">
<div class="row">
<div class="col-sm-12 col-md-10 offset-md-1">
<div class="row">
#forelse ($books as $book)
<div class="col-md-3">
<div class="home-catalog-image">
<a href="{{ route('book', $book->id) }}" target="_blank">
<!-- <img src="{{ $book->image }}" alt="trending image" /> -->
<img src="{{ $book->image_url }}" class="img-responsive" alt="{{$book->image_url}}">
</a>
</div>
<p class="author">{{ $book->author->name }}</p>
<h1 class="book-title">{{str_limit($book -> name, 20) }}</h1>
</div>
#empty
<h2>Book Not Found, please try using another search term</h2>
#endforelse
</div>
<p style="text-align:center;>"> {!! $books->render() !!} </p>
</div>
</div>
</div>
To solve my problem, I use forelse instead of foreach a
#forelse($books as $book)
<div class="col-md-3">
<div class="home-catalog-image">
<a href="{{ route('book', $book->id) }}" target="_blank">
<!-- <img src="{{ $book->image }}" alt="trending image" /> -->
<img src="{{ $book->image_url }}" class="img-responsive" alt="{{$book->image_url}}">
</a>
</div>
<p class="author">{{ $book->author->name }}</p>
<h1 class="book-title">{{str_limit($book -> name, 20) }}</h1>
</div>
#empty
<h2>Book Not Found, please try using another search term</h2>
#endforelse
In my controller, I used
public function search()
{
$search = request('search', null);
$books = Book::when($search, function ($query, $search) {
return $query->where('name', 'LIKE', "%{$search}%");
})
->orderBy('created_at', 'desc')
->simplePaginate(12);
return view('search')->with('books', $books);
}

dompdf laravel on live server, can't download pdf and return error 500

I hosted my Laravel Project to 000webhost, my project can make report in pdf format. I use dompdf to do all pdf things. When in my localhost server (xampp), dompdf works fine with no error and rendering html very well. In live server i got error 500 "Whoops, something went wrong on our servers.". I can't find any solution, i thought it was execution time, i rise it to 2000 seconds still got 500.
here is my controller
public function retur_preport(Request $request)
{
ini_set('max_execution_time', '2000');
ini_set('memory_limit','512M');
$tgl1 = date('Y-m-d', strtotime($request->input('tanggal_mulai')));
$tgl2 = date('Y-m-d', strtotime($request->input('sampai_tanggal')));
if($tgl1 > date('Y-m-d') || $tgl2 > date('Y-m-d')){
return redirect('upper/laporan/retur')->with('error','Tanggal yang diinput tidak bisa lebih besar dari '.date('d M Y').' (hari ini)');
}elseif($tgl2 != '1970-01-01' && $tgl1 > $tgl2){
return redirect('upper/laporan/retur')->with('error','Tanggal mulai tidak bisa lebih besar dari inputan tanggal sampai');
}elseif($tgl1 == '1970-01-01' && $tgl2 == '1970-01-01'){
return redirect('upper/laporan/retur')->with('error','Salah satu dari kedua tanggal harus diisi');
}elseif($tgl1 == '1970-01-01'){
$r = \App\Retur::whereDate('tgl_retur',$tgl2)->orderBy('tgl_retur','desc')->get();
$pdf = PDF::loadview('pimpinan/laporan/retur/report', compact('r','tgl1','tgl2'))->setPaper('a4','landscape');
return $pdf->download('laporan retur tanggal '.date('d-M-Y', strtotime($tgl2)).'.pdf');
}elseif($tgl2 == '1970-01-01'){
$r = \App\Retur::whereBetween('tgl_retur',[$tgl1, date('Y-m-d')])->orderBy('tgl_retur','desc')->get();
$pdf = PDF::loadview('pimpinan/laporan/retur/report',compact('r','tgl1','tgl2'))->setPaper('a4','landscape');
return $pdf->download('laporan retur tanggal '.date('d-M-Y', strtotime($tgl1)).' sampai tanggal '.date('d-M-Y').'.pdf');
}else{
$r = \App\Retur::whereBetween('tgl_retur',[$tgl1,$tgl2])->orderBy('tgl_retur','desc')->get();
$pdf = PDF::loadview('pimpinan/laporan/retur/report', compact('r','tgl1','tgl2'))->setPaper('a4','landscape');
return $pdf->download('laporan retur tanggal '.date('d-M-Y', strtotime($tgl1)).' sampai tanggal '.date('d-M-Y', strtotime($tgl2)).'.pdf');
}
}
and here is my html that rendered from controller
<html>
<head>
<link rel="stylesheet" href="{{ asset('assets') }}/bower_components/bootstrap/dist/css/bootstrap.min.css">
<!-- Font Awesome -->
<link rel="stylesheet" href="{{ asset('assets') }}/bower_components/font-awesome/css/font-awesome.min.css">
<!-- Ionicons -->
<link rel="stylesheet" href="{{ asset('assets') }}/bower_components/Ionicons/css/ionicons.min.css">
<!-- Theme style -->
<link rel="stylesheet" href="{{ asset('assets') }}/dist/css/AdminLTE.min.css">
<!-- AdminLTE Skins. Choose a skin from the css/skins
folder instead of downloading all of them to reduce the load. -->
<link rel="stylesheet" href="{{ asset('assets') }}/dist/css/skins/_all-skins.min.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700,300italic,400italic,600italic">
<style>
.no-gutters {
margin-right: 0;
margin-left: 0;
> .col,
> [class*="col-"] {
padding-right: 0;
padding-left: 0;
}
}
table, td, th, tr, thead, tbody {
border: 1px solid black !important;
}
</style>
</head>
<body>
<div class="content-header row text-center">
<img src="{{ asset('uploads') }}/kadatuan_logo/logo.jpg" width="120" height="120"><br>
Jl. Karawitan No. 48, Bandung
</div>
<section class="content">
<div class="box box-primary box-solid">
<div class="box-header">
Laporan Retur
#if($tgl1 == '1970-01-01')
Tanggal <strong>{{ date('d-M-Y', strtotime($tgl2)) }}</strong>
#elseif($tgl2 == '1970-01-01')
Tanggal <strong>{{ date('d-M-Y', strtotime($tgl1)) }}</strong> Sampai <strong>{{ date('d-M-Y') }}</strong>
#else
Tanggal <strong>{{ date('d-M-Y', strtotime($tgl1)) }}</strong> Sampai <strong>{{ date('d-M-Y', strtotime($tgl2)) }}</strong>
#endif
<div class="pull-right">Dicetak pada tanggal <strong>{{ date('d-M-Y') }}</strong></div>
</div>
<div class="box-body">
<div class="d-flex justify-content-center">
<table class="table text-center">
<thead>
<tr>
<th>No</th>
<th>ID Pemesanan</th>
<th>ID Retur</th>
<th>Tanggal Retur</th>
<th>Nama Mitra</th>
<th>Barang Retur</th>
<th>Jumlah Retur</th>
<th>Status Retur</th>
<th>Keterangan</th>
</tr>
</thead>
<tbody>
#foreach ($r as $row)
<tr>
<td rowspan="{{ \App\DRetur::where('id_retur',$row->id_retur)->count() }}">{{ !empty($i) ? ++$i : $i = 1}}</td>
<td rowspan="{{ \App\DRetur::where('id_retur',$row->id_retur)->count() }}">{{ $row->id_pemesanan }}</td>
<td rowspan="{{ \App\DRetur::where('id_retur',$row->id_retur)->count() }}">{{ $row->id_retur }}</td>
<td rowspan="{{ \App\DRetur::where('id_retur',$row->id_retur)->count() }}">{{ date('d-M-Y', strtotime($row->tgl_retur)) }}</td>
<td rowspan="{{ \App\DRetur::where('id_retur',$row->id_retur)->count() }}">{{ $row->mitra->nama_mitra }}</td>
#foreach (\App\DRetur::where('id_retur',$row->id_retur)->limit(1)->get() as $row2)
<td>{{ \App\TPemesanan::join('t_dpemesanan','t_tpemesanan.id_dpemesanan','=','t_dpemesanan.id_dpemesanan')->join('t_stock','t_dpemesanan.id_stock','=','t_stock.id_stock')->where('t_tpemesanan.id_tpemesanan',$row2->id_tpemesanan)->first()->nama_stock }}</td>
<td>{{ $row2->jumlah_dretur }}{{ \App\TPemesanan::join('t_dpemesanan','t_tpemesanan.id_dpemesanan','=','t_dpemesanan.id_dpemesanan')->join('t_stock','t_dpemesanan.id_stock','=','t_stock.id_stock')->where('t_tpemesanan.id_tpemesanan',$row2->id_tpemesanan)->first()->satuan }}</td>
#if( $row2->status_dretur == 1 )
<td>Menunggu Diperiksa</td>
#elseif( $row2->status_dretur == 2 )
<td>Diterima</td>
#elseif( $row2->status_dretur == 3 )
<td>Ditolak</td>
#endif
<td>{{ $row2->keterangan }}</td>
</tr>
#foreach(\App\DRetur::where('id_retur',$row->id_retur)->get() as $row3)
#if($row2->id_dretur != $row3->id_dretur)
<tr>
<td>{{ \App\TPemesanan::join('t_dpemesanan','t_tpemesanan.id_dpemesanan','=','t_dpemesanan.id_dpemesanan')->join('t_stock','t_dpemesanan.id_stock','=','t_stock.id_stock')->where('t_tpemesanan.id_tpemesanan',$row3->id_tpemesanan)->first()->nama_stock }}</td>
<td>{{ $row3->jumlah_dretur }}{{ $row2->jumlah_dretur }}{{ \App\TPemesanan::join('t_dpemesanan','t_tpemesanan.id_dpemesanan','=','t_dpemesanan.id_dpemesanan')->join('t_stock','t_dpemesanan.id_stock','=','t_stock.id_stock')->where('t_tpemesanan.id_tpemesanan',$row3->id_tpemesanan)->first()->satuan }}</td>
#if( $row3->status_dretur == 1 )
<td>Menunggu Diperiksa</td>
#elseif( $row3->status_dretur == 2 )
<td>Diterima</td>
#elseif( $row3->status_dretur == 3 )
<td>Ditolak</td>
#endif
<td>{{ $row3->keterangan }}</td>
</tr>
#endif
#endforeach
#endforeach
#endforeach
</tbody>
</table>
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
Sistem Informasi Supply
<div class="pull-right">
Hormat Saya, <strong>Reyhan (Admin)</strong>
</div>
</div>
<!-- /.box-footer-->
</div>
<!-- /.box -->
</section>
<!-- jQuery 3 -->
<script src="{{ asset('assets') }}/bower_components/jquery/dist/jquery.min.js"></script>
<!-- Bootstrap 3.3.7 -->
<script src="{{ asset('assets') }}/bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
<!-- SlimScroll -->
<script src="{{ asset('assets') }}/bower_components/jquery-slimscroll/jquery.slimscroll.min.js"></script>
<!-- FastClick -->
<script src="{{ asset('assets') }}/bower_components/fastclick/lib/fastclick.js"></script>
<!-- AdminLTE App -->
<script src="{{ asset('assets') }}/dist/js/adminlte.min.js"></script>
<!-- AdminLTE for demo purposes -->
<script src="{{ asset('assets') }}/dist/js/demo.js"></script>
<script>
$(document).ready(function () {
$('.sidebar-menu').tree()
})
</script>

Dompf load forever laravel 4.2

Im using dompf trying to generate a pdf and save it to the storage folder and a database, the problem is when i try to generate the pdf, it never load, when i use the sample code in github it work, it shows "test", but when i try to load a view it takes forever. this is the code i'm using
$pdf = PDF::loadView('emails.myView',$myData);
return $pdf->stream();
I have tried using the download and the save methods, but doens't work and the page load forever but the pdf is never generated.
and the view just show 4 or 6 elements with 1 main call $hist, returning the view is rendered without problems.
<style type="text/css">
Bunch of css.
</style>
<table class="center-block">
<tr>
<td colspan="3">
<img src="{{ asset('images/boletin/btn_cabeza.jpg') }}" class="img-responsive center-block">
</td>
</tr>
<tr>
<td rowspan="4" class="aside">
<a href="{{ URL::to('contacto/donaciones') }}">
<img src="{{ asset('images/boletin/btn_dona.jpg') }}" class="img-responsive center-block" >
</a>
<div class="social-container">
<h3>Siguenos en:</h3>
<hr>
<ul>
<li><i class="fa fa-twitter"></i></li>
<li><i class="fa fa-instagram"></i></li>
<li><i class="fa fa-facebook"></i></li>
<li><i class="fa fa-youtube-play"></i></li>
</ul>
</div>
<h2 class="text-blue">Historias Epékeinas</h2>
<br>
#if(count($hist->imagenes) > 0)
<img src="{{ asset('images/news/'.$hist->imagenes->first()->image) }}" class="img-responsive img-boletin" alt="{{ $hist->titles->first()->text }}">
#endif
<div class="bg-green padding-20">
<h2 class="boletin-title">
{{ $hist->titles->first()->text }}
#if(!is_null($hist->subtitle))
{{ $hist->subtitle->titles->first()->text }}
#endif
</h2>
</div>
<hr>
<div class="text-justify">
{{ substr(strip_tags($hist->descriptions->first()->text), 0, 1600) }}[...]
<br>
Leer más
</div>
</td>
</tr>
<?php $k = 0;?>
#foreach($article as $a)
#if($k == 0 || $k%2 == 0)
<tr>
#endif
#if(!empty($principal))
#if($a->slugs->first()->text != $principal->id)
<td class="news fixedHeight bg-{{ $colors[$j] }}">
#if(count($a->imagenes) > 0)
<img src="{{ asset('images/news/'.$a->imagenes->first()->image) }}" class="img-responsive center-block img-boletin" alt="{{ $a->titles->first()->text }}">
#else
<img src="{{ asset('images/logo.png') }}" class="img-responsive center-block img-boletin" alt="{{ $a->titles->first()->text }}">
#endif
<h2 class="boletin-title">{{ $a->titles->first()->text }}</h2>
<p class="text-justify">{{ substr(strip_tags($a->descriptions->first()->text), 0, 300) }} [...]</p>
<a target="_blank" href="{{ URL::to('noticias/'.$a->slugs->first()->text) }}" class="btn btn-default btn-xs pull-right">Leer más</a>
<div class="clearfix"></div>
</td>
<?php $k++; ?>
#endif
#else
<td class="news fixedHeight bg-{{ $colors[$j] }}">
#if(count($a->imagenes) > 0)
<img src="{{ asset('images/news/'.$a->imagenes->first()->image) }}" class="img-responsive center-block img-boletin" alt="{{ $a->titles->first()->text }}">
#else
<img src="{{ asset('images/logo.png') }}" class="img-responsive center-block img-boletin" alt="{{ $a->titles->first()->text }}">
#endif
<h2 class="boletin-title">{{ $a->titles->first()->text }}</h2>
<p class="text-justify">{{ substr(strip_tags($a->descriptions->first()->text), 0, 300) }} [...]</p>
<a target="_blank" href="{{ URL::to('noticias/'.$a->slugs->first()->text) }}" class="btn btn-default btn-xs pull-right">Leer más</a>
<div class="clearfix"></div>
</td>
<?php $k++; ?>
#endif
<?php $j++; ?>
#if($j == 4)
<?php $j=0; ?>
#endif
#if(($k != 0 && $k%2 == 0) || $k == count($article))
</tr>
#endif
#endforeach
<tr>
<td colspan="3" class="text-center">
<h3>© Derechos Reservados Funda Epékeina 2016.</h3>
</td>
</tr>
</table>
<div class="container center-block">
<div class="bg-square bg-blue"></div>
<div class="bg-square bg-yellow"></div>
<div class="bg-square bg-green"></div>
<div class="bg-square bg-pink"></div>
</div>
<div class="clearfix"></div>
what could be wrong? is there any other choice for generating pdf.
Check those points in order:
For Laravel 4.* I suggest you to use this DOMPDF wrapper https://github.com/barryvdh/laravel-dompdf/tree/0.4
Maybe your view is trying to render content out of the page, try to add dinamic page break, you can do so by styling an element with page-break-before: always; or page-break-after: always.; Try to delete the foreach and adding 1 or 2 static element rendering it in one page
Check for errors in the view by rendering it in html with return View::make('emails.myView',$myData)->render();.
Check php and html sintax, dompdf doesn't work if some html tags are
not well closed
Consider to use Knp-snappy library for Laravel https://github.com/barryvdh/laravel-snappy/tree/0.1

Displaying feeds and blogs according to the created time

I'm having a home page where all the feeds along with comments and blogs are being displayed. Right now it is showing first all the feeds and then the blog. But I want it should be displayed according to the created time, mixer of feeds and blog not like first feed will come and then the blog. Can anyone tell me how to acheive that. This is my index.blade.php
#extends('layouts/default')
{{-- Page title --}}
#section('title')
Home
#parent
#stop
{{-- page level styles --}}
#section('header_styles')
<!--page level css starts-->
<link rel="stylesheet" type="text/css" href="{{ asset('assets/css/frontend/action.css') }}">
<link rel="stylesheet" type="text/css" href="{{ asset('assets/css/frontend/tabbular.css') }}">
<link rel="stylesheet" type="text/css" href="{{ asset('assets/css/frontend/jquery.circliful.css') }}">
<link rel="stylesheet" type="text/css" href="{{ asset('assets/vendors/owl.carousel/css/owl.carousel.css') }}">
<link rel="stylesheet" type="text/css" href="{{ asset('assets/vendors/owl.carousel/css/owl.theme.css') }}">
<!--end of page level css-->
#stop
{{-- content --}}
#section('content')
<div class="row">
<div class="column col-md-1 col-xs-1 col-sm-1"></div>
<div class="column col-md-3 col-xs-3 col-sm-3"><!--gm-editable-region--> </div>
<div class="column col-md-4 col-xs-4 col-sm-4">
#include ('action.partials.form')
#include ('action.partials.error')
#include ('action.partials.feed')
#include ('action.partials.blogfeed')
</div>
<div class="column col-md-3 col-xs-3 col-sm-3"><!--gm-editable-region--></div>
<div class="column col-md-1 col-xs-1 col-sm-1"></div>
#stop
{{-- footer scripts --}}
#section('footer_scripts')
<!-- page level js starts-->
<script type="text/javascript" src="{{ asset('assets/js/frontend/jquery.circliful.js') }}"></script>
<script type="text/javascript" src="{{ asset('assets/vendors/owl.carousel/js/owl.carousel.min.js') }}"></script>
<script type="text/javascript" src="{{ asset('assets/js/frontend/carousel.js') }}"></script>
<script type="text/javascript" src="{{ asset('assets/js/frontend/index.js') }}"></script>
<!--page level js ends-->
#stop
This is action.partials.feed
#foreach($feeds as $feed)
<article class="media">
<div class="well">
<div class="pull-left">
<img class="profile" src="{{ URL::to('/uploads/users/'.$feed->user->pic) }}" class="img-responsive" alt="Image" style="width:48px;height:48px;padding-right : 10px;padding-bottom: 5px;">
</div>
<strong>{{ $feed->user->first_name }}
{{ $feed->user->last_name }}
<small> posted </small>
</strong>
{{ $feed->created_at->diffForHumans() }}<br><hr>
{{ $feed->feed_content }}
<hr>
{!! Form::open(['url' => 'home/{storecomment}']) !!}
<div><input type="hidden" name="feed_id" value="{{ $feed->feed_id }}" /></div>
<div class="form-group">
{!! Form::text('comment', null, ['class'=>'form-control', 'rows'=>3, 'placeholder'=>"Comment"]) !!}
</div>
<div class="form-group feed_post_submit">
{!! Form::submit('Comment', ['class' => 'btn btn-default btn-xs']) !!}
</div>
{!! Form::close() !!}
#foreach($feed->comments as $comment)
<div class="pull-left">
<img class="profile" src="{{ URL::to('/uploads/users/'. $comment->user->pic) }}" class="img-responsive" alt="Image" style="width:48px;height:48px;padding-right : 10px;padding-bottom: 5px;">
</div>
{{ $comment->user->first_name }}
{{ $comment->created_at->diffForHumans() }}
{{ $comment->comment }}<hr>
#endforeach
</div>
</article>
#endforeach
action.partials.blogfeed
#foreach($blogs as $blog)
<article class="media">
<div class="well">
<div class="pull-left">
<img class="media-object" src="{{ URL::to('/uploads/users/'.$blog->user->pic) }}" class="img-responsive" alt="Image" style="width:48px;height:48px;padding-right : 10px;padding-bottom: 5px;">
</div>
<strong>{{ $blog->user->first_name }}
{{ $blog->user->last_name }}
<small> posted blog</small>
</strong>
{{ $blog->created_at->diffForHumans() }}<br><hr>
<h4>{{ $blog->title }}</h4>
<div class="featured-post-wide thumbnail">
#if($blog->image)
<img src="{{ URL::to('/uploads/blog/'.$blog->image) }}" class="img-responsive" alt="Image">
#endif
</div>
</div>
</article>
#endforeach
This is my feedcontroller
<?php
namespace App\Http\Controllers;
use Request;
use Auth;
use Sentinel;
use App\Feed;
use App\Http\Requests;
use App\Blog;
use App\Http\Controllers\Controller;
use App\Comment;
class FeedsController extends Controller
{
public function index() {
// $comments = Comment::latest()->get();
$feeds = Feed::with('comments', 'user')->where('user_id', Sentinel::getUser()->id)->latest()->get();
$blogs = Blog::latest()->simplePaginate(5);
$blogs->setPath('blog');
return view('action.index', compact('feeds', 'blogs'));
}
public function store(Requests\CreateFeedRequest $request)
{
$request->merge( [ 'user_id' => Sentinel::getuser()->id] );
Feed::create($request->all());
return redirect('home');
}
public function storecomment(Requests\CommentRequest $request, Feed $feed)
{
$comment = new Comment;
$comment->user_id =Sentinel::getuser()->id;
$comment->feed_id = $request->feed_id;
$comment->comment = $request->comment;
$comment->save();
return redirect('/home');
}
}
Can any one tell me how to display feeds and blogs according to the published time.
In your controller index method, try something like this:
public function index() {
// $feeds = Feed::with('comments', 'user')->where('user_id', Sentinel::getUser()->id)->latest()->get();
// $blogs = Blog::latest()->simplePaginate(5);
// $blogs->setPath('blog');
$feeds = Feed::with('comments', 'user')->where('user_id', Sentinel::getUser()->id)->latest()->get();
$blogs = Blog::latest()->paginate(5);
$feeds = $feeds->merge($blogs)->sortByDesc('created_at');
return view('action.index', compact('feeds'));
}
The biggest problem you're going to have is that your Feed object is likely different than your Blog object. Meaning each will have unique column names that the other doesn't have. This is going to make doing the foreach a bit of a mess...
Remove #include ('action.partials.blogfeed'). This is not longer relevant for us.
In action.partials.feed...we'll output it all (hopefully without too many "hacks" and conditionals):
#foreach($feeds as $feed)
<article class="media">
<div class="well">
<div class="pull-left">
<img class="profile" src="{{ URL::to('/uploads/users/'.$feed->user->pic) }}" class="img-responsive" alt="Image" style="width:48px;height:48px;padding-right : 10px;padding-bottom: 5px;">
</div>
<strong>
{{ $feed->user->first_name }}
{{ $feed->user->last_name }}
<small> posted </small>
</strong>
// We'll use #if(isset($feed->title)) to check if it's a blog post, aka ugly hack.
#if(isset($feed->title))
{{ $blog->created_at->diffForHumans() }}
<br><hr>
<h4>{{ $blog->title }}</h4>
<div class="featured-post-wide thumbnail">
#if($blog->image)
<img src="{{ URL::to('/uploads/blog/'.$blog->image) }}" class="img-responsive" alt="Image">
#endif
</div>
#else
{{ $feed->created_at->diffForHumans() }}<br><hr>
{{ $feed->feed_content }}
<hr>
{!! Form::open(['url' => 'home/{storecomment}']) !!}
<div><input type="hidden" name="feed_id" value="{{ $feed->feed_id }}" /></div>
<div class="form-group">
{!! Form::text('comment', null, ['class'=>'form-control', 'rows'=>3, 'placeholder'=>"Comment"]) !!}
</div>
<div class="form-group feed_post_submit">
{!! Form::submit('Comment', ['class' => 'btn btn-default btn-xs']) !!}
</div>
{!! Form::close() !!}
#foreach($feed->comments as $comment)
<div class="pull-left">
<img class="profile" src="{{ URL::to('/uploads/users/'. $comment->user->pic) }}" class="img-responsive" alt="Image" style="width:48px;height:48px;padding-right : 10px;padding-bottom: 5px;">
</div>
{{ $comment->user->first_name }}
{{ $comment->created_at->diffForHumans() }}
{{ $comment->comment }}<hr>
#endforeach
#endif
</div>
</article>
#endforeach
Because Illuminate\Database\Eloquent extends Illuminate\Support\Collection, we can easily merge them together. Because both $feeds and $blogs are an Eloquent Collection, we can easily merge them into one collection:
$feeds_and_blogs = collect($feeds->toArray(), $blogs->toArray());
Now you will have a combination of both. Because you've used $table->timestamps() in your migration to get your columns, we can easily perform a comparison against the created_at timestamp to get the sorting that you want:
$sorted_feeds_and_blogs = $feeds_and_blogs->sortBy(function($item){
return $item->created_at;
});
However, you likely want them to be sorted by newest first. Thankfully, collections have a sortByDesc function which will do exactly what we want.
Although in the grand scheme of things you probably really want a Polymorphic relationship.

Resources