Jump to content

Populate select field


I-AM-OBODO

Recommended Posts

Hi all. I have two tables. categories and products. On the create product page, I have a select dropdown field that I want to populate all the available categories. How can I archive that?

This is what I did but I am having error:

MY FORM

<form> 
  <select name="cat_name" > 
    <option value="">Select One</option> @foreach($categories as $category) <option value="{{ $category->cat_name}}"> {{ $category->cat_name}} </option> @endforeach 
  </form>

MY CONTROLLER

use App\Http\Controllers\ProductController; 
use App\Http\Controllers\CategoryController;


public function create() { return view('products.create', [ $result = (new CategoryController)->Category() ]);
} }

Thanks

Link to comment
Share on other sites

3 hours ago, requinix said:

What is this, Laravel?

Get the list of categories in your controller and pass them to your view. Try writing the code for that and we'll see how it goes.

Yes. It's Laravel. I can get this list in the CategoryController but if I try to get it from the create page which happens to be on ProductCategory I couldn't, cos obviously the category is not defined in in the ProductController.

Basically what i want to achieve is select a category while filling the product form

Link to comment
Share on other sites

1 hour ago, requinix said:

Then the issue here may just be your syntax:

return view('products.create', [ $result = (new CategoryController)->Category() ]);

Compare that with how the online docs use the view() function.

Yes I know my syntax is the problem. How can I get the right syntax to use? I have already called the CreateProduct controller but the right syntax to use to get the category is where I am having problem

Thanks 

Link to comment
Share on other sites

Controllers are not models.  

Having a controller that handles CRUD for Categories is irrelevant to  controller built for products (that happen to include Categories.

  • In your product controller, you should use eloquent to query for the categories.

I'd expect to see something like this:

 

$categories = Categories::all();

return view('products.create', ['categories' => $categories]);

 

  • Then in your product blade template you will build the category drop down in the list using something like:
<select name="category" id="category">
  @foreach ($categories as $category)
    <option value="{{ $category->id }}">{{ $category->name }}</option>
  @endforeach
</select>

It's good to put this into its own partial template and include it, if you expect to use this type of code in multiple places.  That way any style classes you might add or changes, will be reflected throughout the system, rather than having to go through and change the same code multiple times.  

So you will want to put code similar to what I have shown in it's own blade file, which you might name category.blade.php.  Make sure you understand where you need to put templates and how you will reference them when including.

So your product.blade.php template would include category.blade.php using something like this:

<form method="POST" action="{{ route('product.new') }}">
  // whatever other stuff
  @include(category)
  <button name="submit" type="submit">Submit</button>
</form>

 

This assumes that you have a method in your product controller that has a named route setup for it, pointing to product.new.  Hopefully you get the idea.

  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.