php - laravel redirect to page based on id from database is selected
one text
I'm new to laravel And want help on how to do this! goal: when I click on the city name from the select option it will route me to a page that has all villas in that city.
So I have the website controller as follows:
class WebsiteController extends Controller
{
public function index(){
$users = Villa::with('City','Seller', 'Payment')->get();
return view('website', compact('users'));
}
the website blade:
<select class="form-select">
<option value="" id="searchoption"> City </option>
@foreach($users as $villa )
<option id="searchoption" value="{{ $villa->City->id }}" >
{{ $villa->City->name }}
</option>
@endforeach
</select>
This code probably doesn't give me all cities' names based on id! here villa model:
class Villa extends Model
{
use HasFactory;
protected $fillable=[
"id", "title", "description", "price", "state", "status", "city_id", "seller_id", "payment_id", "created_at", "updated_at"
];
public function City()
{
return $this -> hasOne(City::class,'id','city_id');
}
public function Seller()
{
return $this -> hasOne(Seller::class,'id','seller_id');
}
public function Payment()
{
return $this -> hasOne(Payment::class,'id','payment_id');
}
}
and here's the route :
Route::get('/',[WebsiteController::class,'index'])->name('home.index');
I want a way when I click on the city select option to show me all cities' names based on id from the database cities table and when I select one to route me on a page that shows all villas in that city.
Source