Home > Software design >  Creating a dependent drop down using laravel livewire
Creating a dependent drop down using laravel livewire

Time:07-09

I am trying to use laravel livewire to create a dynamic dependent drop down list. So I have created a livewire component named assignment.

assignment.blade.php

<div>
    <div >
        <label for="category" >Category</label>
        <div >
            <select name="category" id="category" >
                <option value="null">Select category</option>
                @foreach ($categories as $item)
                    <option value="{{$item->id}}">{{$item->name}}</option>
                @enforeach
            </select>
        </div>
    </div>     

                
    <div >
        <button  wire:loading.attr="disable">Add New Product</button>
        <div wire:loading>
            Hold on...
        </div>
    </div>
</div>

assignment.php

<?php

namespace App\Http\Livewire;

use Livewire\Component;
use App\Models\Category;

class Assignment extends Component
{
    public function render()
    {
        return view('livewire.assignment', [
            'Category' => Category::all()
        ]);
    }
}

The livewire component is embedded on the create.blade.php. Here is a snippet

 </div>

            <livewire:assignment />

            <div >
                <label for="subcategory" >Sub Category</label>
                <div >
                    <select name="subcategory" id="subcategory" >
                        <option value="null">Select subcategory</option>
                        <option value="wine">Wine</option>
                        <option value="whisky">Whisky</option>

Here is my productscontroller function that renders the view

public function create()
    {

        return view('products.create');
    }

routes

Route::get('/explore', [App\Http\Controllers\ConnectsController::class, 'index']);

Route::get('/p/create', [App\Http\Controllers\ProductsController::class, 'create']);

Route::post('/p', [App\Http\Controllers\ProductsController::class, 'store']);

I am getting the following error: syntax error, unexpected end of file. What could be the issue?

CodePudding user response:

There is a typo in assignment.blade.php. @enforeach should be @endforeach. I had to post this as answer as could not post this as comment.

  • Related