Failed to refresh component on click in livewire - php

Tried all the possible solution to refresh the component but failed. Is there any option not to push the new comments and refresh the $comments variable whatever it contains?
Want to refresh $comments and its relation with replies in the blade on click of the send button
Calling $refresh action on the send button but it doesn't seem to work. Even tried to re-render the component with $this->render(). This also fails.
<div>
#foreach($comments as $item)
<div class="row" #if($item->reply_id != null) style="margin-left:40px;" #endif>
<div class="col-md-12">
<div class="py-md-4 py-3 border_bottom" >
<div class="row">
<div class="col-md-1 col-sm-1 col-1">
<div class="rating_person pt-3">
<img src="{{ isset($item->owner->profile_picture) ? asset($item->owner->profile_picture) : asset('images/default.jpg') }}">
</div>
</div>
<div class="col-md-11 col-sm-12 col-12">
<div class="rating_view_here_content_single_border">
<div class="rating_view_here_content_single">
<div class="rating_view_stars d-flex justify-content-between pt-3">
<div class="rating_view_heading">
<h4>{{ $item->owner->name }}</h4>
<div class="rating_wrapper">
<a href="#" class="grey_rating active-rating">
<i class="fa fa-star" aria-hidden="true"></i>
</a>
<a href="#" class="grey_rating active-rating">
<i class="fa fa-star" aria-hidden="true"></i>
</a>
<a href="#" class="grey_rating active-rating">
<i class="fa fa-star" aria-hidden="true"></i>
</a>
<a href="#" class="grey_rating active-rating">
<i class="fa fa-star" aria-hidden="true"></i>
</a>
<a href="#" class="grey_rating active-rating">
<i class="fa fa-star" aria-hidden="true"></i>
</a>
</div>
</div>
<span class="rating_days">{{ \App\Library\Helper::getTime($item->created_at) }}</span>
</div>
<div class="rating_comment_here pt-3">
<p>{{ $item->comment }}</p>
<p>
<a href="javascript:void(0)" class="reply" data-id="{{ $item->id }}" wire:click="toggleCommentBox({{ $item->id }}, 'true')">
<i class="fa fa-reply" aria-hidden="true"></i> <span>Reply</span>
</a>
</p>
</div>
#if($item_id == $item->id && $show=="true")
<div class="video_inner_form_wrapper commentBox-{{ $item->id }}">
<form wire:submit.prevent="sendReply" class="row">
<input type="hidden" name="reply_id" value="{{ $item->id }}">
<div class="comment_input col-md-8">
<input type="text" class="bluish_label video_inner_input #error('replyText') is-invalid #enderror" wire:model.lazy="replyText" placeholder="Write your comment here">
#error('replyText')
<div class="invalid-feedback">{{$message}}</div>
#enderror
</div>
<div class="col-md-4">
<button type="submit" class="btn_edit text-uppercase" wire:click="$refresh">Send</a>
<button type="button" class="btn_edit text-uppercase cancel" wire:click="toggleCommentBox({{ $item->id }}, 'false')">Cancel</a>
</div></form>
</div>
#endif
</div>
</div>
</div>
</div>
</div>
<livewire:frontend.discussion-chat :key="'chat-' . $item->id" :comments="$item->replies" :id="$model_id" :type="$model_type" />
</div>
</div>
#endforeach
</div>

Since none of the properties is updated, the livewire chat component will not refresh.
Possible solution is not to pass the list as a param but fetch it inside the chat component. Use events to trigger a refresh:
#click="$emit('discussion.chat.{{ $id }}.refresh')"
Inside frontend.discussion-chat have a
public function listeners(): array
{
return [
"discussion.chat.$this->id.refresh" => "reload"
];
}
public function reload(): void
{
// E.g.
$this->comments->fresh();
}

Related

Trying to show currently authenticated user post in laravel view

