[英]how to retrieve data from database into input values of a form in laravel php to complete the update action?
I'm trying to populate data into input values from mysql database so that I can see and change the data. 我正在尝试将数据填充到mysql数据库的输入值中,以便可以查看和更改数据。
I have a form named form.blade.php which will be included in both create.blade.php and edit.blade.php . 我有一个名为form.blade.php的表单,它将同时包含在create.blade.php和edit.blade.php中 。 What I need is to know is how to retrive data from database to populate data to those inputs when I click on edit.Then when I click on Add button in index.blade.php ,I'll be redirected to create.blade.php and then inputs must be empty.
我需要知道的是如何在单击Edit时从数据库中检索数据以将数据填充到这些输入中。然后,当我单击index.blade.php中的 Add按钮时,将重定向到create.blade.php然后输入必须为空。
I tried to add some code functions to every input value 我试图向每个输入值添加一些代码功能
{{old('title',$page->title}} then {{ isset($page) ? page->title: ''}}
but its not working 但它不起作用
I'm using one form to perform create and update actions 我正在使用一种形式来执行创建和更新操作
This is the edit.blade.php 这是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
and this is the child form form.blade.php 这是子表单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">
When you are using the same form for create and update, you can use the optional method. 当您使用相同的表单进行创建和更新时,可以使用可选方法。
<input type="text" class="form-control{{ $errors->has('date') ? ' is-invalid' : '' }}" name="date" id="date" value="{{ old('date', optional($expense)->date) }}">
In the create function send the variable as null like $expense = null;
在create函数中,将变量作为null发送,例如
$expense = null;
And in edit function the variable has its own value from database. 在编辑功能中,变量具有来自数据库的自己的值。 So when the variable has its value the input field will be filled by value from database otherwise it will be empty.
因此,当变量具有其值时,输入字段将由数据库中的值填充,否则它将为空。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.