-2

By using blade's extend, we can easily get the complete code of previous template and can use it in other template. For example we can use same code in create and update form.

+--------+-----------+------------------+--------------+---------------------------------------------+--------------+
| Domain | Method    | URI              | Name         | Action                                      | Middleware   |
+--------+-----------+------------------+--------------+---------------------------------------------+--------------+
|        | GET|HEAD  | /                |              | Closure                                     | web          |
|        | GET|HEAD  | api/user         |              | Closure                                     | api,auth:api |
|        | GET|HEAD  | todo             | todo.index   | App\Http\Controllers\TodoController@index   | web          |
|        | POST      | todo             | todo.store   | App\Http\Controllers\TodoController@store   | web          |
|        | GET|HEAD  | todo/create      | todo.create  | App\Http\Controllers\TodoController@create  | web          |
|        | GET|HEAD  | todo/{todo}      | todo.show    | App\Http\Controllers\TodoController@show    | web          |
|        | PUT|PATCH | todo/{todo}      | todo.update  | App\Http\Controllers\TodoController@update  | web          |
|        | DELETE    | todo/{todo}      | todo.destroy | App\Http\Controllers\TodoController@destroy | web          |
|        | GET|HEAD  | todo/{todo}/edit | todo.edit    | App\Http\Controllers\TodoController@edit    | web          |
+--------+-----------+------------------+--------------+---------------------------------------------+--------------+
Tim Lewis
  • 27,813
  • 13
  • 73
  • 102
ViShU
  • 49
  • 8
  • 1
    Please add more context to your question. – ahinkle Apr 28 '23 at 16:22
  • 2
    `{!! Form::open(['route' => $item ? ['todo.edit', $item->id] : 'todo.create']) !!}` would also work, and actually uses your Routes as defined by name. Also, consider moving away from this package; it used to be part of Laravel's default packages, but was removed in `5.x`: https://stackoverflow.com/questions/35695949/why-are-form-and-html-helpers-deprecated-in-laravel-5-x (it still can be used, but bloats Laravel, which is already bloated a bit, and is less customizable/controllable vs plain HTML) – Tim Lewis Apr 28 '23 at 16:35
  • @TimLewis Your form open stmt has one issue and i.e. **$item** which is not going to get it in case of **'todo.create'** and then it throw error of **Undefined variable: item** – ViShU Apr 28 '23 at 16:59
  • It depends what `$item` is, which you included no context for in your question... If `$item` is `null` or an instance of a `Todo` item, then my code will work. If `$item` is always a `Todo` model instance (`new Todo()` vs `Todo::find()`), then it'll need to be adjusted to `$item->exists ? ... : ...`. If you want a more specific example, then you need to [edit your question](https://stackoverflow.com/posts/76131458/edit) and include more details, specifically what `$item` is in a `create` and `edit` context. – Tim Lewis Apr 28 '23 at 17:02
  • For reference, `$model->exists` returns `true` if the Model has been saved to the Database, or `false` if it has not, and is a more reliable way of checking persistence than checking `$item->id`, since that can be set at runtime (i.e. `$model = new Model(); $model->id = 1;` is valid, and `$model->id ?? null` will be `1`, not `null`, but `$model->exists` will be `false`). – Tim Lewis Apr 28 '23 at 17:05

1 Answers1

0
{!! Form::open(['url'=>['todo',$item->id ?? null ]]) !!}

which is exactly similar to

@if (Route::currentRouteName() == 'todo.create')
    {!! Form::open(['url'=>'todo']) !!}
@elseif (Route::currentRouteName() == 'todo.edit') --}}
    {!! Form::open(['url'=>['todo',$item->id]]) !!}
@endif
ahinkle
  • 2,117
  • 3
  • 29
  • 58
ViShU
  • 49
  • 8