简体   繁体   English

如何从数据库中检索数据到laravel PHP中表单的输入值中以完成更新操作?

[英]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.phpedit.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.

相关问题 PHP - 如何从数据库中检索数据并将其显示在表单中并对其进行更改和更新? - PHP - How can I retrieve data from a database and display it in a form and make changes to it and update it? php从mySQL数据库检索数据成表格 - php retrieve data from mySQL database into form 如何在php表单中发送输入数据? - How to send input data in the action of the form in php? 如何从数据库中检索数据作为 Laravel 中的数组? - How to retrieve data from a database as an array in Laravel? 从 Mysql 数据库中检索数据,显示在表单上,​​并允许用户使用 PHP 进行更新 - Retrieve data from Mysql database, display on a form, and allow user to update using PHP 如何从PHP操作更新数据库数量 - How to update quantity of database from php action 如何使用按钮更新php表单和数据库的值? - How to update values of php form and database with a button? 是否有使用laravel框架中的表单输入数据更新数据库中当前记录的功能? - Is there a function to update the current record in the database with form input data in laravel framework? 使用从表单将相同的变量名称输入从多个数据更新到Laravel数据库的单个列中 - Update multiple data using same variable name input from a form into single column in database in Laravel 从模态表单中检索值并更新数据库php - Retrieve value from modal form and update database php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM