In category counts his item from database table - php

My model code
how we can call this function in blade.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class BasicModel extends Model
{
public static function get_product_count($id){
$query = "select COUNT(sub_id) AS count FROM products WHERE products.sub_id = $id";
print_r($query);
return $query->row_array();
}
}
My view.blade.php code
count in foreach loop or show in all category
#foreach ($r as $row)
<li class="grid-item type-rent">
<div class="property-block">
<img src="{{ URL::to('/template/images/background-images/sub-category-images/' .$row->sub_cat_images. '')}}" alt=""> <!-- <span class="images-count"><i class="fa fa-picture-o"></i> 2</span> <span class="badges">Rent</span> -->
<div class="property-info">
<h4>{{ ucwords(substr($row->sub_cat_name, 0, 22)) }}</h4>
<span class="location">NYC</span>
<div class="price"><strong>Items</strong><span>
<!-- start count code from here -->
$data = $this->BasicModel->count {{ ($row->sub_id) }}
echo $data['count'];
</span></div>
</div>
<!-- <div class="property-amenities clearfix"> <span class="area"><strong>5000</strong>Area</span> <span class="baths"><strong>3</strong>Baths</span> <span class="beds"><strong>3</strong>Beds</span> <span class="parking"><strong>1</strong>Parking</span> </div> -->
</div>
</li>
#endforeach
My BasicController Code
public function grid(Request $request, $id)
{
if ($id == 1) {
$r = DB::table('sub_category')->select('*')->where('cat_id', $id)
->where('sub_status', '1')->orderBy('sub_id', 'asc')->get();
$name = DB::table('category')->where('cat_id', $id)->get();
return view('buy-and-sell/grid', compact('r','name','count'));
}
image for your help
image for your help
problem in this image please solve the problem

I think you are closing the PHP tag in the wrong place. Change to this:
<?php print_r($count);
$data = $this->BasicController->count {{ ($row->sub_id) }}
echo $data['count'];//here is the rightplace to close your PHP ?>
</span></div>
All PHP code needs to stay between the <?php and ?>
Looking to your image, apparently, it's printing the $count and then it's printing the plain text , so you have 80$data = ...

If you counted your rows in controller by the code below
$count = DB::table('products')->where('sub_id', $id)->count();
then why wrote so many code in html for counting?
Remove all code form counting form your html and just use this {{ $count }}

Related

Recursive system : load all products in each categories

I've made this system with the help of Staudenmeir\LaravelAdjacencyList.
It's a recurcive system to load categories, with their products for each.
The moment where I fail (I think) is when loading the products. Because his module duplicates a request and I don't think that's normal ?
the view :
`
#foreach($products as $product)
<article class="article-small">
<a href="{{ route('product.index', array($product->id, $product->productCat->slug, $product->slug)) }}">
<header>
<img src="{{ asset('uploads/products/'.$product->img.'')}}" alt="{{ $product->product_name }}"/>
<p>{{\Illuminate\Support\Str::limit($product->description, 70, '...')}}</p>
</header>
<section class="d-flex justify-content-between">
<div class="left">
<h3 title="{{$product->product_name}}">{{$product->product_name}}</h3>
</div>
#if($product->product_price != '')
<span class="price"><b>{{$product->priceRender()}} €</b></span>
#else
<span class="price"><b>{{$product->product_total_price}} €</b></span>
#endif
</section>
</a>
</article>
#endforeach
`
The Controller :
`
public function getById($locale, $id)
{
$category=$this->productCatManagement->getById($id);
$query=$this->productCatManagement->allProductsByParent($category);
$ariane = $this->productCatManagement->getAriane($category, "catPage");
return view('product.productCat.get-by-id', compact('category', 'query', 'ariane'));
}
`
allProductsByParent function :
`
$categories = $parent->descendantsAndSelf()->get();
$stockProducts = collect([]);
foreach($categories as $category)
{
$products = $category->products->where('state', '=', '1');
$products->each(function($product) use ($stockProducts){
$stockProducts->push($product);
});
}
return $stockProducts;
`
Do you think i'm doing right ?
Picture of the requests : Here
I except a system with less of request as possible.
Thank you by advance all !

Why Laravel pagination show same page?

I have a blog module in my laravel with multiple categories.In other categories pagination works just fine but with this category method links() always show the first page.This is my code:
$page = Categoriepage::where('identifiant', 'orientation')->first();
$idArticle = Articleliaison::select(array('idArticle'))->where('idPage',$page->id)->pluck('idArticle');
$articles = Article::whereIn('id',$idArticle)->where('status','enable')->orderby('created_at','desc')->get()->take(6);
$links = Article::whereIn('id',$idArticle)->where('status','enable')->orderby('created_at','desc')->paginate(6);
return View::make('pages.orientation')->with('articles',$articles)->with('links',$links);
and in my view:
<?php $i = 1 ?>
<div class="masonry ">
#foreach($articles as $article)
#include('components.article-or')
<?php $i++ ?>
#endforeach()
</div>
<div class="text-center" style="margin-top: 5%">
{{ $links->links() }}
</div>
You always select the first 6 articles because the articles displayed come from:
$articles = Article::whereIn('id',$idArticle)->where('status','enable')->orderby('created_at','desc')->get()->take(6);
and not from a paginate request.
There is no need to separate the query for the links and the query for the actual articles. Instead of:
$articles = Article::whereIn('id',$idArticle)->where('status','enable')->orderby('created_at','desc')->get()->take(6);
$links = Article::whereIn('id',$idArticle)->where('status','enable')->orderby('created_at','desc')->paginate(6);
You can do:
$articles = Article::whereIn('id',$idArticle)->where('status','enable')->orderby('created_at','desc')->paginate(6);
and in your view:
<div class="masonry ">
#foreach($articles as $article)
#include('components.article-or')
#endforeach
</div>
<div class="text-center" style="margin-top: 5%">
{{ $articles->links() }}
</div>
I think you got this one a bit mixed up.
You retrieve all articles (->get()) and take the first six (->take(6)). And then you retrieve $links using ->paginator(6).
This can and should all be done using just the ->paginator(6), this way Laravel retrieves the correct 6 articles for the ?page=X you are on and renders the links based on the total amount of articles found by the query.
Try it like this:
$page = Categoriepage::where('identifiant', 'orientation')->first();
$idArticle = Articleliaison::select(array('idArticle'))->where('idPage',$page->id)->pluck('idArticle');
$articles = Article::whereIn('id',$idArticle)->where('status','enable')->orderby('created_at','desc')->paginate(6);
return View::make('pages.orientation')->with('articles',$articles)
<?php $i = 1 ?>
<div class="masonry ">
#foreach($articles as $article)
#include('components.article-or')
<?php $i++ ?>
#endforeach
</div>
<div class="text-center" style="margin-top: 5%">
{{ $articles->links() }}
</div>
you do not have to make two requests but only one
$page = Categoriepage::where('identifiant', 'orientation')->first();
$idArticle = Articleliaison::select(array('idArticle'))->where('idPage',$page->id)->pluck('idArticle');
$articles = Article::whereIn('id',$idArticle)->where('status','enable')->orderby('created_at','desc')->paginate(6);
return View::make('pages.orientation')->with('articles',$articles);
In your view.
<div class="masonry ">
#foreach($articles as $article)
#include('components.article-or')
<?php $i++ ?>
#endforeach()
</div>
<div class="text-center" style="margin-top: 5%">
{{ $articles->links() }}
</div>

can we use multiple variables in foreach loop..?(laravel)

i want to use $news_data variable in my span tag that is under foreach loop.
this is my code..
#foreach($news as $key => $value)
<li>
<h3 class="title">{{$value->title}}</h3>
<div class="description">
<div class="popup">
<i class="fa fa-newspaper-o"></i>
<span class="popuptext" id="myPopup"> **use $news_data here**</span>
</div>
{{$value->news_date}} : {{$value->description}}...
</div>
</li>
#endforeach
this is my controller function:
public function index()
{
$news = DB::table('feed_news')
->select(DB::raw('news_date,title,LEFT(`description`, 40) as
`description`'))
->orderBy('news_date','DESC')->get();
$news_data = DB::table('feed_news')->select(DB::raw('description'))->get();
return view('DisplayNews.index')
->with('news_data',$news_data)
->with('news',$news);
}
my $news & $news_data comes from controller.
what is the best way to do this..?
You can use different variables inside for loop, that doesn't belong to loop.
Just use that variable as any other variable:
Replace **use $news_data here** with
{{$news_data}}
You are getting a collection again in $news_data , hence you will have to loop it again to get it's description.
for example:
foreach($newsdata as $news)
{
<span>{{$news->description}}</span>
}
<span class="popuptext" id="myPopup">{{ json_encode($news_data) }}</span>
Above code will show all data of that $news_data
If you want to detail showing instead of showing all. Loop it again using #foreach
To Show All
#foreach ($news_data as $newsDesc)
<span class="popuptext" id="myPopup">{{ $newsDesc->description }}</span>
#endforeach

model function call in blade view laravel

My model code
how we can call this function in blade.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class BasicModel extends Model
{
public static function get_product_count($id){
$query = "select COUNT(sub_id) AS count FROM products WHERE products.sub_id = $id";
print_r($query);
return $query->row_array();
}
}
My view.blade.php code
count in foreach loop or show in all category
#foreach ($r as $row)
<li class="grid-item type-rent">
<div class="property-block">
<img src="{{ URL::to('/template/images/background-images/sub-category-images/' .$row->sub_cat_images. '')}}" alt=""> <!-- <span class="images-count"><i class="fa fa-picture-o"></i> 2</span> <span class="badges">Rent</span> -->
<div class="property-info">
<h4>{{ ucwords(substr($row->sub_cat_name, 0, 22)) }}</h4>
<span class="location">NYC</span>
<div class="price"><strong>Items</strong><span>
<!-- start count code from here -->
$data = $this->BasicModel->count {{ ($row->sub_id) }}
echo $data['count'];
</span></div>
</div>
<!-- <div class="property-amenities clearfix"> <span class="area"><strong>5000</strong>Area</span> <span class="baths"><strong>3</strong>Baths</span> <span class="beds"><strong>3</strong>Beds</span> <span class="parking"><strong>1</strong>Parking</span> </div> -->
</div>
</li>
#endforeach
My BasicController Code
public function grid(Request $request, $id)
{
if ($id == 1) {
$r = DB::table('sub_category')->select('*')->where('cat_id', $id)
->where('sub_status', '1')->orderBy('sub_id', 'asc')->get();
$name = DB::table('category')->where('cat_id', $id)->get();
return view('buy-and-sell/grid', compact('r','name','count'));
}
image for your help
image for your help
problem in this image please solve the problem
Although its no good Practice Accessing the DB in Blade (better do this in the controller and pass the data) you can do:
<div class="price"><strong>Products</strong>
<span>
{{ BasicModel::where('sub_id', $row->sub_id)->count() }}
</span>
</div>
Its not tested, but have a look at the Eloquent docs, the count() method is explained there.
Update: I am not shure if laravel will find the class BasicModel (I never would access Models directly in blade, as stated do this in the controller and pass the data.) So maybe you need to write it with the full Namespace most likely {{ \App\BasicModel::where() }}.

Loop Object Relationship Laravel

Here i'm trying to to loop through all my users with their related assignments. I have made the User->Assignment Relationship in the model. When I dd($students) I receive the array of students with their related assignments, however when I view this on the page I do not receive the loop of assignments. What could I have missed in the code? Im sure it's somewhere in the html I messed up.
User Model:
public function assignments()
{
return $this->hasMany('App\Assignment');
}
Assignment Model:
public function userAssignment()
{
return $this->belongsTo('App\User');
}
Controller:
$students = User::where('position','student')->with('assignments')->get();
Html:
#foreach($students as $student)
<div class="item{{{ $isFirst ? ' active' : '' }}}">
<div class = "deck">
<div class = "row marg">
<div class="col-md-8">
<div class = "stdnt">
<h1>Student Info</h1>
<h1>{{$student->name}}</h1>
<p>Program: {{$student->pack}}</p>
<p>Level: {{$student->level}}</p>
<p>Status: {{$student->status}}</p>
<p>Lesson: {{$student->lesson}}</p>
</div>
</div>
<div class = "col-md-4">
<div class = "teac">
<h1>Teacher Info</h1>
<h1>{{$student->teacher}}</h1>
<p>Picture</p>
<p>assign form</p>
<p>assign button</p>
</div>
</div>
</div>
</div>
<div class = "mask flow">
<div class = "row-fluid marg">
#foreach($student->assignments() as $assignment)
<div class = "ablock">
<h1>{{$assignment->title}}</h1>
<p>{{$assignment->created_at}}</p>
<p>{{$assignment->type}}</p>
<p>{{$assignment->body}}</p>
</div>
#endforeach
</div>
</div>
{{--*/ $isFirst = false; /*--}}
</div>
#endforeach
Don't call the assignments method. Simply access it as a property:
#foreach($student->assignments as $assignment)
// ^^
#endforeach

Categories