I'm trying to show items count on parent and on child category. So far the count is visible only on child category but when the item is assigned only to parent directory is showing 0. I have added this in my Category model
public function addRelation( $categoires )
{
$categoires->map(function( $item, $key)
{
$sub = $this->selectChild($item->id);
$item->itemCount = $this->getItemCount($item->id , $item->parent_id );
return $item = array_add($item, 'subCategory', $sub);
});
return $categoires;
}
public function getItemCount( $category_id, $parent_id )
{
if( $parent_id == 0)
{ // for root-caregory
$ids = Category::select('id')->where('parent_id', $category_id)->get();
$array = array();
foreach ($ids as $id)
{
$array[] = $id->id;
}
return Item::whereIn('category_id', $array )->count();
}
else
{
return Item::where('category_id', $category_id)->count();
}
}
In the controller
$Category = new Category;
$allCategories = $Category->getCategories();
return view('home', compact('allCategories'));
And in my blade
#foreach($allCategories as $category)
<div class="card-body">
<h4 class="card-title">{!!$category->title!!} <span class="badge badge-primary badge-pill">({!! $category->itemCount !!})</span></h4>
</div>
#endforeach
itemCount is (0) when item is in Parent category.
After Discution that we had it's a simlpe query that you want just do it like this :
public function getItemCount( $category_id)
{
return Item::where('category_id', $category_id)->count();
}
Or if you have a relationship you can simply do it like this :
public function addRelation( $categoires )
{
$categoires->map(function( $item, $key)
{
$sub = $this->selectChild($item->id);
$item->itemCount = $item->items()->count();
return $item = array_add($item, 'subCategory', $sub);
});
return $categoires;
}
Related
I have implemented printing a categories tree to the page recursively:
foreach ($categories as $category) {
$tree .='<li>'.$category->title.'';
if(count($category->childs)) {
$tree .=$this->childView($category);
}
}
$tree .='<ul>';
return view('categoryTreeview',compact('categories','allCategories','tree'));
}
public function childView($category){
$html ='<ul>';
foreach ($category->childs as $arr) {
if(count($arr->childs)){
$html .='<li>'.$arr->title.'';
$html.= $this->childView($arr);
}else{
$html .='<li>'.$arr->title.'';
$html .="</li>";
}
}
$html .="</ul>";
return $html;
}
For DB structure: id|title|parent_id
Now I need to implement a iterative way to print a category tree to the page, and so far I have not found a solution.
I also tried:
function buildTree($categories) {
$childs = array();
foreach($categories as $category)
$childs[$category->parent_id][] = $category;
foreach($categories as $category) if (isset($childs[$category->id]))
$category->childs = $childs[$category->id];
return $childs[0];
}
$treez = buildTree($categories);
But I also don't know how to use this data non-recursively.
Could anyone lead me on the right path? Maybe I should combine foreach loops with some kind of while condition?
In your category model:
public function subCategories()
{
return $this->hasMany(Category::class, 'parent_id', 'id');
}
public function children()
{
return $this->subCategories()->with('children');
}
In your model:
public function getCategories()
{
$categories = Category::whereNull('parent_id')->with('children')->get();
return view('category', ['categories' => $categories]);
}
Blade (category.blade.php)
<div class="tree">
#include('category-list-widget',['categories' => $categories])
</div>
Blade (category-list-widget.blade.php)
<ul>
#foreach($categories as $category)
<li>
<a>{{$category->name}}</a>
#if(!empty($category->children) && $category->children->count())
#include('category-list-widget',['categories' => $category->children])
#endif
</li>
#endforeach
</ul>
I did not test the code, Just wrote it here directly in the texteditor. I hope you get the idea.
I am developing an online ecommerce for the first time and currently, i am not able to iterate through my collection.
Every item for a shop is categorized into a product category. My relationship is below
Category
public function items()
{
return $this->belongsToMany('App\Item','category_item','category_id','item_id')
->withTimestamps();
}
Items
public function categories()
{
return $this->belongsToMany('App\Category','category_item','item_id','category_id')
->withTimestamps();
}
This code here is able to fetch the groups and their products. I try to loop through to get the names of the products like below but it only displays the name of the last product in the database.
Why is this happening?
ItemController
//get id of product categories and display all products in grid table
$items_in_table = Category::whereIn('id',$request->get('product_category'))->with('products')->get();
foreach($items_in_table as $item)
{
return $item->name;
}
update
$temp
foreach($items_in_table as $item)
{
temp = $item;
}
return $temp
response
{"id":2,"title":"freight","no_of_contacts":0,"user_id":1,"created_at":"2018-04-15 23:55:30","updated_at":"2018-04-15 23:55:30","items":[{"id":1,"name":"AirBag ","phone":"0247878234","group_id":null,"user_id":1,"created_at":"2018-04-16 00:14:20","updated_at":"2018-04-16 05:31:05","pivot":{"group_id":2,"customer_id":1,"created_at":"2018-04-16 05:33:08","updated_at":"2018-04-16 05:33:08"}}
enter image description here
let's define our relationships first
Category:
public function items()
{
return $this->belongsToMany('App\Item')
->withTimestamps();
}
Item:
public function categories()
{
return $this->belongsToMany('App\Category')
->withTimestamps();
}
then in your controller:
$items_in_table = Category::with('items')->get();
$names = [];
foreach($items_in_table as $category) {
foreach($category->items as $item) {
$names[] = $item->name;
}
}
dd($names);
Maybe you need to put it in an array and then rotate it
$items_in_table = Category::whereIn('id',$request->get('product_category'))->with('products')->get();
$name = [];
foreach($items_in_table as $item)
{
$name[] = $item->name;
}
return $name;
foreach will not manipulate the values of the array
Some possible solutions:
Use a reference, this will change the values in $items_in_table
foreach($items_in_table as &$item)
{
$item = $item->name;
}
Use map (since it is an Laravel-collection), this will also alter the array
$items_in_table->map(function ($item) {
return $item->name;
})
you can Access any relation like this way .
For View what is you are getting. you can also use dd($item->quoteReturnDetail)
return collect($models->items())->transform(function ($item, $index){
if( $item->quoteReturnDetail){ dd($item->quoteReturnDetail); } });
I'm going to pass data in array
but here's the code
public function test($pid){
$row_for_menu = Menu::where('parent_id', '=', $pid)->get();
$menu = array();
foreach ($row_for_menu as $menu_one_by_one){
$menu[] = array('title' => $menu_one_by_one->name, 'nodes' => $this->test($menu_one_by_one->id));
}
dd($menu);
}
public function index(Request $request)
{
$this->test(1);
//$perPage = 25;
//$menu = Menu::paginate($perPage);
//return view('admin.menu.index', compact('menu'));
}
my foreach in first round worked well
but in the end responsed [ dd($menu) ] empty array and error 500 !
my english is not well sorry ;)
try this demo working fine
public function test($pid){
$row_for_menu = Menu::where('parent_id', '=', $pid)->get()->lists('id','name');
$menu = array();
//foreach ($row_for_menu as $menu_one_by_one){
//$menu[] = array('title' => $menu_one_by_one->name, 'nodes' => $this->test($menu_one_by_one->id));
//}
dd($menu);
}
$menu is empty ie. array() , because $row_for_menu is empty
public function test($pid){
$row_for_menu = Menu::where('parent_id', '=', $pid)->get();
$menu = array();
foreach ($row_for_menu as $menu_one_by_one){
$menu[] = array('title' => $menu_one_by_one->name, 'nodes' => $this->test($menu_one_by_one->id));
}
if(count($menu)){
dd($menu);
}
}
public function index(Request $request)
{
$this->test(1);
//$perPage = 25;
//$menu = Menu::paginate($perPage);
//return view('admin.menu.index', compact('menu'));
}
You should define a relation between your menu items on the model instead of recursively looping to build the menu.
// Menu model
public function parent ()
{
return $this->belongsTo(__CLASS__, 'parent_id');
}
public function children ()
{
return $this->hasMany(__CLASS__);
}
Then in your view:
#foreach ($menu->children as $child)
// $child->name
// $child->link
#endoreach
Your controller would simply be:
// assuming you pass a menu id for specific page or similar
public function index(Request $request) {
$menu = Menu::find($request->getAttribute('menu_id'))->toArray();
return view('admin.menu.index', compact('menu'));
}
the test function must return $menu after foreach like this
public function test($pid){
$menus = Menu::where('parent_id', '=', $pid)->get();
$menu = array();
foreach ($menus as $menu_one_by_one){
$menu[] = array('text' => $menu_one_by_one->name, 'nodes' => $this->test($menu_one_by_one->id));
}
return $menu;
}
public function index(Request $request)
{
$x = $this->test(1);
dd($menu)
I have complex query and relation which I'm not fully understand. I'm kind of new in Laravel. Anyway, I'm looking for a way to load this with slugs instead of ID's.
This is the function in the controller
public function index( $category_id)
{
$Category = new Category;
$allCategories = $Category->getCategories();
$category = Category::find($category_id);
if($category->parent_id == 0) {
$ids = Category::select('id')->where('parent_id', $category_id)->where('parent_id','!=',0)->get();
$array = array();
foreach ($ids as $id) {
$array[] = (int) $id->id;
}
$items = Item::whereIn('category_id',$array)->where('published', 1)->paginate(5);
} else {
$items = Item::where('category_id' ,$category_id)->where('published', 1)->paginate(5);
}
return view('list', compact('allCategories','items'));
}
Those are relations in the Model
public function item()
{
return $this->hasMany('App\Item','category_id');
}
public function children()
{
return $this->hasMany('App\Category', 'parent_id');
}
public function getCategories()
{
$categoires = Category::where('parent_id',0)->get();
$categoires = $this->addRelation($categoires);
return $categoires;
}
public function selectChild( $id )
{
$categoires = Category::where('parent_id',$id)->where('published', 1)->paginate(40);
$categoires = $this->addRelation($categoires);
return $categoires;
}
public function addRelation( $categoires )
{
$categoires->map(function( $item, $key)
{
$sub = $this->selectChild($item->id);
$item->itemCount = $this->getItemCount($item->id , $item->parent_id );
return $item = array_add($item, 'subCategory', $sub);
});
return $categoires;
}
public function getItemCount( $category_id )
{
return Item::where('category_id', $category_id)->count();
}
This is what I have in my routes
Route::get('list/{category}', 'ListController#index')->name('list');
currently is loading urls like http://example.com/list/1 where 1 is the ID. I'm wonder if with current setup is possible to make it like, http://example.com/slug
I'm aware how slugs are working. I just can't understand how to use them in queries instead of ID's
You can use explicit Route Model Binding to grab your Category by slug before processing it.
In your RouteServiceProvider you need to bind the model:
Route::bind('category', function ($value) {
//Change slug to your column name
return App\Category::where('slug', $value)->firstOrFail();
});
Then, you can typehint the categories.
For example in your index method:
public function index(Category $category)
{
$Category = new Category;
$allCategories = $Category->getCategories();
//This line is obsolete now:
//$category = Category::find($category_id);
//...
}
Try to change your index function parameter from $category_id to $category_slug
Remove this line $Category = new Category;
And change this $category = Category::find($category_id);
To this: $category = Category::where('slug', $category_slug)->first();
*Assuming that you have a unique slug in category table
I have model for my post in my site.My post model has a column that stores visits.when user visit my post should increments by 1.
I exactly knows how to do this, but my problem is when I increment it by one in my model, it's increments by 2!!!!!
I wrote this code in my controller:
$new_post_inst = Post::where('id','=',$id)->first();
$new_post_inst->increment('visit');
This is my controller:
public function get_id($id) {
// cat
$cat = Category::join('catrelations', 'catrelations.idcat', '=', 'categories.id')->orderBy('categories.id', 'ASC')->get();
// menu
$nav = Nav::orderBy('index', 'DESC')->get();
$post = Post::find($id);
$setting = Settings::find(1);
$comments = Comment::where('post_id', '=', $id)->orderBy('id', 'asc')->get();
$get_reply = array();
$get_reply_id = array();
//$counter = 0;
//var_dump($comments);
foreach ($comments as $comment) {
$reply = Reply::where('parentId', '=', $comment->id)->get();
if (!$reply->isEmpty()) {
//$arr_reply[$comment->id] = $reply;
//echo $comment->id.' has reply!!!!!<br>';
//$reply_parent_id = $comment->id;
$counter = 0;
foreach ($reply as $replying) {
$get_reply[$comment->id][$counter] = Comment::where('id', '=', $replying->comment_id)->get();
//$comment_reply_id = $replying->comment_id;
//$reply_arr = array();
//$reply_arr[$comment->id] = $comment_reply_id;
//echo 'The reply is: '.$comment_reply_id.'<br>';
foreach ($get_reply[$comment->id][$counter] as $key => $value) {
//($value->text);
$get_reply_id[] = $value->id;
}
$counter++;
}
//$counter++;
}
}
$post_owner_info = User::select('id', 'first_name', 'last_name', 'image', 'desc')->find($post->user_id);
$arr = array();
$arr['comments'] = $comments;
$arr['post'] = $post;
//$arr['reply'] = $reply;
//var_dump($get_reply);
//var_dump($get_reply_id);
$new_post_inst = Post::where('id','=',$id)->first();
$new_post_inst->increment('visit');
return View::make('blogsingle', compact('arr'))->with('setting', $setting)->with('post', $post)->with('nav', $nav)->with('get_reply', $get_reply)->with('get_reply_id', $get_reply_id)->with('cat', $cat)->with('post_owner_info', $post_owner_info);
}
And this is my Post model:
class Post extends Eloquent
{
public function User()
{
return $this->belongsTo('User');
}
public function Categories()
{
return $this->belongstomany('Category');
}
public function comments()
{
return $this->hasMany('Comment');
}
}
you can try like this
$new_post_inst = Post::where('id','=',$id)->first();
$new_post_inst->increment('visit',1);
Try this:
$new_post_inst = Post::where('id','=',$id)->first();
$new_post_inst->visit+=1;
$new_post_inst->save();
OR this:
$new_post_inst = Post::where('id','=',$id)->first();
$new_post_inst->update(array('visit' => $new_post_inst->visit+1));
I searched around for a little bit and this could be the favicon icon that is making a second request. How did you declare the favicon? Or maybe you could remove it to try and see if it is indeed the favicon?