如何在网站的子类别中发布数据

时间:2019-06-06 05:09:13

标签: laravel-5 eloquent laravel-5.2 laravel-5.1

我已经创建了类别表,并在parent_id的帮助下创建了子类别,现在我想在网站的子类别中发布数据,因为我能够为类别发布数据,但不知道如何为子类别进行发布。

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

@停止

FrontendController

              public function welcome()

{

    // @TODO Refactor This Line
    return view('welcome')

        ->with('title', Setting::first()->site_name)
        ->with('levels', Category::take(7)->get())
        ->with('first_post', Post::orderBy('created_at','desc')->first())
        ->with('second_post', Post::orderBy('created_at', 'desc')->skip(1)->take(1)->get()->first())
        ->with('third_post', Post::orderBy('created_at','desc')->skip(2)->take(2)->get()->first())
        ->with('forth_post', Post::orderBy('created_at','desc')->skip(3)->take(3)->get()->first())
        ->with('HOME', Category::find(1))
        ->with('ABOUT US', Category::find(2))
        ->with('RESEARCH', Category::find(3))
        ->with('NEWS AND PUBLICATION', Category::find(4))
        ->with('EVENTS', Category::find(5))
        ->with('PEOPLE', Category::find(6))
        ->with('CONTACT US',Category::find(7));

}

PostController。

            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();

}

0 个答案:

没有答案