Using Laravel 5.6 and MySQL in my application.
In my system I have saving some information about vehicles types like car
, van
, bus
, truck
. Some of the vehicles type have common values and those are saving in vehicle table, like this
id name model year category_name user_id
1 toyota 121 2001 car 1
2 nissan sunney 1998 car 2
3 toyota liteace 2000 van 5
4 isuzu elf 2005 truck 9
5 bmw 520d 2010 car 7
and some specific information for each vehicle type saving separate tables like cars, vans, trucks etc each table related with vehicle table with vehicle_id
car table like this
id fuel transmission vehicle_id
1 p auto 1
2 d mannual 7
3 e auto 6
4 p auto 5
5 p auto 3
and each user can access they created ads in there account and its VehicleController is like this,
public function indexpersonel(){
$vehicles = Vehicle::personal()->get();
return view('vehicles.myads')->withVehicles($vehicles);
}
and Vehicle Model
public function scopePersonal($query){
return $query->where('user_id', Auth::user()->id);
}
and each ads showing blade file is,
<td>
<a class="button is-outlined" href="{{route('vehicles.edit',$vehicule->id.'/edit/')}}" >Edit</a>
</td>
<td>
<a class="button is-outlined" href="/myads/{{$vehicule->id}}/delete" onclick="return confirm('Are you sure to want to delete this record?')" >Delete</a>
</td>
</a>
My problem is there currently is a vehicle information edit blade template (edit.blade.php) in vehicles view file. As a result, some specific values of each vehicles type unable to use above common edit form.
I need separate edit forms for each vehicle types like caredit, vanedit etc...
How can create and route with users accounts created ads as above. now I need urls when going to some vehicle edit like this,
http://localhost:8000/myads/car/18/edit,
http://localhost:8000/myads/van/20/edit
My current route for vehicle edit is,
Route::get('myads/{id}/edit', [
'uses' => '\App\Http\Controllers\VehicleController@edit',
'as'=> 'vehicles.edit'
]);
How can do this?
First of all your new url will be:
Route::get('myads/{vehicleType}/{id}/edit', [
'uses' => '\App\Http\Controllers\VehicleController@edit',
'as'=> 'vehicles.edit'
]);
In your edit method you can do something like this.
public function edit($vehicleType, $id) {
switch($vehicleType) {
case 'car':
return view('caredit');
case 'van':
return view('vanedit');
}
}
Or if you use the same string for the vehicle type in your url as the name of the view and pass the same variables, your function can look like this:
public function edit($vehicleType, $id) {
return view($vehicleType . 'edit');
}