1
votes

I am trying to access a page using a nested url in Laravel 5.1 but I have reached a dead end. I would like to make a GET request with a parameter in the middle of the url. To be precise, cars/{cars}/edit. This is my code:

In the routes file

Route::resource('cars', 'carController');

In the cars controller file

class carController extends Controller
{

    public function index(){

       $cars = Car::all();
       return view('carshome', compact('cars'));
    }

    public function edit($id){  
        return 'Welcome:  '.$id.'page';
    }
}

In the carshome blade template file

@foreach ($cars as $car)
<tr>
    <td>{{ $car->name }}</td>
    <td>{{ $car->type }}</td>
    <td class="text-center">
        <a href = {{url('/cars',[$car->name])}}>
           <i class="fi-clipboard-pencil"></i>
        </a>
        <a href = {{url('/cars',[$car->name])}}>
           <i class="fi-x-circle"></i>
        </a>
    </td>
</tr>
@endforeach

In the car model file

class Car extends Model
{
    protected $fillable = [
        'name', 'type'
    ];
}

The helper function url can take parameters as part of the url. Am not sure how I can it to create the custom url. How can I access the url resource using blade?

1
tbh, using Route::resource('cars', 'carController'); would save you from using url(..). you could use route('cars.edit', [$car->name]) simple as that. ps. confirm it with documentation on restful resource controller. - Bagus Tesa
As @Tezla wrote, but you can still use url(), like url('cars', [$car->id, 'edit']). - Iamzozo
Wow, thanks @Tezla, it works. I don't know where to click to mark your comment as correct. - Adwin
Thank you, @lamzozo. Your answer is equally right. - Adwin

1 Answers

1
votes

This is just to mark the question as answered. As @Tezla shared: In the carshome blade template file I can use:

route('cars.edit', [$car->name])

@lamzozo suggested another working method to use in the carshome blade template file:

url('cars', [$car->name, 'edit'])