me10071990
5 months ago

I have dynamic category and subcategory on my website, I am able to post my data in menu page only but don't have idea to how to post data under subcategory.

Posted 5 months ago by me10071990

This is my Controller for post..

public function store(Request $request) { $this->validate($request, [

        'title' =>'required',
        'opt_1'=>'required',
        'opt_2'=>'required',
        'featured'=>'mimes:jpeg,pdf,docx,png:5000',
        'file'=>'mimes:jpeg,pdf,docx,png:5000',
        'content'=>'required',
        'category_id'=>'required',


    ]);

    // Create Initial Required Data ARray
    $data = [

        'opt_1'=>$request->opt_1,
        'opt_2'=>$request->opt_2,
        'title'=>$request->title,
        'content'=>$request->content,
        'category_id'=>$request->category_id,


        'slug'=>str_slug($request->title)
    ];

    if(request('opt_1'))
    {
        $opt_1=request('opt_1');

    }




    // Optionally add 'featured' if found to the Data array
    if (request('featured'))
    {
        $featured = request('featured');
        $file_name = time() . $featured->getClientOriginalName();
        $featured->move('uploads/posts', $file_name);
        $data['featured'] = 'uploads/posts/'.$file_name;
    }

    // Optionally add 'file' if found to the Data array
    if (request('file')) {
        $file = request('file');
        $file_name = time() . $file->getClientOriginalName();
        $file->move('uploads/posts', $file_name);

// $content = base64_encode(file_get_contents($_FILES['pdf'['tmp_name']));

        $data['file'] = 'uploads/posts/'.$file_name;


        //$data->content=$content;
    }



    // Create the Post with the $data Array




    $post = Post::create($data);


    Session::flash('success', 'New Blog has been Published on Website for Particular Menu');

    return redirect()->back();

}

Create.blade.php

@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">

                        </div>

                        @if(count($errors)>0)
                            <ul class="list-group">
                                @foreach($errors->all() as $error)
                                    <li class="list-group-item text-danger">
                                        {{$error}}
                                    </li>
                                @endforeach
                            </ul>
                        @endif

                            <div class="panel-heading">

                                <div class="text-center text-white">
                                    <b> <h2>Create a new Blog</h2></b>
                                </div>
                            </div><BR>
                            <div class="panel-body">
                                <form action="{{route('post.store')}}" method="post" enctype="multipart/form-data">
                                    {{csrf_field()}}


                                    <div class="form-group table-dark text-white">
                                        <label for ="category"><h3>Select a Menu</h3></label>
                                        <select name="category_id" id="category" class="form-control" >
                                            @foreach($levels as $category)
                                                <option value="{{$category->id}}">{{$category->name}}</option>
                                            @endforeach
                                        </select>
                                    </div>





                                    <div class="form-group text-white">
                                        <label for ="title"><h3>Option1 date/Name Manually</h3></label>
                                        <input type="text" name="opt_1" class="form-control text-danger">
                                    </div>

                                    <div class="form-group text-white">
                                        <label for ="title"><h3>Option2 date/Name Manually</h3></label>
                                        <input type="text" name="opt_2" class="form-control text-danger">
                                    </div>

                                    <div class="form-group text-white">
                                        <label for ="title"><h3>Title</h3></label>
                                        <input type="text" name="title" class="form-control text-danger">
                                    </div>


                                    <div class="form-group text-white">
                                        <label for ="featured"><h3>Image/Featured</h3></label> <input type="file" name="featured" class="form-control">
                                    </div>

                                    <div class="form-group text_white">

                                        <label for ="file"><h3>PDF file</h3></label><input type="file" name="file" class="form-control">
                                    </div>




                                    <div class="form-group text-white" >
                                        <label for ="content"><h3>Content</h3></label>
                                        <textarea name="content" id="content" cols="5" rows="5" class="form-control"> </textarea>
                                    </div>

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


                        </div>
                        @stop

                        @section('styles')

                            <link href="http://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.11/summernote.css" rel="stylesheet">

                        @stop

                        @section('scripts')

                            <script src="http://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.11/summernote.js"></script>


                            <script>
                                $(document).ready(function() {
                                    $('#content').summernote();
                                });

                            </script>
                        <!-- Row -->

                    javascript:
                    -------------
                    <script type="text/javascript">
                        $.ajaxSetup({
                            headers: {
                                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                            }
                        });

                        $(document).ready(function(){
                            $('#name').on('change',function(e){
                                console.log(e);
                                var parent_id= e.target.value;
                                $.getJSON('/your url/sub?parentID=' + parent_id, function(data){

                                    console.log(data);
                                    $('#sub_cat_id').empty();
                                    $.each(data,function(index, sub_cat_id){
                                        $('#sub_cat_id').append('<option value="'+sub_cat_id.id+'">'+sub_cat_id.name+'</option>');
                                    });
                                });

                            });
                        });
                    </script>


                    </div>
                </div>
            </div>
        </div>
        <!-- Row -->

@stop

Please suggest what I have to do to create the post for subcategory. thanks

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