Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,148,580 members, 7,801,667 topics. Date: Thursday, 18 April 2024 at 07:45 PM

Create A Nested Lists Of Categories In Laravel 5 - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Create A Nested Lists Of Categories In Laravel 5 (9878 Views)

How To Create Multiple User Authentication In Laravel 5.3 / Common Error In Laravel #1:solved File_put_contents Failed To Open Stream / Solution To Tokenmismatchexception Problem In Laravel 5.1 (2) (3) (4)

(1) (Reply) (Go Down)

Create A Nested Lists Of Categories In Laravel 5 by maekhel(m): 12:06am On Aug 26, 2015
Laravel gurus I need your help on a project am currently working on. Am trying to create a tree-like categories structure where each child will be listed under its parent. Am being able to create and save these categories in the database but now am finding it difficult to loop through the categories and display them. I want the categories to be rendered as below:
Fashion Accessories
- Bags
- Cloths
Mobile Phones
- Tablets
- Smartphones
Re: Create A Nested Lists Of Categories In Laravel 5 by babatope88(m): 8:16am On Aug 26, 2015
Maybe this will work nested for each.
give the sample of the data structure, this will aid more contribution from our nairaland e-family

for each($something as $something) {
for each() {}
}
Re: Create A Nested Lists Of Categories In Laravel 5 by danvery2k6(m): 8:47am On Aug 26, 2015

@if($categories->any)
@foreach($categories as $category)
<h3>$category</h3>
<ul>
@foreach($category as $aCategory)
<li>$aCategory->item</li>
@endforeach
</ul>
@endforeach
@endif


I presume you are using the blade templating engine. And that you also have categories and items tables linked together by foreign keys and the relationships properly defined in your models.
Re: Create A Nested Lists Of Categories In Laravel 5 by maekhel(m): 9:41am On Aug 26, 2015
Sorry for not providing codes before.
My Controller:
public function index()
{
$categories = Category::with('children')->get();

return view('backend.categories.index')->with('categories', $categories);
}

My Category Model :
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
protected $guarded = ['id'];

public function parent()
{
return $this->belongsTo('App\Category', 'parent_id');
}

public function children()
{
return $this->hasMany('App\Category', 'parent_id');
}
}
My View:
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>Name</th>
<th>Description</th>
<th>Slug</th>
<th>Action</th>
</tr>
</thead>
<tbody>
@foreach ($categories as $category)
{{--@foreach ($category->children as $children)--}}
<tr>
<td>{{ $category->name }}</td>
<td>{{ $category->description }}</td>
<td>{{ $category->slug }}</td>
<td><a class="edit" href="{!! action('Admin\CategoriesController@edit', $category->id) !!}" title="Edit"><i class="fa fa-pencil-square-o"></a></i> <a class="delete" href="{!! action('Admin\CategoriesController@destroy', $category->id) !!}" title="Are you sure you want to delete?"><i class="fa fa-trash-o"></i></a></td>
@foreach ($category->children as $children)
<tr>
<td>{{ $children->name }}</td>
<td>{{ $children->description }}</td>
<td>{{ $children->slug }}</td>
<td></td>
</tr>
@endforeach
</tr>
</tbody>
{{--@endforeach--}}
@endforeach
</table>
Yes I have a categories table with the following structure:

+-------------+------------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------------------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| slug | varchar(255) | NO | | NULL | |
| parent_id | int(11) | YES | | NULL | |
| description | text | NO | | NULL | |
| created_at | timestamp | NO | | 0000-00-00 00:00:00 | |
| updated_at | timestamp | NO | | 0000-00-00 00:00:00 | |
+-------------+------------------+------+-----+---------------------+----------------+

Re: Create A Nested Lists Of Categories In Laravel 5 by jacob05(m): 10:47am On Aug 26, 2015
Make things simple for yourself... it won't hurt creating another Model for CategoryItem



Category


namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
protected $guarded = ['id'];

public static $table = 'categories';

public function children()
{
return $this->hasMany('App\CategoryItem', 'parent_id');
}
}



CategoryItem


namespace App;

use Illuminate\Database\Eloquent\Model;

class CategoryItem extends Model
{
protected $guarded = ['id'];
public static $table = 'categoryitems';

public function pareet()
{
return $this>belongsTo('App\CategoryItem', 'parent_id');
}
}




Migratations


<?php

