Laravel Live Search Results Display Issue on the View - php

I'm trying to implement a live search in my laravel application.
Following is my index function in the controller (ParticipantController.php). Here if there is no search happens, then displaying the participants by default.
public function index(Request $request)
{
if($request->ajax()){
$data = User::orderBy('id','DESC')
->WHERE('role_id','=','3')
->where('email','LIKE','%'.$request->search."%")
->paginate(12);
return view('admins.participants.index',compact('data'))
->with('i', ($request->input('page', 1) - 1) * 12 );
}
else{
$data = User::orderBy('id','DESC')->WHERE('role_id','=','3')->paginate(12);
return view('admins.participants.index',compact('data'))
->with('i', ($request->input('page', 1) - 1) * 12 );
}
}
following is my view (only included the relevant part)
<div class="row mt-5 ">
<div class="col-md-12 mb-2 text-right">
<input type="text" class="form-controller" id="search" name="search"></input>
</div>
</div>
<div class="row mt-2 participant-row">
#foreach ($data as $key => $user)
<div class="col-md-3 d-flex align-items-stretch mb-4">
<div class="white-panel w-100 ">
<div class="card p-2 ">
<div class="row no-gutters ">
<div class="col-auto my-auto">
#if(empty($user->image_id))
<img src="/propics/default-avatar.png" class="img_participant">
#else
<?php if(file_exists('propics/'.$user->image_id.'')){
echo '<img src="/propics/'.$user->image_id.'" class="img_participant">
';
}else{
echo '<img src="/propics/default-avatar.png" class="img_participant">
';
}?>
#endif
</div>
<div class="col my-auto">
<div class="card-block px-2">
<div class="text-right">
<a class="btn btn-default btn_icon" href="{{ route('participants.edit',$user->id) }}"><i class="fas fa-edit"></i></a>
{!! Form::open(['method' => 'DELETE','route' => ['participants.destroy', $user->id],'style'=>'display:inline']) !!}
{!! Form::button('<i class="fas fa-trash-alt"></i>', ['class' => 'btn btn-default btn_icon','type'=>'submit','onclick'=>'return confirm("Are you sure want to remove this User?")']) !!}
{!! Form::close() !!}
</div>
<hr>
<h4 class="card-title participant_name alas">{{ $user->first_name }} {{ $user->last_name }}</h4>
<p class="card-text participant_email alas">{{ $user->email }}</p>
</div>
</div>
</div>
</div>
</div>
</div>
#endforeach
</div>
And set my route as (web.php),
Route::resource('/admins/participants','Admin\ParticipantController');
Following is my Javascript which I included at the bottom of my view
<script type="text/javascript">
jQuery('#search').on('keyup',function(){
$value=jQuery(this).val();
$.ajax({
type : 'get',
url:"{{ route('participants.index') }}",
data:{'search':$value},
success:function(data){
jQuery('.participant-row').html(data);
}
});
})
</script>
Now the issue is when I search for something, as the result, the current code provides me a whole new view inside the old view. It loads a new view (with the menus and all) inside the target div class whichi mentioned in my javascript code.

Related

Larapoll results in admin panel

Hello i'm using inani larapoll it's works great but in admin panel i can't see results of voting i can see only question, how many answers it have and how many vote was added i want to create custom page in admin where will be results of current vote like it is on WEB
like this
question - > why its don't working
answer1 -> don't know <- this option has 10 votes
answer2 -> because i'm doing something wrong <- this option has 100 votes
i added route
Route::get('/results', ['uses' => 'PollManagerController#results', 'as' => 'poll.results']);
pollmanager controller
public function results(Poll $poll)
{
$total = $poll->votes->count();
$results = $poll->results()->grab();
$options = collect($results)->map(function ($result) use ($total){
return (object) [
'votes' => $result['votes'],
'percent' => $total === 0 ? 0 : ($result['votes'] / $total) * 100,
'name' => $result['option']->name
];
});
$question = $poll->question;
echo view(config('larapoll_config.results') ? config('larapoll_config.results') : 'larapoll::dashboard.results', compact('options', 'question'));
}
and i copied results blade to my new blade
#if(Session::has('errors'))
<div class="alert alert-danger">
{{ session('errors') }}
</div>
#endif
#if(Session::has('success'))
<div class="alert alert-success">
{{ session('success') }}
</div>
#endif
<div class="w-80 m-auto">
<div class="font-size-14 font-medium-caps text-blue-100">{{ $question }}</div>
#foreach($options as $option)
<div class="mt-20px">
<div class="font-size-14 font-size-xs-12 text-blue-100">{{ $option->name }}</div>
<div class="w-100 d-flex justify-content-between pb-5px">
<div class="w-100 d-flex align-items-center">
<div class="w-100 bg-lighter-gray h-4px position-relative">
<div class="position-absolute top-0 bg-yellow h-4px" style='width: {{ $option->percent }}%'> </div>
</div>
</div>
<div class="font-size-14 font-size-xs-12 text-blue-100 pl-10px">{{ $option->percent }}%</div>
</div>
</div>
#endforeach
<div class="d-flex justify-content-between mt-auto pt-30px align-items-center">
<div class="d-flex">
<span class="fas fa-calendar text-light-gray pr-5px"></span>
<p class="mr-20px font-medium font-size-12 text-light-gray"></p>
</div>
<div class="d-flex align-items-center text-light-black flex-end font-base font-size-14 font-size-xs-12"></div>
</div>
</div>
but's its showing nothing... empty

laravel error: Trying to get property of non-object (\show.blade.php)

I'm making a discussion forum app with laravel and i face this error:
Trying to get property of non-object (View:
C:\xampp\htdocs\fourm\resources\views\discussion\show.blade.php) at
PhpEngine->evaluatePath('C:\xampp\htdocs\fourm\storage\framework\views/a2f2c494b8859e0552bfa22c40ceb4065b2efbe5.php',
array('__env' => object(Factory), 'app' => object(Application),
'channels' => object(Collection), 'errors' => object(ViewErrorBag),
'd' => null, 'best_answer' => null))in CompilerEngine.php (line 59)
This is my controller code:
public function show($slug)
{
$discussion = Discussion::where('slug', $slug)->first();
$best_answer = Reply::where('best_answer', 1)->first();
return view('discussion.show')->with('d', $discussion)->with('best_answer', $best_answer);
}
This is my view code:
#extends('layouts.app')
#section('content')
<div class="panel panel-default">
<div class="panel-heading">
<img src="{{ $d->user->avatar }}" alt="" width="40px" height="40px">
<span>{{ $d->user->name }}, <b>( {{ $d->user->points }} )</b></span>
#if($d->is_being_watch_by_user())
unwatch
#else
watch
#endif
</div>
<div class="panel-body">
<h4 class="text-center">
<b>{{ $d->title }}</b>
</h4>
<hr>
<p class="text-center">
{{ $d->content }}
</p>
<hr>
#if($best_answer)
<div class="text-center" style="padding: 40px;">
<h3 class="text-center">BEST ANSWER</h3>
<div class="panel panel-success">
<div class="panel-heading">
<img src="{{ $best_answer->user->avatar }}" alt="" width="40px" height="40px">
<span>{{ $best_answer->user->name }} <b>( {{ $best_answer->user->points }} )</b></span>
</div>
<div class="panel-body">
{{ $best_answer->content }}
</div>
</div>
</div>
#endif
</div>
<div class="panel-footer">
<span>
{{ $d->replies->count() }} Replies
</span>
{{ $d->channel->title }}
</div>
</div>
#foreach($d->replies as $r)
<div class="panel panel-default">
<div class="panel-heading">
<img src="{{ $r->user->avatar }}" alt="" width="40px" height="40px">
<span>{{ $r->user->name }} <b>( {{ $r->user->points }} )</b></span>
#if(!$best_answer)
#if(Auth::id() == $d->user->id)
Mark as best answer
#endif
#endif
</div>
<div class="panel-body">
<p class="text-center">
{{ $r->content }}
</p>
</div>
<div class="panel-footer">
#if($r->is_liked_by_user())
Unlike <span class="badge">{{ $r->likes->count() }}</span>
#else
Like <span class="badge">{{ $r->likes->count() }}</span>
#endif
</div>
</div>
#endforeach
<div class="panel panel-default">
<div class="panel-body">
#if(Auth::check())
<form action="{{ route('discussion.reply', ['id' => $d->id ]) }}" method="post">
{{ csrf_field() }}
<div class="form-group">
<label for="reply">Leave a reply...</label>
<textarea name="reply" id="reply" cols="30" rows="10" class="form-control"></textarea>
</div>
<div class="form-group">
<button class="btn pull-right">Leave a reply</button>
</div>
</form>
#else
<div class="text-center">
<h2>Sign in to leave a reply</h2>
</div>
#endif
</div>
</div>
#endsection
You're getting this error because both queries return null:
$discussion = Discussion::where('slug', $slug)->first();
$best_answer = Reply::where('best_answer', 1)->first();
So, you need to check for empty result manually with is_null() or empty() or just:
if (!$discussion) {
abort(404);
}
Or use findOrFail() to throw an exception if no record found.
You are trying to access property with null objects, and that's why you got the error like "Trying to get property of null object".
As per our discussion in comments, you are getting null values in both queries and that's the reason for error!
Try to add if..else condition like below:
if (!$d) {
session()->flash('error', 'Discussion not found.');
return redirect()->back();
} else {
return view('discussion.show')->with('d', $discussion)->with('best_answer', $best_answer);
}
Hope this helps you!
Make sure you're not returning an object and accessing it as an array. It can be a common error.
I solved mine by adding: <?php error_reporting(0);?> on page where i got error. :D

