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
Related
I want to go to specific page after click show button but after click show will get this error Undefined offset: 1
web.php
Route::get('/product/{id}', 'ProductController#show')->name('product.show');
ProductController.php
public function show($id)
{
$product = Product::find($id);
return view('product.show')->with(compact('product'));
}
home.blade.php
<div class="row">
#foreach($products as $product)
<div class="col-lg-4 col-md-6 mb-4">
<div class="card h-100">
<img class="card-img-top" src="http://placehold.it/700x400" alt="">
<div class="card-body">
<h4 class="card-title">
{{ $product->name }}
</h4>
<h5>{{ $product->price }}</h5>
<p class="card-text">{{ $product->description }}</p>
<td><a class="btn btn-primary" href="{{ route('product.show', $product->id ) }}">Show</a></td>
</div>
<div class="card-footer">
<small class="text-muted">★ ★ ★ ★ ☆</small>
</div>
</div>
</div>
#endforeach
</div>
Your are doing wrong in blade file. Note here php array start from 0. So array is [0,1,2,3] and database id is start from 1.
this is the correct link
{{ route('product.show', ['Product' => $product->id]) }}
You have to use like below code
{{ route('product.show', ['id' => $product->id]) }}
return view('product.show', compact('product'));
And in your show.blade.php you have to use like below
{{$product->id}} or {{$product->name}} //etc //But not use foorloop
The find method will return only one item. You can't use the loop on it.
Try the following code.
web.php
Route::get('/product/{product}', 'ProductController#show')->name('product.show');
ProductController.php
public function show(Product $product)
{
return view('product.show')->with(compact('product'));
}
home.blade.php
<div class="row">
<div class="col-lg-4 col-md-6 mb-4">
<div class="card h-100">
<img class="card-img-top" src="http://placehold.it/700x400" alt="">
<div class="card-body">
<h4 class="card-title">
{{ $product->name }}
</h4>
<h5>{{ $product->price }}</h5>
<p class="card-text">{{ $product->description }}</p>
<td><a class="btn btn-primary" href="{{ route('product.show', $product->id ) }}">Show</a></td>
</div>
<div class="card-footer">
<small class="text-muted">★ ★ ★ ★ ☆</small>
</div>
</div>
</div>
</div>
you are in wrong blade
you are returning product.show blade in your controller:
return view('product.show')->with(compact('product'));
but you put home.blade code!!!
put the right blade code
show.php: Why not try it $_ Get["id"]
I have this code in my controller:
public function videoGallery()
{
$videoLinksRecord = VideoLinks::all()->sortBy('video_category_id')->groupBy('video_catetgory_id');
return view('video.gallery')->with('videoLinksRecord', $videoLinksRecord);
}
which return this query:
{
"1": [
{"id":1,"video_category_id":1,"name":"Laszlo - Supernova","link":"PKfxmFU3lWY","description":"one loud banger!","home_video":null,"created_at":"2020-12-31T14:12:13.000000Z","updated_at":"2020-12-31T14:12:13.000000Z","deleted_at":null},
{"id":2,"video_category_id":1,"name":"Subtact - Restart","link":"M5A46A2txC4","description":"one loud banger!","home_video":null,"created_at":"2020-12-31T14:12:13.000000Z","updated_at":"2020-12-31T14:12:13.000000Z","deleted_at":null},
{"id":3,"video_category_id":1,"name":"Sushi Killer & Kevin Villeco - Anime Bae","link":"YHHjfYxLyuA","description":"one loud banger!","home_video":null,"created_at":"2020-12-31T14:12:13.000000Z","updated_at":"2020-12-31T14:12:13.000000Z","deleted_at":null},
{"id":4,"video_category_id":1,"name":"Hyper Potions & Subtact - Adventures","link":"TKZUhs9Gcdo","description":"one loud banger!","home_video":null,"created_at":"2020-12-31T14:12:13.000000Z","updated_at":"2020-12-31T14:12:53.000000Z","deleted_at":null}
],
"2": [
{"id":5,"video_category_id":2,"name":"Laszlo - Supernova","link":"PKfxmFU3lWY","description":"one loud banger!","home_video":null,"created_at":"2020-12-31T14:12:13.000000Z","updated_at":"2020-12-31T14:12:13.000000Z","deleted_at":null},
{"id":6,"video_category_id":2,"name":"Subtact - Restart","link":"M5A46A2txC4","description":"one loud banger!","home_video":null,"created_at":"2020-12-31T14:12:13.000000Z","updated_at":"2020-12-31T14:12:13.000000Z","deleted_at":null},
{"id":7,"video_category_id":2,"name":"Sushi Killer & Kevin Villeco - Anime Bae","link":"YHHjfYxLyuA","description":"one loud banger!","home_video":null,"created_at":"2020-12-31T14:12:13.000000Z","updated_at":"2020-12-31T14:12:13.000000Z","deleted_at":null}
]
}
I want to display the result grouped by its video category similar to this blade:
{{-- name of the category --}}
<h2 class="poppins-medium text-lg text-darkergreen text-center">Video Category Name Here</h2>
<div class="container-fluid">
<div class="row">
<div class="col-4"></div>
<div class="col-4 borderline-darkergreen-md"></div>
<div class="col-4"></div>
</div>
</div>
{{-- loop for each result sharing the same "video_category_id" --}}
<div class="container-fluid">
<div class="row d-flex justify-content-center px-0">
<div class="col col-lg-2 d-none d-lg-block d-xl-none"></div>
<div class="col col-xl-2 d-none d-xl-block"></div>
{{-- video --}}
<div class="col-12 col-md-6 col-lg-4 container-fluid d-flex justify-content-center spacer-md">
<div class="row d-flex justify-content-center">
<div class="col-12 d-flex justify-content-center container-fluid px-0">
{{-- video border --}}
<div class="video-box-border position-relative spacer-md">
<a href="">
{{-- video card image --}}
<div class="video-image position-absolute"></div>
{{-- video card background --}}
<div class="video-card"></div>
</a>
</div>
</div>
<div class="container-fluid">
<div class="row">
<div class="col-2 px-0"></div>
<div class="col-8 borderline-darkergreen-sm px-0"></div>
<div class="col-2 px-0"></div>
</div>
</div>
{{-- video name--}}
<p class="poppins-normal text-md text-gray text-center">Video Name Here
</p>
</div>
</div>
</div>
</div>
How should I use the $videoLinksRecord in a foreach logic to display it by its groupBy logic? This is my first time grouping query result so I do not know how to construct the logic for the said desired result.
Edit the code below is the relationship code in the model:
VideoLinks Model:
public function categories()
{
return $this->hasMany(VideoCategories::class);
}
VideoCategories Model:
public function links()
{
return $this->belongsTo(VideoLinks::class);
}
#foreach ($videoLinksRecords as $video_category_id => $videoLinks)
{{ $video_category_id }}
#foreach ($videoLinks as $videoLink)
{{ $videoLink->id }}
{{ $videoLink->video_category_id }}
{{ $videoLink->name }}
{{ $videoLink->link }}
{{ $videoLink->description }}
{{ $videoLink->home_video }}
{{ $videoLink->created_at }}
{{ $videoLink->updated_at }}
{{ $videoLink->deleted_at }}
#endforeach
#endforeach
Your relationship between the VideoLinks and VideoCategory models should be as follows
# VideoLinks model
public function category()
{
// 2nd parameter is optional if you follow the naming conventions
return $this->belongsTo(VideoCategory::class, 'video_category_id');
}
In your query eager load the relationship using with('category') but then group by the category's name and finally replace all() with get().
$videoLinksRecord = VideoLinks::with('category')->get()->sortBy('video_category_id')->groupBy('category.name');
#foreach ($videoLinksRecords as $category_name => $videoLinks)
{{ $category_name }}
#foreach ($videoLinks as $videoLink)
...
I'm trying to create an "updatable" order list|table where the user can change one dropdown value of the list to "take in charge" all orders listed.
(PS: user will also have the possibility to filter the list in order to take in charge only specific orders | order-row checkboxes will be removed since not necessary).
I have created my controller (using not Eloquent but query builder to query the data).
I'm passing my data to my view.
I can view the expected records in the list|table.
User can choose his name into the first order row dropdown called "Prise en charge", then all other order rows dropdowns values are changed accordingly :
https://www.screencast.com/t/Ei1wdJtb
(link works with Mozilla and Chrome but not Opera)
Now I would like to update with submit all in the table listed orders.
I know how to create CRUDs, but I never created this kind of hybrids, so it is a little bit unclear (or completely honestly said) how to do it.
I would appreciate some expertise about what is missing to submit and update accordingly the database table columns.
CONTROLLER
<?php namespace App\Http\Controllers;
use Session;
use Request;
use DB;
use CRUDBooster;
Use SoftDelete;
class AdminLaboOrdersManagementController extends Controller {
public function updateOrder(Request $request) {
# QUERIES
# I haven't printed the queries since it is not relevant I guess and
# because it takes a lot of space. If needed I will of course provide ...
# SEND TO VIEW
return view('labo_orders_mgt_view_index',
compact('staffs','orders','statusorders'));
}
}
BLADE
Structure
row-id is the row id of the order into the table
<div class="order" row-id="1029">rows and data</div>
<div class="order" row-id="1039">rows and data</div>
<div class="order" row-id="1049">rows and data</div>
<div class="order" row-id="1059">rows and data</div>
<div class="order" row-id="1060">rows and data</div>
<div class="order" row-id="1062">rows and data</div>
Code
#extends('crudbooster::admin_template')
#section('content')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="{{ asset('assets/js/laboratory/takeCareOf.js')}}"></script>
<table class='table table-striped table-bordered'>
<tbody>
{{--dd($staffs)--}}
{{--dd($orders)--}}
<!-- FOREACH LOOP TO DISPLAY THE ORDERS -->
<form method="POST" action="" id="takecareof">
<div class='row button'>
<button type="submit" class="btn btn-primary btn-sm" id="btnFront" style="cursor: pointer;font-size:24px; height:47px;width:100%;"> Je confirme la prise en charge </button>
</div>
#foreach($orders as $order)
<div class="order" row-id="{{$order->rowID}}">
<div class="col-md-1 sub_chkbox">
<input type="checkbox" class="sub_chk" data-id="{{$order->rowID}}">
</div>
<div class="row">
<div class="col-md-2 takecareof">
<h5>Prise en charge par</h5>
<p>
<select name="dropdownlabostaffs" class="dropdownlabostaffs form-control">
#if (isset($staffs))
<option value="">Artisans</option>
#foreach ($staffs as $staff)
<option value="{{ $staff->staffID }}"{{ $staff->staffID == $order->ownerID ? 'selected' : '' }}>{{ $staff->staffName }}</option>
#endforeach
#endif
</select>
</p>
</div>
</div>
#if($order->FirstnameUser === NULL || $order->LastnameUser === NULL)
<div class="row">
<div class="col-md-2 ordernr">
<h5>Commande no</h5>
<p>
{{ $order->orderID }}
</p>
</div>
<div class="col-md-3 orderby">
<h5>Commandé par</h5>
<p>
#if(isset($order->FirstnameUser)) {{ $order->FirstnameUser }} #endif #if(isset($order->LastnameUser)) {{ $order->LastnameUser }} #endif
</p>
</div>
<div class="col-md-3 forcustomer">
<h5>Pour Client</h5>
<p>
#if(isset($order->FirstnameCustomer)) {{ $order->FirstnameCustomer }} #endif #if(isset($order->LastnameCustomer)) {{ $order->LastnameCustomer }} #endif
</p>
</div>
<div class="col-md-3 laboratory">
<h5>Laboratoire</h5>
<p>
{{ $order->Laboratory }}
</p>
</div>
</div>
#else
<div class="row">
<div class="col-md-2 ordernr">
<h5>Commande no</h5>
<p>
{{ $order->orderID }}
</p>
</div>
<div class="col-md-3 orderby">
<h5>Commandé par</h5>
<p>
#if(isset($order->FirstnameUser)) {{ $order->FirstnameUser }} #endif #if(isset($order->LastnameUser)) {{ $order->LastnameUser }} #endif
</p>
</div>
<div class="col-md-3 forcustomer">
<h5>Pour Client</h5>
<p>
#if(isset($order->FirstnameCustomer)) {{ $order->FirstnameCustomer }} #endif #if(isset($order->LastnameCustomer)) {{ $order->LastnameCustomer }} #endif
</p>
</div>
<div class="col-md-3 laboratory">
<h5>Laboratoire</h5>
<p>
{{ $order->Laboratory }}
</p>
</div>
</div>
#endif
<div class="row">
<div class="col-md-6 status">
<h5>Statut</h5>
<p>
<select name="statusorder" class="statusorder form-control">
#if (isset($statusorders))
<option value="">Status</option>
#foreach ($statusorders as $statusorder)
<option value="{{ $statusorder->statusorderID }}"{{ $statusorder->statusorderID == $order->StatusID ? 'selected' : '' }}>{{ $statusorder->statusorderName }}</option>
#endforeach
#endif
</select>
</p>
</div>
<div class="col-md-6 pickup">
<h5>Date | heure de retrait</h5>
<p>
{{ date('d-m-Y', strtotime($order->{'DeliveryDate'})) }} | 13:15
</p>
<p>
</p>
</div>
</div>
<div class="row">
<div class="col-md-2 category">
<h5>Catégorie</h5>
<p>
#if(isset($order->Type)) {{ $order->Type }} #endif
</p>
</div>
<div class="col-md-2 product">
<h5>Produit</h5>
<p>
#if(isset($order->Name)) {{ $order->Name }} #endif
</p>
</div>
#if($order->Type === 'Gâteaux' || $order->Type === 'Tarte' || $order->Type === 'Tarte-fine')
<div class="col-md-2 servings">
<h5># Personnes</h5>
<p>
#if(isset($order->Servings)) {{ $order->Servings }} #endif
</p>
</div>
#endif
#if($order->Type === 'Gâteaux' || $order->Type === 'Tarte' || $order->Type === 'Tarte-fine')
<div class="col-md-2 chocodeco">
<h5>Déco Choco</h5>
#if(strpos($order->DecoChocoFruits, 'souhaitée') !== false)
<p>Oui</p>
#else
<p>Non</p>
#endif
</div>
<div class="col-md-4 flowerdeco">
<h5>Déco Fleurs</h5>
#if(strpos($order->DecoSmallFlowers, 'souhaitée') !== false)
<p>Oui</p>
#else
<p>Non</p>
#endif
</div>
#endif
#if($order->Type === 'Pain-surprise')
<div class="col-md-2 weight">
<h5>Poids</h5>
<p>
#if(isset($order->Weight)) {{ $order->Weight }} #endif
</p>
</div>
<div class="col-md-4 colorribbon">
<h5>
Couleur Ruban
</h5>
<p>
#if(isset($order->RibbonColor)) {{ $order->RibbonColor }} #endif
</p>
</div>
#endif
#if($order->Type === 'Pain')
<div class="col-md-2 breadquantity">
<h5>
Quantité
</h5>
<p>
#if(isset($order->BreadQuantity)) {{ $order->BreadQuantity }} #endif
</p>
</div>
<div class="col-md-6 slicecut">
<h5>
Coupe
</h5>
#if(strpos($order->Cut, 'souhaitée') !== false)
<p>Oui</p>
#else
<p>Non</p>
#endif
</div>
#endif
#if($order->Type === 'Salé')
<div class="col-md-2 savouryquantity">
<h5>
Quantité
</h5>
<p>
#if(isset($order->SavouryQuantity)) {{ $order->SavouryQuantity }} #endif
</p>
</div>
<div class="col-md-6 savouryassortment">
<h5>
Assortiment
</h5>
<p>
#if(isset($order->CanapésAssortment)) {{ $order->CanapésAssortment }} #endif
#if(isset($order->MiniPuffsAssortment)) {{ $order->MiniPuffsAssortment }} #endif
#if(isset($order->VolsAuVentAssortment)) {{ $order->VolsAuVentAssortment }} #endif
#if(isset($order->AppetisersAssortment)) {{ $order->AppetisersAssortment }} #endif
</p>
</div>
#endif
#if($order->Type === 'Sucré')
<div class="col-md-2 sweetquantity">
<h5>
Quantité
</h5>
<p>
#if(isset($order->SweetQuantity)) {{ $order->SweetQuantity }} #endif
</p>
</div>
<div class="col-md-6 sweetassortment">
<h5>
Coupe
</h5>
<p>
#if(isset($order->SweetAssortment)) {{ $order->SweetAssortment }} #endif
</p>
</div>
#endif
</div>
<div class="row">
#if($order->Type === 'Pain-surprise')
<div class="col-md-3 variante1">
<h5>
Assortiment 1
</h5>
<p>
#if(isset($order->Assortment1)) {{ $order->Assortment1 }} #endif
</p>
</div>
<div class="col-md-3 variante2">
<h5>
Assortiment 2
</h5>
<p>
#if(isset($order->Assortment2)) {{ $order->Assortment2 }} #endif
</p>
</div>
<div class="col-md-3 variante3">
<h5>
Assortiment 3
</h5>
<p>
#if(isset($order->Assortment3)) {{ $order->Assortment3 }} #endif
</p>
</div>
<div class="col-md-3 variante4">
<h5>
Assortiment 4
</h5>
<p>
#if(isset($order->Assortment4)) {{ $order->Assortment4 }} #endif
</p>
</div>
#endif
</div>
</div>
#endforeach
</form>
</tbody>
</table>
<!-- ADD A PAGINATION -->
<p></p>
#endsection
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
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