class Create_Categories_Table {

/**
* Make changes to the database.
*
* @return void
*/
public function up()
{
//

Schema::table("categories", function($table){
$table->create();
$table->increments('id');
$table->string('name', 255);
$table->timestamps();
});

}

/**
* Revert the changes to the database.
*
* @return void
*/
public function down()
{
//
Schema::drop("categories"wink;
}

}





<?php

class Create_CategoryItems_Table {

/**
* Make changes to the database.
*
* @return void
*/
public function up()
{
//

Schema::table("categoryitems", function($table){
$table->create();
$table->increments('id');
$table->increments('parent_id');
$table->string('name', 255);
$table->string('slug', 255);
$table->timestamps();
$table->foreign('parent_id')->references('id')->on('categories');
});

}

/**
* Revert the changes to the database.
*
* @return void
*/
public function down()
{
//
Schema::drop("categoryitems"wink;
}

}


The Above Might not run but should explain the logic

1 Like

Re: Create A Nested Lists Of Categories In Laravel 5 by Nobody: 12:38pm On Aug 26, 2015
Re: Create A Nested Lists Of Categories In Laravel 5 by jacob05(m): 12:55pm On Aug 26, 2015
MuhammaduBuhari:
check out adjacency model - http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/
- dhtml18
Egbon, that one na sql wahala.... He's looking for the Laravel 5 approach
Re: Create A Nested Lists Of Categories In Laravel 5 by Nobody: 2:09pm On Aug 26, 2015
Me no get time to dey read full posts o jare. No time to check time, am outa here.
- troll
Re: Create A Nested Lists Of Categories In Laravel 5 by jacob05(m): 2:14pm On Aug 26, 2015
MuhammaduBuhari:
Me no get time to dey read full posts o jare. No time to check time, am outa here.
- troll

lol... Ok
Re: Create A Nested Lists Of Categories In Laravel 5 by chiwex(m): 9:20pm On Aug 26, 2015
use Baum laravel package I always us it for unlimited category
Re: Create A Nested Lists Of Categories In Laravel 5 by jacob05(m): 9:45pm On Aug 26, 2015
chiwex:
use Baum laravel package I always us it for unlimited category


An Over-Kill, to me, for the simple task... Baum\Node? , phewww
Re: Create A Nested Lists Of Categories In Laravel 5 by chiwex(m): 10:02pm On Aug 26, 2015
baum is easy

you can

CREATE
ADD
UPDATE
DELETED
MOVE UP AND DOWN UR CATEGORIES

without steers

remember after implementing the package using a controller you can reuse it for any project
Re: Create A Nested Lists Of Categories In Laravel 5 by jacob05(m): 10:13pm On Aug 26, 2015
chiwex:
baum is easy

you can

CREATE
ADD
UPDATE
DELETED
MOVE UP AND DOWN UR CATEGORIES

without steers

remember after implementing the package using a controller you can reuse it for any project

and those can't be done implementing "vanilla" Eloquent ....
Re: Create A Nested Lists Of Categories In Laravel 5 by maekhel(m): 10:15pm On Aug 26, 2015
jacob05:


An Over-Kill, to me, for the simple task... Baum\Node? , phewww
Thats my take on Baum too.
have being able to loop and display the categories the way I wanted.
But am now faced with another problem. I created an edit link on each categories passing it category id, but I got this error:

ErrorException in 775837465f5ab64876c1dc1677878595 line 46: Trying to get property of non-object (View: /resources/views/backend/categories/edit.blade.php)
My controller
public function edit($id)
{
$categories = Category::find($id);
//print_r($categories);
return view('backend.categories.edit')->with('categories', $categories);
}
Re: Create A Nested Lists Of Categories In Laravel 5 by chiwex(m): 10:31pm On Aug 26, 2015
I will post my own codes tomorrow
Re: Create A Nested Lists Of Categories In Laravel 5 by jacob05(m): 10:45pm On Aug 26, 2015
maekhel:

Thats my take on Baum too.
have being able to loop and display the categories the way I wanted.
But am now faced with another problem. I created an edit link on each categories passing it category id, but I got this error:


My controller
It's most likely that $categories = Category::find($id); found nothing but passed as null and the view is trying to access it's property
Re: Create A Nested Lists Of Categories In Laravel 5 by maekhel(m): 10:57pm On Aug 26, 2015
jacob05:

It's most likely that $categories = Category::find($id); found nothing but passed as null and the view is trying to access it's property
It actually found something, cos if I comment out looping thru the categories to create select menu it works. But that does give the options to select from other categories in the database.
Hope you got wat I mean?
Re: Create A Nested Lists Of Categories In Laravel 5 by jacob05(m): 11:04pm On Aug 26, 2015
maekhel:

It actually found something, cos if I comment out looping thru the categories to create select menu it works. But that does give the options to select from other categories in the database.
Hope you got wat I mean?

Let's see how you implemented the Models and the Views
Re: Create A Nested Lists Of Categories In Laravel 5 by maekhel(m): 1:04am On Aug 27, 2015
Thanks guys for your help, working as expected now.smiley

1 Like

Re: Create A Nested Lists Of Categories In Laravel 5 by Nobody: 4:16am On Aug 27, 2015
Thank God for the great programmers on nairaland.
No mind me, na old-school troll i be thinking that laravel is the same as SQL.
Re: Create A Nested Lists Of Categories In Laravel 5 by maekhel(m): 7:21am On Aug 27, 2015
MuhammaduBuhari:
Thank God for the great programmers on nairaland.
No mind me, na old-school troll i be thinking that laravel is the same as SQL.
Lol
Re: Create A Nested Lists Of Categories In Laravel 5 by Nobody: 8:40am On Aug 27, 2015
maekhel:

Lol
Na olodo i be, so you fit laff.
Re: Create A Nested Lists Of Categories In Laravel 5 by jacob05(m): 9:30am On Aug 27, 2015
maekhel:
Thanks guys for your help, working as expected now.smiley
Good cool
Re: Create A Nested Lists Of Categories In Laravel 5 by CRAZYMADMAN(m): 2:48pm On Aug 27, 2015
So many laravel folks here. I wonder how many zend users we have on nairaland undecided
Re: Create A Nested Lists Of Categories In Laravel 5 by Nobody: 5:16pm On Aug 27, 2015
CRAZYMADMAN:
So many laravel folks here. I wonder how many zend users we have on nairaland undecided
We need to do a census one day. I am surprised that we have so much laravel folks as well.
Re: Create A Nested Lists Of Categories In Laravel 5 by jacob05(m): 5:39pm On Aug 27, 2015
MuhammaduBuhari:

We need to do a census one day. I am surprised that we have so much laravel folks as well.
Yea.. Laravel Rocks !!!

1 Like

Re: Create A Nested Lists Of Categories In Laravel 5 by Nobody: 7:43am On Aug 28, 2015
Eya pele come to Yii2 we got that covered. I don't really like L5 , haven't seen any spectacular reason to jump board.

1 Like

Re: Create A Nested Lists Of Categories In Laravel 5 by Nobody: 7:45am On Aug 28, 2015
CRAZYMADMAN:
So many laravel folks here. I wonder how many zend users we have on nairaland undecided

Zend is for masochists. Too complex

1 Like

Re: Create A Nested Lists Of Categories In Laravel 5 by danvery2k6(m): 8:51am On Aug 28, 2015
pcguru1:
Eya pele come to Yii2 we got that covered. I don't really like L5 , haven't seen any spectacular reason to jump board.


You don't know what you are missing out on. Try it, I assure you, you won't regret it.
Re: Create A Nested Lists Of Categories In Laravel 5 by Nobody: 9:17am On Aug 28, 2015
danvery2k6:



You don't know what you are missing out on. Try it, I assure you, you won't regret it.

Don't get me wrong, I know it's awesome but for me i just feel comfortable with Yii2 and it's clean API and all. got nothing against L5 but i will try it our for a project to test, but am not one willing to pick another templating language when PHP template works.
Re: Create A Nested Lists Of Categories In Laravel 5 by maekhel(m): 3:49pm On Aug 28, 2015
Re: Create A Nested Lists Of Categories In Laravel 5 by koechb: 3:54pm On May 30, 2017
Did you get the answer to this

working to something related
want to list just like this

Fashion Accessories
- Bags
- Cloths
Mobile Phones
- Tablets
- Smartphones

I am using laravel 5.3 now

(1) (Reply)

Trivia Coding Questions (euler Project) / Confused Between Node Js And PHP / Architectural Differences Between Virtual And Non-Virtual Machines

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 40
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.