如何在Laravel中的产品创建页面中显示类别

时间:2016-06-07 08:22:20

标签: php mysql laravel-4

我在Laravel管理员后端中有这个表单,它将新产品添加到数据库中。

productCreate.blade.php

 {{ Form::open(['files' => true]) }}
<div class="form-group">
    <label for="title" class="control-block">Product title:</label>
    {{ Form::text('title', null, ['class' => 'form-control']) }}
</div>

<div class="form-group">
    <label for="title" class="control-block">Product description:</label>
    {{ Form::textarea('description', null, ['class' => 'form-control']) }}
    <span class="help-block">HTML is allowed.</span>
</div>

<div class="form-group">
    <label for="title" class="control-block">Product price:</label>
    {{ Form::text('price', null, ['class' => 'form-control']) }}
</div>

<div class="form-group">
    <label for="title" class="control-block">Assign Product to Category:</label>
    <select class="form-control">
        <option value="one">Category 1</option>
        <option value="two">Category 2</option>
        <option value="three">Category 3</option>
        <option value="four">Category 4</option>
        <option value="five">Category 5</option>
    </select>
</div>

<div class="form-group">
    <label for="title" class="control-block">Product image:</label>
    {{ Form::file('image', ['class' => 'form-control']) }}
</div>

<hr />

<button type="submit" class="btn btn-primary">Create new product</button>

{{ Form::close() }}

我的问题是如何从数据库中显示此选择/选项值。我希望能够将产品分配到类别。我应该在哪里查询以及如何在视图中添加它们?

在admincontroller中,我有产品创建功能,可以加载视图

public function productsCreate() {
    return View::make('site.admin.products_create');
}

我试图像这样做

public function productsCreate() {
    $categories = Categories::paginate(15);
        return View::make('site.admin.products_create', [
            'categories' => $categories
        ]);
    //return View::make('site.admin.products_create');
}

然后我收到了消息

production.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Cannot redeclare AdminController::productsCreate() ...

那么我怎样才能查询数据库表类别并在表单中显示类别?

可能是因为我已在管理控制器中选择了类别?

public function categories() {
    $categories = Categories::all();
    return View::make('site.admin.categories', [
        'categories' => $categories
    ]);
}

public function productsCreate() {
//      $categories = Categories::paginate(15); 

        return View::make('site.admin.products_create', [
            'categories' => $categories
        ]);
    //return View::make('site.admin.products_create');
}

1 个答案:

答案 0 :(得分:2)

您传递了productsCreate函数中的类别,并且必须删除旧的productsCreate函数,并将其声明为两次:

public function productsCreate() {
    $categories = Categories::get();
    return View::make('site.admin.products_create', [
        'categories' => $categories
    ]);
}

对于html:

<div class="form-group">
<label for="title" class="control-block">Assign Product to Category:</label>
<select class="form-control">
    @foreach($categories as $categorie)
    <option value="{{ $categorie->id }}">{{ $categorie->name }}</option>
    @endforeach
</select>

我希望这会对你有帮助。