[英]Add Sub Category - Laravel 5.2
我需要在我的主要类别中插入子类别。 我已经完成了显示,添加,编辑和删除父类别的所有工作。 但是现在我一直沉迷于如何将子类别实际添加到我的父母类别之一中。
这是类别和子类别的表格的外观。
如您所见,我已经在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类别,它只会添加一个新的Parent类别,而不是一个子类别
请访问此处以查看详细说明: 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');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.