I am working on a Laravel project and I want to display currently authenticated users posts in view file, I am getting an error while trying to achieve that says
"Class name must be a valid object or a string"...
my blade view
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div id="accordion">
<div class="card">
<div class="card-header" id="headingOne">
<h5 class="mb-0">
<button class="btn btn-link" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
<i class="fa fa-product-hunt" aria-hidden="true"></i> Swap
</button>
</h5>
</div>
<div id="collapseOne" class="collapse show" aria-labelledby="headingOne" data-parent="#accordion">
<div class="card-body">
#foreach($data as $dat)
<div class="card mb-3" style="max-width: 540px;">
<div class="row g-0">
<div class="col-md-4">
<img
src="{{asset('images/' . $dat->imogi)}}"
alt="Trendy Pants and Shoes"
/>
</div>
<div class="col-md-8">
<div class="card-body">
<div style="text-align: right;">
<small style="color:#3490dc;">{{$dat->organazation}}</small> <small><i class="fa fa-calendar" style="color:#3490dc" aria-hidden="true"></i> {{ $dat->created_at->format('d/m/Y') }}</small>
</div>
<h5 class="card-title"><i class="fa fa-user-circle-o" style="color:#3490dc" aria-hidden="true"></i> {{$dat->name}}</h5>
<small><i class="fa fa-map-marker" style="color:#3490dc" aria-hidden="true"></i> {{$dat->location}}</small> | <small><i class="fa fa-phone" style="color:#3490dc" aria-hidden="true"></i>{{$dat->contact}}</small>
<hr/>
<p class="card-text">
<b>{{$dat->title}}</b>
</p>
<small style="color:#3490dc;">M{{$dat->price}}</small>
<p class="card-text">
{{$dat->message}}
</p>
</div>
</div>
</div>
</div>
<br/>
#endforeach
</div>
</div>
</div>
<div class="card">
<div class="card-header" id="headingTwo">
<h5 class="mb-0">
<button class="btn btn-link collapsed" data-toggle="collapse" data-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
<i class="fa fa-product-hunt" aria-hidden="true"></i> Work
</button>
</h5>
</div>
<div id="collapseTwo" class="collapse" aria-labelledby="headingTwo" data-parent="#accordion">
<div class="card-body">
<form class="form-inline" type="get" action="{{url('/search_work')}}" role="search" method="get">
<input id="search-input" type="search" name="query" class="form-control" placeholder="Search...">
<button type="submit" class="btn btn-primary"><i class="fa fa-search" aria-hidden="true"></i></button>
</form>
<br/>
<p style="text-align:center">
<button onclick="window.location.href='/workview'" type="button" class="btn btn-outline-primary">View</button><small style="color:#3490dc"> {{$count = DB::table('jobs')->count()}}</small> <small>Posts</small>
</p>
</div>
</div>
</div>
<div class="card">
<div class="card-header" id="headingThree">
<h5 class="mb-0">
<button class="btn btn-link collapsed" data-toggle="collapse" data-target="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
<i class="fa fa-product-hunt" aria-hidden="true"></i> Sell
</button>
</h5>
</div>
<div id="collapseThree" class="collapse" aria-labelledby="headingThree" data-parent="#accordion">
<div class="card-body">
<form class="form-inline" type="get" action="{{url('/search_sell')}}" role="search" method="get">
<input id="search-input" type="search" name="query" class="form-control" placeholder="Search...">
<button type="submit" class="btn btn-primary"><i class="fa fa-search" aria-hidden="true"></i></button>
</form>
<br/>
<p style="text-align:center">
<button onclick="window.location.href='/sellview'" type="button" class="btn btn-outline-primary">View</button><small style="color:#3490dc"> {{$count = DB::table('announces')->count()}}</small> <small>Posts</small>
</p>
</div>
</div>
</div>
<div class="card">
<div class="card-header" id="headingFour">
<h5 class="mb-0">
<button class="btn btn-link collapsed" data-toggle="collapse" data-target="#collapseFour" aria-expanded="false" aria-controls="collapseFour">
<i class="fa fa-product-hunt" aria-hidden="true"></i> Tenders
</button>
</h5>
</div>
<div id="collapseFour" class="collapse" aria-labelledby="headingFour" data-parent="#accordion">
<div class="card-body">
<form class="form-inline" type="get" action="{{url('/search_tender')}}" role="search" method="get">
<input id="search-input" type="search" name="query" class="form-control" placeholder="Search...">
<button type="submit" class="btn btn-primary"><i class="fa fa-search" aria-hidden="true"></i></button>
</form>
<br/>
<p style="text-align:center">
<button onclick="window.location.href='/tenderview'" type="button" class="btn btn-outline-primary">View</button><small style="color:#3490dc"> {{$count = DB::table('Kinds')->count()}} </small><small>Posts</small>
</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<br/>
#include('footer')
#endsection
My controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\User;
use DB;
class PostController extends Controller
{
//
public function mapost()
{
$data = Auth::user()->Announce::orderBy('created_at', 'DESC')->get();
return view('users.mapost',['data'=>$data]);
}
}
My route in web.php
Route::get('users/mapost', 'PostController#mapost')->name('users.mapost');
I don`t know where I am getting this wrong, i will appreciate any help thanks in advance
Look at this line:
$data = Auth::user()->Announce::orderBy('created_at', 'DESC')->get();
Here you attempt to call the orderBy method of the Auth::user()->Announce class and of course, such class does not exist. It would make sense to do something like this:
Announce::where('user_id', $youruserid)->orderByDesc('created_at');

Why is appering a pagination error after click in the search button?

I have a page "https://proj.test/user/profile?user=2#myRegistrations" where I have two tabs, one tab show the past registrations of a user in conferences and other tab the next registrations of the user in a conference. The user can click "Past Registrations" to check the past registrations and click in "Next Registrations" to check his next registrations in conferences. And its working.
In this same page, above the tabs, I have a form for the user to search for a conference name where he his registered. But when the user enters some text in the form search input like "test" and click in "Search" it appears "Undefined property: Illuminate\Pagination\LengthAwarePaginator::$participants (View: /Users/john/projects/proj/resources/views/users/index.blade.php)". Do you know what can be the issue?
Code in he view:
<div class="tab-pane clearfix fade" id="myTickets" role="tabpanel" aria-labelledby="contact-tab">
<div class="d-flex justify-content-between">
<form method="get" class="clearfix w-75" method="POST" action="{{ route('user.searchRegistration') }}">
{{ csrf_field() }}
<div class="form-group col col-md-6 px-0">
<div class="input-group" data-provide="datepicker">
<input type='text' class="form-control" placeholder="Search Registrations"
name="search_registration"
value="{{old('search_registration')}}"/>
<button class="input-group-addon">Search</button>
</div>
</div>
</form>
<a href="{{route('user.cleanSearchRegistration')}}" class="btn btn-outline-primary"
id="cleanSearchRegistration">Clean Search</a>
</div>
#if(session('searchedRegistrations'))
#if(session('searchedRegistrations')->count() != null)
#foreach(session('searchedRegistrations') as $registration)
#foreach(session('searchedRegistrations')->participants as $participant)
#if(!empty($registration))
<li class="list-group-item">
<h5>{{optional($registration->conference)->name}}</h5>
#if ($registration->status == 'C')
<a href="{{route('conference.registrationInfo',
['id' => $registration->conference->id,
'slug' => $registration->conference->slug,
'regID'=> $registration->id])}}"
class="btn btn-primary ml-2"><i class="fa fa-file-pdf-o"></i> Registration document
</a>
#endif
#if ($participant->registration_type->certificate_available == 'Y')
<a href="{{route('conferences.certificateInfo',
[
'regID'=> $registration->id])}}"
class="btn btn-primary ml-2"><i class="fa fa-file-pdf-o"></i> Download certificate
</a>
#break
#endif
</li>
#endif
#endforeach
#endforeach
<div class="text-center d-flex justify-content-center mt-3">
{{session('searchedRegistrations')->fragment('searchRegistrations')->links("pagination::bootstrap-4")}}
</div>
#else
<div class="alert alert-info" role="alert">
<i class="material-icons">info</i>
<span>Your search didnt return any result.</span>
</div>
#endif
#else
<div class="d-flex mb-3">
<ul class="nav nav-pills">
<li class="nav-item">
<a class="nav-link active border" href="#nextConferences" data-toggle="tab" role="tab">Next
Conferences</a>
</li>
<li class="nav-item">
<a class="nav-link border" href="#PastRegistrations" data-toggle="tab" role="tab">Past
Conferences</a>
</li>
</ul>
</div>
<div class="tab-content" id="myTabContent">
<div class="tab-pane fade show active clearfix" id="nextConferences" role="tabpanel"
aria-labelledby="home-tab">
<ul class="list-group">
#foreach($nextRegistrations as $nextRegistration)
#foreach($nextRegistration->participants as $participant)
#if(!empty($nextRegistration->conference) || !empty($nextRegistration->conference->start_date))
<li class="list-group-item">
<h5>{{optional($nextRegistration->conference)->name}}</h5>
#if ($participant->registration_type->certificate_available == 'Y')
<a href="{{route('conferences.certificateInfo',
[
'regID'=> $nextRegistration->id])}}"
class="btn btn-primary ml-2"><i
class="fa fa-file-pdf-o"></i> Download Certificate</a>
#break
#endif
#if ($nextRegistration->status == 'C')
<a href="{{route('conferences.registrationInfo',
[
'regID'=> $nextRegistration->id])}}"
class="btn btn-primary ml-2"><i
class="fa fa-file-pdf-o"></i> Registration document
</a>
#endif
</li>
#endif
#endforeach
#endforeach
</ul>
<div class="text-center d-flex justify-content-center mt-3">
{{$nextRegistrations->fragment('nextConferences')->links("pagination::bootstrap-4")}}
</div>
</div>
<div class="tab-pane fade show clearfix" id="pastConferences" role="tabpanel" aria-labelledby="home-tab">
<ul class="list-group">
#foreach($pastRegistrations as $pastRegistration)
#foreach($pastRegistration->participants as $participant)
#if(!empty($pastRegistration->conference) || !empty($pastRegistration->conference->start_date))
<li class="list-group-item">
<h5>{{optional($pastRegistration->conference)->name}}</h5>
#if ($participant->registration_type->certificate_available == 'Y')
<a href="{{route('conferences.certificateInfo',['regID'=> $pastRegistration->id])}}"
class="btn btn-primary ml-2"><i
class="fa fa-file-pdf-o"></i> Download Certificate
</a>
#break
#endif
#if ($pastRegistration->status == 'C')
<a href="{{route('conferences.registrationInfo',
[
'regID'=> $pastRegistration->id])}}"
class="btn btn-primary ml-2"><i
class="fa fa-file-pdf-o"></i> Registration info
</a>
#endif
</li>
#endif
#endforeach
#endforeach
</ul>
<div class="text-center d-flex justify-content-center mt-3">
{{$pastRegistrations->fragment('pastConferences')->links("pagination::bootstrap-4")}}
</div>
</div>
</div>
</div>
I assume session('searchedRegistrations') is the Pagination instance. There is no property named participants nor would there be any random properties (it is a container, not what it contains). I would imagine a model instance contained in the collection might have a participants property though.
#foreach(session('searchedRegistrations') as $registration)
#foreach(session('searchedRegistrations')->participants as $participant)
I am guessing that should be
#foreach($registration->participants as $participant)

How to Show 1 plan in laravel?

i have a page "deposits" here are plans are availble with the use of "foreach"
now i wnt to show only plan number 3 here. mean plan of id 3.
<div class="row">
#foreach($plan as $p)
<div class="col-sm-4 text-center">
<div class="panel panel-info panel-pricing">
<div class="panel-heading">
<h3 style="font-size: 28px;"><b>{{ $p->name }}</b></h3>
</div>
<div style="font-size: 18px;padding: 18px;" class="panel-body text-center">
<p><strong>{{ $p->minimum }} {{ $basic->currency }} - {{ $p->maximum }} {{ $basic->currency }}</strong></p>
</div>
<ul style='font-size: 15px;' class="list-group text-center bold">
<li class="list-group-item"><i class="fa fa-check"></i> Commission - {{ $p->percent }} <i class="fa fa-percent"></i> </li>
<li class="list-group-item"><i class="fa fa-check"></i> Time - {{ $p->time }} times </li>
<li class="list-group-item"><i class="fa fa-check"></i> Compound - <span class="aaaa">{{ $p->compound->name }}</span></li>
</ul>
<div class="panel-footer" style="overflow: hidden">
<div class="col-sm-12">
<button type="button" class="btn btn-info btn-block btn-icon icon-left delete_button"
data-toggle="modal" data-target="#DelModal"
data-id="{{ $p->id }}">
<i class="fa fa-send"></i> Invest Under This Package
</button>
</div>
</div>
</div>
</div>
#endforeach
</div>
You can simply extract the plan from your $plan collection , does not need loop in below case
$p = $plan ->find(3);
if you want to use foreach and show plan id 3 , then try this :
<div class="row">
#foreach($plan as $p)
#if($p == 3)
<div class="col-sm-4 text-center">
<div class="panel panel-info panel-pricing">
<div class="panel-heading">
<h3 style="font-size: 28px;"><b>{{ $p->name }}</b></h3>
</div>
<div style="font-size: 18px;padding: 18px;" class="panel-body text-center">
<p><strong>{{ $p->minimum }} {{ $basic->currency }} - {{ $p->maximum }} {{ $basic->currency }}</strong></p>
</div>
<ul style='font-size: 15px;' class="list-group text-center bold">
<li class="list-group-item"><i class="fa fa-check"></i> Commission - {{ $p->percent }} <i class="fa fa-percent"></i> </li>
<li class="list-group-item"><i class="fa fa-check"></i> Time - {{ $p->time }} times </li>
<li class="list-group-item"><i class="fa fa-check"></i> Compound - <span class="aaaa">{{ $p->compound->name }}</span></li>
</ul>
<div class="panel-footer" style="overflow: hidden">
<div class="col-sm-12">
<button type="button" class="btn btn-info btn-block btn-icon icon-left delete_button"
data-toggle="modal" data-target="#DelModal"
data-id="{{ $p->id }}">
<i class="fa fa-send"></i> Invest Under This Package
</button>
</div>
</div>
</div>
</div>
#endif
#endforeach
</div>
But i suggest not to use foreach as you already know you need to show only plan id 3 . Try this :
<div class="row">
<?php $p = $plan["your_desired_id"]; ?> // means 2 or 3 .. whatever your desired id
<div class="col-sm-4 text-center">
<div class="panel panel-info panel-pricing">
<div class="panel-heading">
<h3 style="font-size: 28px;"><b>{{ $p->name }}</b></h3>
</div>
<div style="font-size: 18px;padding: 18px;" class="panel-body text-center">
<p><strong>{{ $p->minimum }} {{ $basic->currency }} - {{ $p->maximum }} {{ $basic->currency }}</strong></p>
</div>
<ul style='font-size: 15px;' class="list-group text-center bold">
<li class="list-group-item"><i class="fa fa-check"></i> Commission - {{ $p->percent }} <i class="fa fa-percent"></i> </li>
<li class="list-group-item"><i class="fa fa-check"></i> Time - {{ $p->time }} times </li>
<li class="list-group-item"><i class="fa fa-check"></i> Compound - <span class="aaaa">{{ $p->compound->name }}</span></li>
</ul>
<div class="panel-footer" style="overflow: hidden">
<div class="col-sm-12">
<button type="button" class="btn btn-info btn-block btn-icon icon-left delete_button"
data-toggle="modal" data-target="#DelModal"
data-id="{{ $p->id }}">
<i class="fa fa-send"></i> Invest Under This Package
</button>
</div>
</div>
</div>
</div>
</div>
If you need to find out a single value of plan from the collection .
You can just find it by it's plan id
$plan = Plan::find(3);
Write this in your controller and then pass it down to your view blade file by using with.
Example :
return view("blade file name")->with(['plan'=>$plan])
Later in your blade file just access it's fields.
{{$plan->field_name}}

Laravel: Change login layout error

I use the simple built-in php artisan make:auth authentication in laravel and I would like to change the default layout.
login.blade.php
#extends('layouts.default')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Login</div>
<div class="panel-body">
<form class="form-horizontal" role="form" method="POST" action="{{ route('login') }}">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label for="email" class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required autofocus>
#if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="password" class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control" name="password" required>
#if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<div class="checkbox">
<label>
<input type="checkbox" name="remember" {{ old('remember') ? 'checked' : '' }}> Remember Me
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-8 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Login
</button>
<a class="btn btn-link" href="{{ route('password.request') }}">
Forgot Your Password?
</a>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
default.blade.php
<!DOCTYPE html>
<html>
<head>
#include('includes.head')
</head>
<body>
#include('includes.sidebar')
<div id="main" class="row">
<div class = "content">
#yield('content')
</div>
</div>
<script src="full_calendar/lib/jquery-ui.js"></script>
<script src='full_calendar/lib/moment.min.js'></script>
<script src='js/bootstrap.min.js'></script>
<script src='js/sidebar.js'></script>
</body>
</html>
sidebar.blade.php
<nav class="navbar navbar-inverse sidebar" role="navigation">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-sidebar-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">{{ Auth::user()->name }}</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-sidebar-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active">Home<span style="font-size:16px;" class="pull-right hidden-xs showopacity glyphicon glyphicon-home"></span></li>
<li >Calendar->employees<span style="font-size:16px;" class="pull-right hidden-xs showopacity glyphicon glyphicon-user"></span></li>
<li >Calendar->employers<span style="font-size:16px;" class="pull-right hidden-xs showopacity glyphicon glyphicon-user"></span></li>
<li >Register<span style="font-size:16px;" class="pull-right hidden-xs showopacity glyphicon glyphicon-envelope"></span></li>
<li >Log out<span style="font-size:16px;" class="pull-right hidden-xs showopacity glyphicon glyphicon-envelope"></span></li>
<li >About<span style="font-size:16px;" class="pull-right hidden-xs showopacity glyphicon glyphicon-logout"></span></li>
<li class="dropdown">
Settings <span class="caret"></span><span style="font-size:16px;" class="pull-right hidden-xs showopacity glyphicon glyphicon-cog"></span>
<ul class="dropdown-menu forAnimate" role="menu">
<li>Action</li>
<li>Another action</li>
<li>Something else here</li>
<li class="divider"></li>
<li>Separated link</li>
<li class="divider"></li>
<li>One more separated link</li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
Error message
ErrorException in 23932780504d5d1cb3d219486de49ace0b7f23c9.php line
11: Trying to get property of non-object (View:
C:\xampp\htdocs\laravel\resources\views\includes\sidebar.blade.php)
(View:
C:\xampp\htdocs\laravel\resources\views\includes\sidebar.blade.php)
(View:
C:\xampp\htdocs\laravel\resources\views\includes\sidebar.blade.php)
Why does this exception occur?
The issue in your code could be Auth::user()->name try dumping the value of Auth::user() (code is dd(Auth::user())) if it shows null that's the issue.
and in below code
<li >Calendar->employees<span style="font-size:16px;" class="pull-right hidden-xs showopacity glyphicon glyphicon-user"></span></li>
<li >Calendar->employers<span style="font-size:16px;" class="pull-right hidden-xs showopacity glyphicon glyphicon-user"></span></li>
Instead of Calendar->employees you should write {{ $Calendar->employees }} and same for Calendar->employers instead you should write {{ $Calendar->employers }} without {{...}} or {!!...!!} it will not print the variable values.

Laravel paginator doesn't work properly

The laravel paginator does work, but not good in my case.
So I have a forum with comments and the paginator must display when there are more than 10 comments on that page.
So I have a topic that has more than 10 comments and then it works fine on that page, but when I go to another page, he does take the pagination of the most comments. That is not what I want.
Here are some screens and code:
Here are there more than 10 comments, so it is good that it displays the paginator. The strange thing here is that it only shows 2 comments and not 10.
Here is another topic with just 8 comments, so the pagination shouldn't display anything:
But as you can see, it does show the paginator....
My code look like this:
Controller:
public function thread($title)
{
$thread = Thread::where('title', '=', str_replace('Thread-', '',str_replace('-', ' ', $title)))->first();
$usercolor = DB::table('threads')
->join('user', 'threads.uid', '=', 'user.id')
->join('role', 'user.role_id', '=', 'role.id')
->where('threads.uid', '=', $thread->uid)
->first();
$usertitle = DB::table('threads')
->join('user', 'threads.uid', '=', 'user.id')
->where('threads.uid', '=', $thread->uid)
->first();
$comments = Comment::orderBy('posted_at', 'asc')
->where('deleted_at', '=', '0000-00-00 00:00:00')
->with('user.role')
->paginate(10);
if (!$thread) {
return die('bestaat niet!');
}else{
return View::make('thread')->with('threads', $thread)
->with('comments', $comments)
->with('usercolor', $usercolor)
->with('usertitle', $usertitle);
}
}
View:
#include('globs.header')
<div class="col-sm-offset-1 col-sm-10">
<div class="clearfix"> </div>
#if(Request::get('page', 1) == 1)
<div class="panel panel-default" id="a9">
<div class="panel-heading">
<div class="panel-title">{{ $threads->title }}
</div>
</div>
<div class="panel-body thread-row">
<div class="row thread-row">
<div class="col-md-2 col-sm-3 text-center userblock">
<div class="clerafix"> </div>
<img class="img-thumbnail avatar" src="./public/img/avatar.jpg" alt="Avatar" height="55px;" width="55px">
<div class="push_bottom_5" style="color:{{ $usercolor->colour }};"><strong>{{ $threads->username }}</strong></div>
<div class="push_bottom_5"><small>{{ $usertitle->usertitle }}</small></div>
<div class="label label-default" style="background-color:{{ $usercolor->colour }};">{{ $usercolor->name }}</div>
<div class="push_bottom_5">
#if($usercolor->id == '1')
<i class="fa fa-star"></i>
#elseif($usercolor->id == '2')
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
#elseif($usercolor->id == '3')
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
#elseif($usercolor->id == '4')
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
#elseif($usercolor->id == '5')
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
#elseif($usercolor->id >= '6')
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
#endif
</div>
<div class="text-muted text-left"><small>Total Posts: 66</small></div>
<div class="text-muted text-left"><small>Joined 05-11-2014</small></div>
<div class="clerafix"> </div>
</div>
<div class="col-md-5 col-sm-6 col-xs-8">
<div class="clerafix"> </div>
<div class="text-muted"><span><i class="fa fa-calendar"></i> </span><small>
<span>Posted:</span> {{ date("d-m-Y", strtotime($threads->date_posted)) }}</small></div>
</div>
<div class="col-md-10 col-sm-9 col-xs-12">
<div class="clerafix"> </div>
<div class="content_body"><p>{{ Helper::Filter(BBCode::parse($threads->message)) }}</p>
</div>
<div class="clerafix"> </div>
</div>
</div>
</div>
<div class="panel-footer clearfix">
<div class="pull-right">
<!-- acties -->
<button class="btn btn-primary btn-sm"><i class="fa fa-edit"></i> Aanpassen</button>
<button class="btn btn-primary btn-sm"><i class="fa fa-quote-left"></i> Quote</button>
<button class="btn btn-danger btn-sm"><i class="fa fa-trash"></i> Verwijder</button>
<button class="btn btn-warning btn-sm"><i class="fa fa-exclamation"></i> Rapporteer</button>
</div>
</div>
</div>
<!-- end of the topic -->
</div>
#endif
<!-- begin of comments -->
#foreach($comments as $comment)
#if($comment->tid == $threads->tid)
<div class="col-sm-offset-1 col-sm-10">
<div class="clearfix"> </div>
<div class="panel panel-default" id="a9">
<div class="panel-body thread-row">
<div class="row thread-row">
<div class="col-md-2 col-sm-3 text-center userblock">
<div class="clerafix"> </div>
<img class="img-thumbnail avatar" src="./public/img/avatar.jpg" alt="Avatar" height="55px;" width="55px">
<div class="push_bottom_5" style="color:{{ $comment->user->role->colour }};"><strong>{{ $comment->username }}</strong></div>
<div class="push_bottom_5"><small>{{ $comment->user->usertitle }}</small></div>
<div class="label label-default" style="background-color:{{ $comment->user->role->colour }};">{{ $comment->user->role->name }}</div>
<div class="push_bottom_5">
#if($comment->user->role->id == '1')
<i class="fa fa-star"></i>
#elseif($comment->user->role->id == '2')
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
#elseif($comment->user->role->id == '3')
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
#elseif($comment->user->role->id == '4')
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
#elseif($comment->user->role->id == '5')
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
#elseif($comment->user->role->id >= '6')
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
#endif
</div>
<div class="text-muted text-left"><small>Total Posts: 66</small></div>
<div class="text-muted text-left"><small>Joined 05-11-2014</small></div>
<div class="clerafix"> </div>
</div>
<div class="col-md-5 col-sm-6 col-xs-8">
<div class="clerafix"> </div>
<div class="text-muted"><span class="hidden-md "><i class="fa fa-calendar"></i>
</span><small><span>Posted:</span> {{ date("d-m-Y", strtotime($comment->posted_at)) }}</small></div>
</div>
<div class="col-md-10 col-sm-9 col-xs-12">
<div class="clerafix"> </div>
<div class="content_body"><p>{{ Helper::Filter(BBCode::parse($comment->comment)) }}</p>
</div>
<div class="clerafix"> </div>
</div>
</div>
</div>
<div class="panel-footer clearfix">
<div class="pull-right">
<!-- acties -->
<button class="btn btn-primary btn-sm"><i class="fa fa-edit"></i> Aanpassen</button>
<button class="btn btn-primary btn-sm"><i class="fa fa-quote-left"></i> Quote</button>
<button class="btn btn-danger btn-sm"><i class="fa fa-trash"></i> Verwijder</button>
<button class="btn btn-warning btn-sm"><i class="fa fa-exclamation"></i> Rapporteer</button>
</div>
</div>
</div>
</div>
#endif
#endforeach
<!-- end of comments -->
#if(Auth::check())
<div class="col-sm-offset-1 col-sm-10">
<div class="clearfix"> </div>
<div class="panel panel-default" id="a9">
<div class="panel-heading"><h4>Snelle reactie plaatsen</h4></div>
<div class="panel-body">
{{ Form::open(array('url' => 'SubmitComment')) }}
<input type="hidden" value="{{ $threads->tid }}" name="tid">
{{ Honeypot::generate('spamprt', 'time') }}
<textarea name="comment" class="form-control" col="250" rows="5" placeholder="Typ uw bericht..."></textarea>
<div class="clearfix"> </div>
<button class="btn btn-success pull-left" type="submit" name="action">
Reageer
</button>
{{ Form::close() }}
</div>
</div>
</div>
#endif
<div class="col-md-4 col-md-offset-5">{{ $comments->links() }}</div>
</div>
</div>
#include('globs.footer')
I hope you guys will understand my problem and are be able to find a perfect solution with me.
It looks like you always retrieve all comments. I can also see this in your template, where you wrap your code in #if($comment->tid == $threads->tid) within your foreach.
This means that later in your template, where you retrieve the pagination links, it will retrieve the links for all comments, but not all comments are shown in the page, since you filter them manually in the loop.
Query the comments like
$comments = Comment::orderBy('posted_at', 'asc')
->where('deleted_at', '=', '0000-00-00 00:00:00')
->where('tid', $threads->tid) // it's about this line!
->with('user.role')
->paginate(10);
Note:
Above should provide a solution for your problem, but as an extra, you could also look into relationships. As an addition, this answer on SO "Laravel 4.1: How to paginate eloquent eager relationship?" (Since you tagged your post with Laravel-4) has some nice arguments on how to approach this efficiently.

Categories