【问题标题】:how to retrieve data from database into input values of a form in laravel php to complete the update action?如何将数据库中的数据检索到 laravel php 中表单的输入值以完成更新操作?
【发布时间】:2019-11-21 02:08:36
【问题描述】:

我正在尝试将数据填充到 mysql 数据库的输入值中,以便我可以查看和更改数据。

我有一个名为 form.blade.php 的表单,它将包含在 create.blade.phpedit.blade.php。 我需要知道的是当我单击编辑时如何从数据库中检索数据以将数据填充到这些输入中。然后当我单击 中的 添加按钮index.blade.php ,我将被重定向到 create.blade.php 然后输入必须为空。

我尝试为每个输入值添加一些代码函数

{{old('title',$page->title}}  then {{ isset($page) ? page->title: ''}}

但它不起作用

我正在使用一种表单来执行创建和更新操作

这是edit.blade.php

@extends('layouts.master')
@section('content')

    <div class="card">
        <div class="card-header card-header-success">
            <h4 class="card-title ">Pages Data Table</h4>
            <p class="card-category"> Here you can update Page data</p>
        </div>
        <div class="card-body">

            {!! Form::open(['action' => 'PagesController@store', 'method' => 'POST' ]) !!}
                @csrf
                @method('PUT')
                @include('pages.includes.form')
            {!! Form::close() !!}
    </div>
    </div>


@endsection

这是form.blade.php的子表单

    <div class="form-group">
        {{Form::label('title', 'Title')}}

  {{ Form::text('title', '',['class' => 'form-control', 'placeholder' => 'Title']) }}

    </div>
    <div class="form-group">
        {{Form::label('content', 'Content')}}
        <br>
        {{ Form::textarea('content', '',['class' =>'form-control', 'placeholder'=> 'Content']) }}
    </div>
    <div class="form-group">
            <div class="form-check form-check-radio">
                    <label class="form-check-label">
                            {{Form::input('radio','status', '0',['class' => 'form-check-input','checked'=>'checked'])}} Status Off
                        <span class="circle">
                            <span class="check"></span>
                        </span>
                    </label>
                </div>
                <div class="form-check form-check-radio">
                    <label class="form-check-label">
                            {{Form::input('radio','status', '1',['class' => 'form-check-input'])}} Status On
                        <span class="circle">
                            <span class="check"></span>
                        </span>
                    </label>
                </div>

    </div>

    <input type="submit" name="submit" class="btn btn-success" value="Save">

【问题讨论】:

  • 使用 Laravel Collective 的 Form 外观已经将 CSRF 插入到表单中。 ;)
  • 所以我不需要添加@csrf ?
  • 抱歉,这不是您的主要问题。但不,你没有。
  • 这应该可以工作:{{old('title') ?? $page-&gt;title }} |或者:你错过了这里的${{ isset($page) ? $page-&gt;title : ''}}
  • 语法错误,意外的 'title' (T_STRING),期待 ')' >>输入:{{ Form::text('title', '{{old('title') ?? $page-&gt;title }}' ,['class' =&gt; 'form-control' , 'placeholder' =&gt; 'Title']) }}

标签: php laravel eloquent


【解决方案1】:

当您使用相同的表单创建和更新时,您可以使用可选方法。

<input type="text" class="form-control{{ $errors->has('date') ? ' is-invalid' : '' }}" name="date" id="date" value="{{ old('date', optional($expense)->date) }}">

在创建函数中将变量作为 null 发送,例如 $expense = null; 并且在编辑功能中,变量具有来自数据库的自己的值。因此,当变量具有其值时,输入字段将由数据库中的值填充,否则将为空。

【讨论】:

  • 我收到一个错误 > 我怎样才能把它放在这个 html 输入中? ```{{ Form::text('title', '',['class' => 'form-control' , 'placeholder' => 'Title']) }}'''
猜你喜欢
  • 2017-09-05
  • 1970-01-01
  • 2017-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多