我正在尝试通过使用Laravel构建的Web服务中的表单插入数据(日期类型数据)。
solicitacoes.blade.php代码:
@extends('layout.app', ["current" => "solicitacaos"])
@section('body')
<div class="container">
<div class="card-border">
<div class="card-body">
<h3 class="card-title">Solicitações</h3>
<table class="table table-striped table-bordered table-hover">
<tr>
<th> Professor
</th>
<th>
Criação
</th>
<th>
Data Solicitação
</th>
<th>
Nome do Produto
</th>
<th>
Observações
</th>
<th>
Status
</th>
<th>
Ação
</th>
</tr>
@foreach($sols as $sol)
@php
dd($sol)
@endphp
<tr>
<td> {{$sol->nome_professor}}
</td>
<td> {{$sol->criacao}}
</td>
<td> {{dd($sol)}}
</td>
<td> {{$sol->nome_produto}}
</td>
<td> {{$sol->observacao}}
</td>
<td> {{$sol->status}}
</td>
<td>
<a href="/solicitacao/apagar/{{$sol->id}}" class="btn btn-sn btn-danger">Cancelar</a>
</td>
</tr>
@endforeach
</table>
</div>
</div>
</div>
@endsection
错误在行上{{\\ Carbon \ Carbon :: parse($ sol-> solicitacao_data-> from_date)-> format('d / m / Y')}}“
即使我将行“ {{\ Carbon \ Carbon :: parse($ sol-> solicitacao_data-> from_date)-> format('d / m / Y')}}“更改为“ {{$ sol-> solicitacao_data }}“,我仍然遇到相同的错误。
控制器代码为:
$date_sol = new DateTime();
$date_sol = DateTime::createFromFormat("d/m/Y",$request->input('solicitacao_data'));
$sol->solicitacao_data = $date_sol;
$sol->save();
我正在插入“ 25/2/2020”,但出现以下错误:
Facade \ Ignition \ Exceptions \ ViewException未定义属性:
stdClass :: $ solicitacao_data(查看:
C:\ xampp \ htdocs \ laravel \ workflow-novo \ resources \ views \ solicitacoes.blade.php)
数据库表是通过迁移创建的,如下所示:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateSolicitacaosTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('solicitacaos', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('id_users')->unsigned();
$table->foreign('id_users')->references('id')->on('users');
$table->bigInteger('id_produto')->unsigned();
$table->foreign('id_produto')->references('id')->on('produtos');
$table->string('status');
$table->string('observacao');
$table->date('solicitacao_data');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('solicitacaos');
}
}
我需要使用Laravel存储和恢复日期。如何解决以上错误?
**我已经注意到,如果我尝试节省时间,数据库保存将无法正常进行。例如,以下代码:
$date_sol = Carbon::createFromFormat("d/m/Y H:i:s","20/12/2020 01:00:00");
$sol->solicitacao_data = $date_sol;
将以下数据存储在solicitacao_data列上。
MariaDB [login]> select * from solicitacaos;
+----+----------+------------+------------+------------+------------------+-----
----------------+---------------------+
| id | id_users | id_produto | status | observacao | solicitacao_data | crea
ted_at | updated_at |
+----+----------+------------+------------+------------+------------------+-----
----------------+---------------------+
| 24 | 1 | 1 | Em Análise | blabla | 2020-12-20 | 2019
-12-30 23:47:52 | 2019-12-30 23:47:52 |
| 25 | 1 | 1 | Em Análise | blabla | 2020-12-20 | 2019
-12-31 01:22:50 | 2019-12-31 01:22:50 |
| 26 | 1 | 1 | Em Análise | blabla | 2020-12-20 | 2019
-12-31 01:23:08 | 2019-12-31 01:23:08 |
| 27 | 1 | 1 | Em Análise | blabla | 2020-12-20 | 2019
-12-31 01:27:33 | 2019-12-31 01:27:33 |
+----+----------+------------+------------+------------+------------------+-----
----------------+---------------------+
4 rows in set (0.001 sec)
请注意,仅存储日期。
我在solicitacao.blade.php中执行了命令dd($ sol),我得到了以下内容:
{#341 ▼
+"id": 1
+"status": "Em Análise"
+"nome_professor": "Olavo"
+"criacao": "2019-12-31 02:08:11"
+"nome_produto": "Cadeira"
+"observacao": "awdcasd"
}
即使表中有我之前显示的列。
MariaDB [login]> select * from solicitacaos;
+----+----------+------------+------------+------------+------------------+-----
----------------+---------------------+
| id | id_users | id_produto | status | observacao | solicitacao_data | crea
ted_at | updated_at |
+----+----------+------------+------------+------------+------------------+-----
----------------+---------------------+
| 24 | 1 | 1 | Em Análise | blabla | 2020-12-20 | 2019
-12-30 23:47:52 | 2019-12-30 23:47:52 |
| 25 | 1 | 1 | Em Análise | blabla | 2020-12-20 | 2019
-12-31 01:22:50 | 2019-12-31 01:22:50 |
| 26 | 1 | 1 | Em Análise | blabla | 2020-12-20 | 2019
-12-31 01:23:08 | 2019-12-31 01:23:08 |
| 27 | 1 | 1 | Em Análise | blabla | 2020-12-20 | 2019
-12-31 01:27:33 | 2019-12-31 01:27:33 |
+----+----------+------------+------------+------------+------------------+-----
----------------+---------------------+
4 rows in set (0.001 sec)
参考方案
根据错误消息,您在solicitacoes.blade.php中调用名为$solicitacao_data
的属性,
您的stdClass $sol
没有属性solicitacao_data
,因此会发生错误。
确保通过查询选择列solicitacao_data
,
并且不要将此字段放在模型的$hidden
中。
有没有一种方法可以使div上的click事件与表单环境中的单选按钮相同?我只希望下面的div提交值,单选按钮很丑代码输出如下:<input id="radio-2011-06-08" value="2011-06-08" type="radio" name="radio_date…
PHP-复选框组 - php我有一个需要发布的表单复选框组。<input type="checkbox" value="true" checked name="chk0[]"> <input type="checkbox" value="false" name=…
php Singleton类实例将在多个会话中保留吗? - php举一个简单的例子,如果我想计算一个不使用磁盘存储的脚本的命中次数,我可以使用静态类成员来执行此操作吗?用户1:<?php $test = Example::singleton(); $test->visits++; ?> 用户2:<?php $test = Example::singleton(); $test->visits+…
故障排除“警告:session_start():无法发送会话高速缓存限制器-标头已发送” - php我收到警告:session_start()[function.session-start]:无法发送会话缓存限制器-标头已发送(错误输出开始如果我将表单数据提交到其他文件进行处理,则可以正常工作。但是,如果我将表单数据提交到同一页面,则会出现此错误。请建议<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0…
CodeIgniter更新查询被执行两次 - php我正在使用CodeIgniter 2.2。每次访问页面时,我都必须用+1更新数据库。代码可以工作,但是每次都会增加+2。示例:如果是total views=2,则在单击页面后total views应该是3,但是数据库中的值是4。我确定我在控制器中仅调用一次模型add_one_to_view_image。控制者 function view(){ $view_i…