2
I have the following structure of a table "Categories":
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->integer('main_category');
$table->string('name');
$table->string('description')->nullable();
$table->timestamps();
});
}
In my model I did the following function:
public function subCategories()
{
return $this->hasMany(Category::class, 'main_category','id');
}
In my controller is this way:
public function index()
{
$tree_categories = Category::where('main_category', '0')->with('subCategories')->get();
return $tree_categories;
}
This function looks for the categories considered "mothers", this return is this way:
[
{
"id": 5,
"main_category": 0,
"name": "Oleos",
"description": "teste",
"created_at": "2017-09-30 01:30:30",
"updated_at": "-0001-11-30 00:00:00",
"sub_categories": [
{
"id": 6,
"main_category": 5,
"name": "Fluidos",
"description": "Teste",
"created_at": "2017-09-30 01:30:30",
"updated_at": "-0001-11-30 00:00:00"
}
]
}
]
My example view:
@extends('layouts.template')
@section('content')
<div class="col-md-3">
<label class="ls-label">
<b class="ls-label-text">Categorias</b>
<div class="ls-custom-select ls-field-md">
<select name="id" id="id" class="ls-select">
<option value="" selected="selected">TODOS</option>
@foreach($tree_categories as $tree_category)
<option value="{{$tree_category['id']}}" data-type="main">{{$tree_category['name']}}</option>
@if(count($tree_category['sub_categories']) > 0)
@foreach($tree_category['sub_categories'] as $sub_category)
<option value="{{$sub_category['id']}}" data-type="sub">{{$sub_category['name']}}</option>
@endforeach
@endif
@endforeach
</select>
</div>
</label>
</div>
@endsection
Return HTML page:
<div class="col-md-3">
<label class="ls-label">
<b class="ls-label-text">Categorias</b>
<div class="ls-custom-select ls-field-md">
<select name="id" id="id" class="ls-select">
<option value="" selected="selected">TODOS</option>
<option value="1" data-type="main">Categoria indefinida</option>
<option value="2" data-type="main">INTERRUPTOR OLEO </option>
<option value="3" data-type="main">FERRAMENTAS EM GERAL</option>
<option value="4" data-type="main">Oléo</option>
<option value="5" data-type="main">Oleos</option>
<option value="7" data-type="main">BOMBAS</option>
</select>
</div>
</label>
</div>
So I foreach this return in my view, if I access $var['id'] I get the id returned, but when accessing $var['sub_categories'] the return is null, I tried several ways to get these values but without success. Is there something I should do because of the way this information was generated? How can I get sub_categories values?
Put the
View
!!! becausesub_categories
is aarray
!– novic
inserted in the question
– Renne Galli
This happens only once or throughout the listing??? looking so could not catch the error.
– novic
throughout the listing, only "printed" the categories considered "main", does not enter the loop of the subs, seeing the return of the data, I see no problem in not being able to access this value, until pq I have other similar returns and access normally as is in my view, but this one I can not access this info
– Renne Galli
If you have how to put the final result? type the html produced.?
– novic
insert database data and html return
– Renne Galli
Boy, now that I’ve seen some weird stuff on your controller is just a comeback like this:
return $tree_categories;
????– novic
yes, in this return comes all the data of the table already with the indicated subs, I put only one data in the question as example, but in the return comes all correctly, the problem is just access this information to display it in the view
– Renne Galli
For me to tell you anything would have to go through a fine-tooth comb, because, imagine you mismatched data, theoretically correct code, but, now I saw a strange bit in the return so it can be so much ... !!! It’s hard to tell if you see a bigger part.
– novic
I understand, I appreciate the attention.
– Renne Galli
I was able to solve kkkk, the correct way to access this information would be: $variavel->subcategories seeing the whole array, the name is presented in another way, which was the one I was trying to access, however it must be accessed via relationship function
– Renne Galli