me10071990
5 months ago

Categories and Subcategories

Posted 5 months ago by me10071990

Hey!

I'm editing a my website on local host and I have a db table called categories which has the following fields:

id parent_id name

My model looks like

public function children() {
    return $this->hasMany(Category::class, 'parent_id', 'id');
}

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

and my controller looks

public function store(Request $request) {

    $this->validate($request, [

        'name' =>'required'

    ]);




    $category= new Category;

    $category= Category::with('children')->whereNull('parent_id')->get();
    
    $category->name =$request->name;

    $category->save();

}

and my create.blade.php for category

@extends('layouts.app')

@section('content')

<!-- End Sidebar scroll-->
</aside>
<!-- ============================================================== -->
<!-- End Left Sidebar - style you can find in sidebar.scss  -->
<!-- ============================================================== -->
<!-- ============================================================== -->
<!-- Page wrapper  -->
<!-- ============================================================== -->
<div class="page-wrapper">
    <!-- ============================================================== -->
    <!-- Container fluid  -->
    <!-- ============================================================== -->
    <div class="container-fluid">
        <!-- ============================================================== -->
        <!-- Bread crumb and right sidebar toggle -->
        <!-- ============================================================== -->

        <!-- ============================================================== -->
        <!-- End Bread crumb and right sidebar toggle -->
        <!-- ============================================================== -->
        <!-- ============================================================== -->
        <!-- Start Page Content -->
        <!-- ============================================================== -->
        <!-- Row -->
        <div class="row">
            <div class="col-lg-12">
                <div class="card">
                    <div class="card-header bg-info">
                        <div class="text-center">
                            <h4 class="m-b-0 text-white">


                                <div class="panel panel-default">
                                    <div class="panel-heading" >
                                        <div class="text-center">

                                            <b>Create new Menu</b>
                                        </div>
                                    </div>

                                    <div class="panel-body">
                                        <form action="{{route('category.store')}}" method="post">
                                            {{csrf_field()}}
                                            <div class="form-group">

                                                <div class="text-center table-dark">
                                                    <label for ="Name" class="table-dark ">Name</label>


                                                </div>

                                                <input type="text" name="name" class="form-control">

                                            </div>

                                            <div class="form-group">
                                                <div class="text-center">
                                                    <button class="btn btn-success" type="submit">

                                                        Add Menu
                                                    </button>
                                                </div>
                                            </div>

                                        </form>

                                    </div>
                                </div>


                            </h4>
                        </div>
            </div>
        </div>
            </div>
        </div>
    </div>
</div>

        <!-- Row -->

@stop

I'm not really sure of the best way of achieving this and hoping someone can help point me in the right direction :-)

Please sign in or create an account to participate in this conversation.