Laravel 5.7 using same url action for insert and update with @yield in blade template
I am using laravel 5.7 and I am using one form for inserting and updating. In form action I want to use @yield() for laravel route to get the id for updation. Every thing is fine but I can’t use @yield() method. Here is my code, problem is only in action of the form.
<form class="form-horizontal" action="{{ url('/todo/@yield('editId')') }}" method="post"> {{csrf_field()}} @section('editMethod') @show <fieldset> <div class="form-group"> <div class="col-lg-10"> <input type="text" name="title" placeholder="Title" value="@yield('editTitle')" class="form-control"> </div> </div> <div class="form-group"> <div class="col-lg-10"> <textarea class="form-control" placeholder="Body" name="body" rows="5" id="textarea">@yield('editBody')</textarea> <br> <button type="submit" class="btn btn-success">Submit</button> </div> </div> </fieldset> </form>
I have also checked with single and double quotes.
action="/todo/@yield('editid')"
When I use simple this method then after submitting it redirects me to localhost and with an error page not found. In laravel 5.4 it works. but not in laravel 5.7. Any help would be appreciated Thanks
Here is my edit.blade.php from where I am using the @section and @yield
@extends('Todo.create') @section('editId',$item->id) @section('editTitle',$item->title) @section('editBody',$item->body) @section('editMethod') {{ method_field("PUT") }} @endsection
Controller store edit and update methods are
public function store(Request $request) { $todo = new todo; $this->validate($request,[ 'body'=>'required', 'title'=>'required|unique:todos', ]); $todo->body = $request->body; $todo->title = $request->title; $todo->save(); return redirect("todo"); } public function edit($id) { $item = todo::find($id); return view("Todo.edit",compact('item')); } public function update(Request $request, $id) { $todo = todo::find($id); $this->validate($request,[ 'body'=>'required', 'title'=>'required', ]); $todo->body = $request->body; $todo->title = $request->title; $todo->save(); return redirect("/todo"); }
Answer
To answer the OP actual question you would need to do
@section('editId', "/$item->id") or @section('editId', '/'.$item->id') {{ url('/todo') }}@yeild('editId')
But much better to do
{{ url('/todo/'.(isset($item) ? $item->id : '')) }}
Or for PHP >= 7
{{ url('/todo/'.($item->id ?? '')) }}