【问题标题】:Add Sub Category - Laravel 5.2添加子类别 - Laravel 5.2
【发布时间】:2016-04-03 16:58:23
【问题描述】:

我需要在我的主要类别中插入子类别。我已经完成了显示、添加、编辑和删除父类别的所有工作。但现在我坚持如何将子类别实际添加到我的父母类别之一。

这是类别和子类别的表格。

如您所见,我在 iPhone 下已经有一个子类别,是我通过数据库手动添加的。要将子类别添加到主类别,我只需单击 + 子类别链接,该链接会将我带到添加子类别的表单。

这是我展示和添加子类别的路线:

Route::group(["middleware" => 'admin'], function(){

    /** More category routes here -->, just hidden for shortness **/


    /** Show the Admin Add Sub-Categories Page **/
    Route::get('admin/categories/addsub/{id}', [
        'uses' => '\App\Http\Controllers\CategoriesController@addSubCategories',
        'as'   => 'admin.category.addsub',
        'middleware' => ['auth'],
    ]);


    /** Post the Sub-Category Route **/
    Route::post('admin/categories/postsub/{id}', [
        'uses' => '\App\Http\Controllers\CategoriesController@addPostSubCategories',
        'as'   => 'admin.category.postsub',
        'middleware' => ['auth'],
    ]);


});

这是我的 CategoriesController.php:

缩短只是为了显示子类别功能。这是我无法将子类别添加到父类别的地方

class CategoriesController extends Controller
    /**
     * Return the view for add new sub category
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function addSubCategories($id) {

        $category = Category::findOrFail($id);

        return view('admin.category.addsub', compact('category'));
    }


    /**
     * @param $id
     * @param CategoryRequest $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function addPostSubCategories($id, CategoryRequest $request) {

        // Find the Parent Category ID
        $category = Category::findOrFail($id);

        // Insert into categories where the Parent_id = to the category ID
        $categories = Category::where('parent_id', '=', $category);


        // Assign $category to the Category Model, and request all validation rules
        $categories = new Category($request->all());

        // Then save the newly created category in DB
        $categories->save();

        // Flash a success message
        flash()->success('Success', 'Sub Category added successfully!');

        // Redirect back to Show all categories page.
        return redirect()->route('admin.category.show');
    }

}

我的 Category.php 模型:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{

    protected $table = 'categories';

    protected $fillable = ['name'];

    protected $guarded = ['id'];


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



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

}

我的添加子类别表单:

 <form role="form" method="POST" action="{{ route('admin.category.postsub', $category->id) }}">
                    {{ csrf_field() }}
                    <li class="collection-item blue">
                        <h5 class="white-text text-center">
                            Sub-Category to {{ $category->name }}
                        </h5>
                    </li>
                    <li class="collection-item">
                        <div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
                      <input type="text" class="form-control" name="name" value="{{ old('name') }}" placeholder="Add Sub-Category">
                            @if($errors->has('name'))
                                <span class="help-block">{{ $errors->first('name') }}</span>
                            @endif
                        </div>
                    </li>
                    <li class="collection-item blue">
                        <div class="form-group text-center">
                            <button type="submit" class="btn btn-link grey lighten-5">Create Sub-Category</button>
                        </div>
                    </li>
                </form>

还有我的数据库结构:

我在我的 CategoriesController 中的 addPostSubCategories() 函数中特别需要帮助,因为现在如果我添加一个新的 SUB 类别,它只会添加一个新的父类别,而不是子类别

【问题讨论】:

    标签: php mysql laravel laravel-5.2


    【解决方案1】:

    访问这里查看详细说明:https://laravel.com/docs/5.2/eloquent-relationships#inserting-related-models

    这就是你想要的:

    /**
     * @param $id
     * @param CategoryRequest $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function addPostSubCategories($id, CategoryRequest $request) {
    
        // Find the Parent Category
        $category = Category::findOrFail($id);
    
        // Create the new Subcategory
        $subcategory = new Category($request->all());
    
        // Save the new subcategory into the relationship
        $category->children()->save($subcategory);
    
        // Flash a success message
        flash()->success('Success', 'Sub Category added successfully!');
    
        // Redirect back to Show all categories page.
        return redirect()->route('admin.category.show');
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-16
      • 2016-04-12
      • 2016-07-23
      • 2017-09-05
      • 2019-01-02
      • 2016-11-07
      • 1970-01-01
      • 2019-01-17
      相关资源
      最近更新 更多