6

I am trying to insert data (date type data) through a form in a web service built with Laravel.

the solicitacoes.blade.php code:

    @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

The error is on the line " {{ \Carbon\Carbon::parse($sol->solicitacao_data->from_date)->format('d/m/Y')}} "

Even if I change the line " {{ \Carbon\Carbon::parse($sol->solicitacao_data->from_date)->format('d/m/Y')}} " to " {{$sol->solicitacao_data}} ", I still getting the same error.

The controler code is:

$date_sol = new DateTime();
$date_sol = DateTime::createFromFormat("d/m/Y",$request->input('solicitacao_data'));

$sol->solicitacao_data = $date_sol;

$sol->save();

I am inserting '25/2/2020', but I get the following error:

Facade\Ignition\Exceptions\ViewException Undefined property: stdClass::$solicitacao_data (View: C:\xampp\htdocs\laravel\workflow-novo\resources\views\solicitacoes.blade.php)

The database table is created through migrations, as follows:

<?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');
    }
}

I need to store and recover the date using Laravel. How can I make it without the error above?

**I have noticed that if I try to save the time, the database saving does not work properly. For example, the following code:

$date_sol = Carbon::createFromFormat("d/m/Y H:i:s","20/12/2020 01:00:00");

$sol->solicitacao_data = $date_sol;

stores the following data on solicitacao_data column.

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)

Note that only the date is being stored.

I have executed the command dd($sol) in the solicitacao.blade.php I have got the following:

{#341 ▼
  +"id": 1
  +"status": "Em Análise"
  +"nome_professor": "Olavo"
  +"criacao": "2019-12-31 02:08:11"
  +"nome_produto": "Cadeira"
  +"observacao": "awdcasd"
}

Even though the table has the columns I have shown before.

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)
5
  • please put your all controller coed Commented Dec 27, 2019 at 6:21
  • I have added some code to the question. Commented Dec 28, 2019 at 21:50
  • you need to provide the controller method that returns that view so we can see how you are retrieving the data since you are not using a Model Commented Dec 30, 2019 at 19:41
  • It is there. I say "the controler code is:" and the "solicitacoes.blade.php" code is the one that retrieves the data. It all there. Commented Dec 30, 2019 at 19:59
  • Do you still need help? Commented Dec 31, 2019 at 8:06

2 Answers 2

8

According to the Error message, you are call the attributes named $solicitacao_data in the solicitacoes.blade.php,

Your stdClass $sol have no attribute solicitacao_data, so the error occurs.

Make sure to select the column solicitacao_data by query,

and don't put this field in model's $hidden.

Sign up to request clarification or add additional context in comments.

16 Comments

I am not using "->$solicitacao_data" anywhere.
As the code I have posted above, I am using "$sol->solicitacao_data = $date_sol;"
I got what you mean. I have posted the solicitacoes.blade.php, where it uses "$sol->solicitacao_data->from_date".
It is probably where the error is. I do not know how to solve it, however.
@Siqueira I think you need to dd($sol) and post what $sol really is.
|
0
$date_sol = new DateTime();
    $date_sol = DateTime::createFromFormat("d/m/Y",$request->input('solicitacao_data'));

    $sol->solicitacao_data = $date_sol;

    $sol->save();

According to above code, there must be an input with name "solicitacao_data" on your blade file for $date_sol = DateTime::createFromFormat("d/m/Y",$request->input('solicitacao_data')); to work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.