in view:
#if($posts)
#foreach($posts as$post)
<h2>
{{$post->title}}
</h2>
<p class="lead">
by {{$post->user->name}}
</p>
<p><span class="glyphicon glyphicon-time"></span> Posted {{$post->created_at->diffForHumans()}}</p>
<hr>
<img class="img-responsive" src="{{$post->photo->file}}" alt="">
<hr>
{!! str_limit($post->body,35 )!!}
<a class="btn btn-primary" href="{{route('home.post',$post->id)}}">Read More <span class="glyphicon glyphicon-chevron-right"></span></a>
<hr>
#endforeach
#endif
in controller:
public function searchmulti(){
$keyword=Input::get('title');
$posts = Post::where('title', 'like', "%$keyword%")->get();
return view('Admin.comments.postsauthor',compact('posts'));
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
convert search result to array with toArray() method (see here) and then
iterate through results and use str_replace to replace searched word with <span class="highlight">your word</span>
after this you will have $post['title'] (instead of $post->title) with highlighted span inside
of course you must add .highlight with some styling in your css
$posts = Post::where('title', 'like', "%$keyword%")->get()->toArray();
foreach($posts as &$post){
$post['title']=str_replace($keyword,"<span class='highlight'>$keyword</span>",$post['title']);
}
PS if you don't want to convert results to array you can edit tile in eloquent model directly but I prefer arrays to be certain that I don't save changes accidently
Related
I've written a blog that's supposed to fetch data from the database. But when I click on the read more link, it keeps opening all the posts that are in the db instead of selecting only one specific post that I've clicked. Can some kindly assist me with this issue?
View
#foreach($data as $aboutus)
<div class="col-lg">
<img src="{{ asset('storage/'.$aboutus->image) }}" class="img-fluid shadow-lg" style="">
<p class="mt-3 text-success fw-bold fs-5 text-lg-start">{{$aboutus->title}}</p>
<p class="fs-6 text-lg-start"> {{$aboutus->description}}</p>
<p class="text-success fw-bold text-lg-start">{{$aboutus->button}}</p>
<!--<img src="images/hr-2.png" class="img-fluid float-start my-2">-->
</div>
#endforeach
Controller
public function show($id)
{
//
$data = AboutUs::find($id)->get();
return view('about.show')->with(compact('data'));
}
Route
Route::get('/about.show/{id:title}','App\Http\Controllers\WelcomeController#show')->name('about.show');
i am trying to hide html code by if condition when all product featured is zero, html code should not be shown in front page please help me how can i do that ?
controller
public function index()
{
$data = [
'products' => Product::with('productColorGallary')->where('is_featured', 1)->first(),
];
return view('home', $data);
}
html view
#if($products->is_featured == 0)
<div class="col-md-6">
<div class="new-wall-image">
<img src="{{config('wall_master_furishing.file_url').$products->productColorGallary->featured_image}}" alt="">
</div>
</div>
<div class="col-md-6">
<div class="new-wall-descp">
<h2 class="theme-title">New Walls
</h2>
<p>{!!$products->description!!}</p>
<a class="blue-btn-a" href="#">Read More
<i class="fa fa-angle-right">
</i>
</a>
</div>
</div>
#endif
You are getting your products in your database with is_featured = 1. It means that your if condition in your blade will be always false.
Plus, are you trying to get all products or just one ?
If it's many, then :
Your controller
public function index()
{
$products = Product::with('productColorGallary')->get();
return view('home', compact('products');
}
and your blade
#foreach($products as $product)
#if($product->is_featured == 0)
<div class="col-md-6">
<div class="new-wall-image">
<img src="{{config('wall_master_furishing.file_url').$product->productColorGallary->featured_image}}" alt="">
</div>
</div>
<div class="col-md-6">
<div class="new-wall-descp">
<h2 class="theme-title">New Walls
</h2>
<p>{!!$products->description!!}</p>
<a class="blue-btn-a" href="#">Read More
<i class="fa fa-angle-right">
</i>
</a>
</div>
</div>
#else
SOMETHING HERE IF PRODUCT IS FEATURED
#endif
SOMETHING HERE COMMONS TO BOTH FEATURED AND NOT FEATURED
#endforeach
Products is an array key, so you can use like $data['products'] etc..
But if you create a collection/object like the following should be work:
public function index()
{
// first will return the first matched item from db
// and you will get only is_featured = 1
$products = Product::with('productColorGallary')->where('is_featured', 1)->first();
return view('home', compact('products');
}
I want to show user orders and use this code:
#foreach($orderHistory as $order)
<div class="with-spacing three-col orders" id="works-grid">
<div class="work-item branding ui-ux">
<div class="work-detail">
<a href="#">
<img src="images/portfolio-1.jpg" alt="">
<div class="work-info">
<div class="centrize">
<div class="v-center">
<h3>{{ $order->product_name }}</h3>
<p> {{ $order->product_price }}</p>
</div>
</div>
</div>
</a>
</div>
</div>
</div>
#endforeach
controller:
$orderHistory = DB::table('cart')
->where('user_id', auth()->id())
->join('singleproducts', 'singleproducts.id', '=', 'cart.product_id')
->select('singleproducts.name', 'cart.qty', 'cart.product_price')
->get();
return view('UI.UserProfile.profile' , compact('orderHistory'));
and the error is: " Undefined property: stdClass::$product_name "
what is the problem?
Typo, you are selecting the column "name" from the database.
#foreach($orderHistory as $order)
<div class="with-spacing three-col orders" id="works-grid">
<div class="work-item branding ui-ux">
<div class="work-detail">
<a href="#">
<img src="images/portfolio-1.jpg" alt="">
<div class="work-info">
<div class="centrize">
<div class="v-center">
<h3>{{ $order->name }}</h3>
<p> {{ $order->product_price }}</p>
</div>
</div>
</div>
</a>
</div>
</div>
</div>
#endforeach
or change the query
$orderHistory = DB::table('cart')
->where('user_id', auth()->id())
->join('singleproducts', 'singleproducts.id', '=', 'cart.product_id')
->select('singleproducts.name as product_name', 'cart.qty', 'cart.product_price')
->get();
return view('UI.UserProfile.profile' , compact('orderHistory'));
The problem is that $orderis not an object and you're trying to access it like an object. Perhaps it's an array - try accessing it as such.
We can't be sure unless we see where $order is defined in your controller maybe. One quick way is to place a #dd($order) just after initializing the for loop. That dumps the variable so you can see it's format.
Your query should be like this:
$orderHistory = DB::table('cart')
->where('user_id', auth()->id())
->join('singleproducts', 'singleproducts.id', '=', 'cart.product_id')
->select('singleproducts.name as product_name', 'cart.qty as quantity', 'cart.product_price as product_price')
->get();
return view('UI.UserProfile.profile' , compact('orderHistory'));
So you end up with an array of objects in this format:
[
{
product_name: 'name',
product_price: 555.00,
quantity: 2
}
...
{}
]
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
I am creating an basic pagination with Laravel5.1 , I receive the following PHP code.
public function postFindUsers(){
$name= \Request::input('name');
$findUserByNombre = User::where('name', 'LIKE', '%'.$name.'%')->paginate(6);
return view('users.findUsers')->with('users',$findUserByNombre);
}
This code returns the list of users correctly but in the view I don't know how to solve this error , I have the following code.
<div class="hotel-list listing-style3 hotel">
#foreach($users as $usuario)
<article class="box">
<figure class="col-sm-5 col-md-4">
<img width="270" height="160" alt="" src="{{$usuario->foto}}">
</figure>
<div class="details col-sm-7 col-md-8">
<a href="{{ URL::asset('detalle') }}">
<div>
<div>
<h4 class="box-title">{{$usuario->nombre}} {{$usuario->primer_apellido}} {{$usuario->segundo_apellido}}<small><i class="soap-icon-departure yellow-color"></i> {{$usuario->fecha_nacimiento}}</small></h4>
</div>
</div>
<div>
<p>{{$usuario->descripcion}}</p>
<div>
<a class="button btn-small full-width text-center" title="" href="detalle">CONSULTAR</a>
</div>
</div>
</a>
</div>
</article>
#endforeach
{{$users->render()}}
</div>
However , when I put this url in the page public/search?page=2 , this url return a typical error MethodNotAllowedHttpException in RouteCollection.php line 219:
Could anyone help to me ?
/UPDATE/
Route::get('/search-users', 'UserController#getUsers');
Route::post('/search', 'UserController#postFindUsers');
Get request
routes
Route::get('/search', 'UserController#getFindUsers');
controller
public function getFindUsers(){
$name= \Request::input('name');
$findUserByNombre = User::where('name', 'LIKE', '%'.$name.'%')->paginate(6);
return view('users.findUsers')->with('users',$findUserByNombre);
}
HTML
{!! Form::input ('search' , 's' , null , ['class' => 'form-control'] , ['placeholder' => 'Search...']) !!}
You can return both $findUserByNombre and $links in the function postFindUsers()
public function postFindUsers(){
$name= \Request::input('name');
$findUserByNombre = User::where('name', 'LIKE', '%'.$name.'%')->paginate(6);
$links = $findUserByNombre->render();
return view('users.findUsers', compact('findUserByNombre', 'links'));
}
And in the view findUsers.blade.php, just add
<div>{!! $links !!}</div>
And change the request method from post to get.
Route::get('/search', 'UserController#postFindUsers');
To get the 2nd page, just call the following url
public/search?page=2