我正在使用laravel 5.7,并且正在使用一种表单进行插入和更新。在表单操作中,我想对laravel路由使用@yield()以获得更新的ID。一切都很好,但是我不能使用@yield()方法。这是我的代码,问题仅在表单上起作用。
<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>
我还检查了单引号和双引号。
action="/todo/@yield('editid')"
当我使用简单的此方法时,提交后将其重定向到localhost,并且未找到错误页面。在laravel 5.4中有效。但不是在laravel 5.7中。任何帮助将不胜感激,谢谢
这是我使用@section和@yield的edit.blade.php
@extends('Todo.create')
@section('editId',$item->id)
@section('editTitle',$item->title)
@section('editBody',$item->body)
@section('editMethod')
{{ method_field("PUT") }}
@endsection
控制器存储的编辑和更新方法是
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");
}
答案 0 :(得分:0)
如apokryfos所述-@yield被认为可以简化模板的重用。
如果您只是想确定(例如)之后应该调用哪个操作,则最好执行以下操作:
@extends('Todo.create')
<form class="form-horizontal" action="/todo/{{ isset($item) ? $item->id : '' }}" method="post">
@if( ! isset($item))
{{ method_field("PUT") }}
@else
{{ method_field("PATCH") }}
{{csrf_field()}}
<fieldset>
<div class="form-group">
<div class="col-lg-10">
<input type="text" name="title" placeholder="Title" value="{{ isset($item) ? $item->title : '' }}" 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">{{ isset($item) ? $item->body : '' }}</textarea>
<br>
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</fieldset>
</form>
我还记得方法字段应始终排在最前面,以确保正确识别它。另外,您不需要url()来生成我认为的url。 无需第二个刀片。只需将变量直接注入模板中,并在访问它们之前确保已设置它们。我没有尝试过,但我认为它应该可以工作。
答案 1 :(得分:0)
要回答OP实际问题,您需要做
@section('editId', "/$item->id") or @section('editId', '/'.$item->id')
{{ url('/todo') }}@yeild('editId')
但是要做的更好
{{ url('/todo/'.(isset($item) ? $item->id : '')) }}