I-AM-OBODO Posted April 3, 2023 Share Posted April 3, 2023 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 Quote Link to comment Share on other sites More sharing options...
requinix Posted April 3, 2023 Share Posted April 3, 2023 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. Quote Link to comment Share on other sites More sharing options...
I-AM-OBODO Posted April 3, 2023 Author Share Posted April 3, 2023 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 Quote Link to comment Share on other sites More sharing options...
requinix Posted April 3, 2023 Share Posted April 3, 2023 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. Quote Link to comment Share on other sites More sharing options...
I-AM-OBODO Posted April 3, 2023 Author Share Posted April 3, 2023 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 Quote Link to comment Share on other sites More sharing options...
requinix Posted April 4, 2023 Share Posted April 4, 2023 9 hours ago, I-AM-OBODO said: I have already called the CreateProduct controller but the right syntax to use to get the category is where I am having problem The correct syntax is demonstrated in the link I gave. Quote Link to comment Share on other sites More sharing options...
I-AM-OBODO Posted April 6, 2023 Author Share Posted April 6, 2023 Thanks for the pointer. Quote Link to comment Share on other sites More sharing options...
gizmola Posted April 7, 2023 Share Posted April 7, 2023 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. 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.