How to link comments to each post by id with Laravel Blade

I have created a Post and Comment application. I am trying to link the comments to the id of the post. I am trying to use PHP in a Blade template to do this. I get the error that the variable post_id does not exist. I want to use the #if and #foreach of Blade. The problem seems to be in the #if statement.
Here is my HTML:
<div class="col-md-9">
<div class="row">
#foreach($posts as $post)
<div class="col-md-12 post" id="post{{ $post->id }}">
<div class="row">
<div class="col-md-12">
<h4>
{{ $post->title }}
</h4>
</div>
</div>
<div class="row">
<div class="col-md-12 post-header-line">
<span class="glyphicon glyphicon-user"></span> by {{ $post->user->firstName }} {{ $post->user->lastName }} | <span class="glyphicon glyphicon-calendar">
</span> {{ $post->created_at }} | <span class="glyphicon glyphicon-comment"></span><a href="#">
3 Comments</a>
</div>
</div>
<div class="row post-content">
<div class="col-md-2">
<a href="#">
<img src="/images/random/postimg.jpg" alt="" class="img-responsive postImg">
</a>
</div>
<div class="col-md-10">
<p>
{{ $post->post }}
</p>
</div>
</div>
<div class="row add-comment">
<div class="form-group">
<input type="text" name="addComment" placeholder="Add your comment" class="form-control" v-model="comment">
</div>
<input id="addComment" type="submit" name="submitComment" value="Submit Comment" class="btn btn-default" v-on:click="submitComment" data-id="{{ $post->id }}">
</div>
#if($post->comment->post_id == $post->id)
#foreach($post->comment as $comment)
<div class="row">
<div class="col-md-12 mb-r">
<div class="card example-1 scrollbar-ripe-malinka">
<div class="card-body">
<h4 id="section1"><strong>By: {{ $comment->user->firstName }} {{ $comment->user->lastName }}</strong></h4>
<p>{{ $comment->comment }}</p>
</div>
</div>
</div>
</div>
#endforeach
#endif
</div>
#endforeach
</div>
</div>
Here is my PostController:
public function index(){
$posts = Post::with('comment', 'user')->orderBy('id', 'desc')->limit(20)->get();
return view('post.posts')->with('posts', $posts);
}
You don't need #if statement, remove it. $post->comment is a collection, that 's why your are not getting post_id. if you want to check, then check inside foreach.
#foreach($post->comment as $comment)
<div class="row">
<div class="col-md-12 mb-r">
<div class="card example-1 scrollbar-ripe-malinka">
<div class="card-body">
<h4 id="section1"><strong>By: {{ $comment->user->firstName }} {{ $comment->user->lastName }}</strong></h4>
<p>{{ $comment->comment }}</p>
</div>
</div>
</div>
</div>
#endforeach
You need to add post_id to the comments table and use the relationship:
public function comments()
{
return $this->hasMany(Comment::class);
}
Then you'll be able to load related comments:
Post::with('comments', ....
And iterate over posts and their comments:
#foreach ($posts as $post)
#foreach ($post->comments as $comment)
{{ $comment->content }}
#endforeach
#endforeach
I think if you create relationship between Post model and Comment model (post table and comments table), your #if is useless. I recommend you to check your corresponding models.
Your code should be like this,
Post.php
public function comments(){
return $this->morphMany('App\Comments','commentable');
}
Comment.php
public function commentable(){
return $this->morphTo();
}
in my app Comment model is shared by some other models also.
Eg :- User’s Questions also have comments.
Find more details in laravel:Eloquent page

search result issue in laravel 5.4

I have search box which is work and show data with pagination but when i search for example for word sample which i have 3 posts with that title in first page of results I'm getting results like this:
https://ibb.co/hGGxwQ
But when i go to second page everything changes!
https://ibb.co/eZJMO5
This is my searchcontroller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
use Illuminate\Support\Facades\Input;
use Carbon\Carbon;
class SearchController extends Controller
{
public function index() {
$countTodayOrders = Post::whereRaw('Date(created_at) = CURDATE()')->count();
$yesterday_posts = Post::whereRaw('Date(created_at) = DATE_ADD(CURDATE(), INTERVAL -1 DAY)')->count();
$weekly_posts = Post::whereBetween( 'updated_at', [Carbon::today()->startOfWeek(), Carbon::today()->endOfWeek()] )->count();
$monthy_posts = Post::whereRaw('MONTH(created_at) = ?', Carbon::today()->month)->count();
$q = Input::get('q');
$posts = Post::where('title','LIKE','%'.$q.'%')->orWhere('body','LIKE','%'.$q.'%')->paginate('2');
$resultcount = Post::where('title','LIKE','%'.$q.'%')->orWhere('slug','LIKE','%'.$q.'%')->count();
return view('theme.search', compact('posts', 'resultcount', 'countTodayOrders', 'yesterday_posts', 'weekly_posts', 'monthy_posts', 'q'));
}
}
This is my search.Blade
#extends('layout.app')
#section('title')
Search results
#endsection
#section('content')
<div class="col-md-9 technology-left">
<div class="tc-ch wow fadeInDown" data-wow-duration=".8s" data-wow-delay=".2s">
<h2> Here is <b> {{ $resultcount }} </b> result for your query.</h2>
<hr/>
<!-- panel group -->
<div class="row form-group">
#foreach($posts as $post)
<div class="col-xs-12 col-md-6">
<div class="panel panel-default">
<div class="panel-image hide-panel-body">
<img src="{{ url('/storage') }}/{{ $post->image }}" class="img-responsive panel-image-preview" alt="{{ $post->title }}" />
</div>
<div class="panel-body">
<h4>{{ $post->title }}</h4>
<p>{{ substr(strip_tags($post->body), 0, 50) }}{{ strlen(strip_tags($post->body)) > 50 ? "..." : "" }}</p>
</div>
<div class="panel-footer text-center">
<span class="glyphicon glyphicon-share-alt"></span>
</div>
</div>
</div>
#endforeach
</div>
<!-- panel group -->
<div class="row text-center">
<div class="col-md-12">
{!! $posts->links() !!}
</div>
</div>
</div>
</div>
#endsection
My route
Route::any('/search', ['uses' => 'SearchController#index', 'as' => 'search.index']);
My search box
<!-- search box -->
<form action="/search" method="post" role="search">
{{ csrf_field() }}
<div class="input-group">
<input type="text" class="form-control" name="q" placeholder="Search..."> <span class="input-group-btn">
<button type="submit" class="btn btn-default">
<span class="glyphicon glyphicon-search"></span>
</button>
</span>
</div>
</form>
<!-- search box -->
Why is that?
The problem is most probably with your q parameter not getting passed in the page links.
Just add it In your controller like this:
$q = Input::get('q');
$posts = null;
if($q) {
$posts = Post::where('title','LIKE','%'.$q.'%')
->orWhere('body','LIKE','%'.$q.'%')
->paginate(2)
->appends('q', $q);
}
In your blade you should add an if statement to check if there are any posts
#if($posts and $posts->count())
// show posts
#else
// show a nothing found message
#endif

Undefined property: Illuminate\Pagination\LengthAwarePaginator::$id (View: F:\project\resources\views\admin\carousels\index.blade.php)

when I try to pass variable data to view I get this error, I can't find any document about this problem
My controller(CarouselController.php)
public function index()
{
$carousels = Carousel::orderBy('created_at', 'asc')->paginate(12);
return view('admin.carousels.index')->withCarousels($carousels);
}
My view(index.blade.php)
<div class="row">
<div class="col-md-9">
<h1>All Images</h1>
</div>
<div class="col-md-3">
Create New carousel
</div>
<div class="col-md-12">
<hr>
</div>
</div>{{-- end of the row --}}
<div class="row">
<div class="col-md-12">
<div class="row">
#foreach($carousels as $photo)
<div class="col-xs-6 col-md-3">
{!! Form::open( array('route'=>array('carousels.destroy', $carousels->id),'method'=>'DELETE')) !!}
{!! Form::submit('Delete', array('class'=>"btn btn-danger btn-sm tours-delete tour-index-delete"))!!}
{!! Form::close() !!}
<a href="{{ url($photo->path) }}" class="thumbnail" data-lity>
<img class="img-responsive" src="{{ $photo->path }}" alt="">
</a>
</div>
#endforeach
</div>
</div>
<div class="text-center">
{!! $carousels->links(); !!}
</div>
</div>
You foreach loop contains the code:
$carousels->id;
which seems to be getting a single object from collection, which isn't the right way, you should try this:
#foreach($carousels as $carousel)
<div class="col-xs-6 col-md-3">
{!! Form::open( array('route'=>array('carousels.destroy', $carousel->id),'method'=>'DELETE')) !!}
{!! Form::submit('Delete', array('class'=>"btn btn-danger btn-sm tours-delete tour-index-delete"))!!}
{!! Form::close() !!}
<a href="{{ url($carousel->path) }}" class="thumbnail" data-lity>
<img class="img-responsive" src="{{ $carousel->path }}" alt="">
</a>
</div>
#endforeach
Hope this helps!

Categories