Laravel4.2 IF locale statement - laravel

I have one question, I'm developing a multi-lang site and struggling of displaying data for each lang. Can I write a IF statement where it detects current locale, for example:
#if('locale' = 'lv')
#foreach($slides_lv as $slide)
<img src="{{ $slide->path }}">
#endforeach
#endif
#if('locale' = 'en')
#foreach($slides_en as $slide)
<img src="{{ $slide->path }}">
#endforeach
#endif
#if('locale' = 'ru')
#foreach($slides_ru as $slide)
<img src="{{ $slide->path }}">
#endforeach
#endif
Or there is some better way to do it in Laravel ?

You can do it this way:
<?php
$varName = 'slides_' . App::getLocale();
?>
#foreach ($$varName as $slide)
<img src="{{ $slide->path }}">
#endforeach
App::getLocale() will return the locale (in your case 'lv', 'en' etc).
So you build a dynamic variable name based on it and the use it for your foreach loop.
Take care

Related

Undefined property: Illuminate\Pagination\Paginator::$image

I need your help with this simplePaginate();
controller:
foreach($lesson->products as $product)
{
$p[] = $product->simplePaginate(1);
}
view:
#foreach($p as $data)
<img src="{{ asset('storage/'.$data->image) }}" alt="">
#endforeach
{{ $p->links('layouts.paginate') }}
what is wrong that I got this error:
Undefined property: Illuminate\Pagination\Paginator::$image
Why dont you remove that foreach to generate $p and loop $lesson->products in the blade directly
#foreach($lesson->products as $product)
<img src="{{ asset('storage/'.$product->image) }}" alt="">
#endforeach
If you need the simplePaginate() for reasons not posted in your question, then you can loop it like this
#foreach($p as $productSimplePaginate)
#foreach($productSimplePaginate as $product)
<img src="{{ asset('storage/'.$product->image) }}" alt="">
#endforeach
#endforeach

Trying to get property 'header_image' of non-object

I want to show last database data in my website page but I have this error "Trying to get property 'header_image' of non-object"(I have data in database) what is the problem?
controller:
public function show()
{
$edu = Classes::get()->first();
return view('UI.classes.educationcourses' , compact('edu'));
}
blade:
#foreach( $edu as $class)
<section id="home">
<div id="home-slider">
<div class="slide-item">
<img src="{{ $class->header_image }}" alt="">
</div>
</div>
Try this -
In controller:
public function show()
{
$edu = Classes::all();
return view('UI.classes.educationcourses',compact('edu'));
}
And in blade file:
#foreach( $edu as $class)
<section id="home">
<div id="home-slider">
<div class="slide-item">
#if($class->header_image)
<img src="{{ asset('imagepath/'.$class->header_image) }}" alt="">
#else
<img src="{{ asset('imagepath/defaultimage.ext') }}" alt="">
#endif
<div class="slide-wrap">
Please make sure your image exists in the folder.
Update:
If you want a single row and the last one, then you don't need to go through with foreach loop in blade file.
Then you should try this:
public function show()
{
$edu = Classes::latest()->first();
return view('UI.classes.educationcourses',compact('edu'));
}
And in blade file:
<section id="home">
<div id="home-slider">
<div class="slide-item">
#if($edu->header_image)
<img src="{{ asset('imagepath/'.$edu->header_image) }}" alt="">
#else
<img src="{{ asset('imagepath/defaultimage.ext') }}" alt="">
#endif
<div class="slide-wrap">
You can get the last one by using the entry time in that table.
$edu = Classes::where('any_condition',true)->order_by('upload_time', 'desc')->first();
And then in the blade, you can simply use $edu without any #foreach loop. Also, where clause is not necessary, just added it there for now.

Displaying A Laravel Collection in a blade template

I'm having issues displaying a collection in a blade template.
$comments = Comment::all();
return view('comments/index')->with(compact('comments'));
The code for the blade is:
#isset($comments)
#foreach($comments as $comment)
<div>
<p>
<{{ $comment->commentor }}
</p>
</div>
<hr>
#endforeach
#endisset
#empty($comments)
<div>
<p>There were no comments available.</p>
{{ $comments }}
</div>
#endempty
But not sure how to get the data to render in the template. It just renders a blankpage.
Use this instead :
$comments = Comment::all();
return view('comments.index')->with(compact('comments'));
Use dot notation to reference the view folder structure, view('comments.index'). This represents the file resources/views/comments/index.blade.php. Use this.
#forelse ($comments as $comment)
<div>
<p>
{{ $comment->commentor }}
</p>
</div>
<hr/>
#empty
<div>
<p>There were no comments available.</p>
</div>
#endforelse

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.

If external url contains this url then

I have this controller which is storing the host and url of a video in youtube, vimeo, vevo etc
public function attach()
{
$input = Input::except('_token');
if ( ! Input::get('host') || ! Input::get('url')) {
return Response::json(trans('video::main.fillAllRequiredFields'), 400);
}
}
And in the blade i have this
#foreach ($title->link as $k => $video)
<p><a target="_blank" href="{{ $video->url }}">{{ $video->host }}</a></p>
#endforeach
So as for now the member has to insert both the url and the label of the host..
I want to use a function either in Laravel or knockout.js so when an url is inserted to extract the host and show the host label automatically without been stored in DB. Something like this
#foreach ($title->link as $k => $video)
#if {{$video->url}} contain "youtube.com"
<p><a target="_blank" href="{{ $video->url }}">YouTube</a>
#elseif {{$video->url}} contain "vimeo.com"
<p><a target="_blank" href="{{ $video->url }}">Vimeo</a>
#elseif {{$video->url}} contain "vevo.com"
<p><a target="_blank" href="{{ $video->url }}">Vimeo</a>
#else <p><a target="_blank" href="{{ $video->url }}">Unknown</a>
#endif
#endforeach
Any help is really appreciated
PHP function strpos() allows you to check if the url string has specific host inside it or not. For example like this
#foreach ($title->link as $k => $video)
#if(strpos($video->url, "youtube.com")!==false)
<p><a target="_blank" href="{{ $video->url }}">YouTube</a>
#if(strpos($video->url, "vimeo.com")!==false)
<p><a target="_blank" href="{{ $video->url }}">Vimeo</a>
#if(strpos($video->url, "vevo.com")!==false)
<p><a target="_blank" href="{{ $video->url }}">Vevo</a>
#else <p><a target="_blank" href="{{ $video->url }}">Unknown</a>
#endif
#endforeach

Resources