Jordan Vit Jordan Vit - 6 months ago 96
SQL Question

How to show categories in product creating page in Laravel

I have this form in the Laravel admin backend which adds new products to database.

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


My question is how to show this select/options values from database. I want to be able to assign products to categories. Where I should make query and how to add them here in the view?

In admincontroller i have functions for product creation which load the view

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


and I tried to make it like this

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


but then I got message

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


So how exactly I can query database table categories and show categories in the form?

May be because I already have categories selection in admin controller?

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');
}

Answer

You pass the categories in theproductsCreate function, and you must remove the old productsCreate function, you declared it twice :

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

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

I hope that will help you.