Where do Eloqent Laravel Doesn’t Work

Asked

Viewed 130 times

3

I have the following code

public function getConsultaLocacao() 
{           

    $consulta =  LocacaoBanco::where('id',4)->get();

    foreach($consulta as $v) 
    {       
        echo"aqui->".$v->datalocacao;
    }
    echo "<br>";
    var_dump($v->datalocacao);
}

CODE RESULT

aqui->2019-01-01 00:00:00
string(19)"2019-01-01 00:00:00"

inserir a descrição da imagem aqui

Now look at my database, the id = 4 refers to another line and the laravel only returns me wrong.

id    datalocacao            datadevolucao          valorlocacao    estadodevolucao
4     2019-01-31 00:00:00    2019-01-31 00:00:00    11              nao

inserir a descrição da imagem aqui

Complete code of the model:

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class LocacaoBanco extends Model 
{
    protected $table = 'locacao';
    public $timestamps = false;
    public $datalocacao;
    public $datadevolucao;
    public $valorlocacao;
    public $estadodevolucao;
    public $id;

        public function __construct() {

            $this->datalocacao = "2019-01-01 00:00:00";
            $this->datadevolucao = "2019-01-01 00:00:00";
            $this->valorlocacao = 5.20;
            $this->estadodevolucao = "nao";
            $this->id = null;


        }
        public function setRegLocacao() {
                $datalocacao = $this->datalocacao;
                $datadevolucao = $this->datadevolucao;
                $valorlocacao = $this->valorlocacao;
                $estadodevolucao = $this->estadodevolucao;
                $id = $this->id;

            echo "Insercao da tabela locacao executada";

        }
        public function setAtualizaLocacao() {

                $datalocacao = $this->datalocacao;
                $datadevolucao = $this->datadevolucao;
                $valorlocacao = $this->valorlocacao;
                $estadodevolucao = $this->estadodevolucao;
                $id = $this->id;
                DB::update("update locacao SET datalocacao = ?,datadevolucao = ?,valorlocacao=?,estadodevolucao=?,id=? ",[$datalocacao,$datadevolucao,$valorlocacao,$estadodevolucao,$id]);
                echo "locacao atualizada";
        }
        public function getConsultaLocacao() {


        $consulta =  LocacaoBanco::where('id',4)->get();

        foreach($consulta as $v) {

            echo"aqui->".$v->datalocacao;


        }
        echo "<br>";
            var_dump($v->datalocacao);

        }

    }
}
?>
  • Hi @Lorena, do me a favor after the $consulta = LocacaoBanco::where('id',4)->get(); put this here dd($consulta); and tell me the result.

  • If you’re doing it wrong, you’re using Eloquent wrong.

1 answer

1

He’s not getting it wrong. He’s getting exactly what’s in the __construct

public function __construct() {
    $this->datalocacao = "2019-01-01 00:00:00";
    $this->datadevolucao = "2019-01-01 00:00:00";
    $this->valorlocacao = 5.20;
    $this->estadodevolucao = "nao";
    $this->id = null;
}

You should not have a constructor in a Model. Try to leave it as follows, following what is in the Model itself documentation of the Laravel

class LocacaoBanco extends Model {
    protected $table = 'locacao';
    public $timestamps = false;

    protected $fillable = [
            'datalocacao', 'datadevolucao', 'valorlocacao', 'estadodevolucao'
        ];
}

To perform the tests, you can use the Laravel Tinker, do not put the code in your Model that is not strictly his responsibility.

To make insertions in your table, use Locacaobanco::create. Ex.:

LocacaoBanco::create([
    'datalocacao' => '2019-01-31 00:00:00', 
    'datadevolucao' => '2019-01-31 00:00:00',
    'valorlocacao' => '11',
    'estadodevolucao' => 'nao'
])

And to perform queries, use Where normally, the way you used it. Perform tests on the terminal using Tinker php artisan tinker. Afterward:

use App\LocacaoBanco

$model= LocacaoBanco::find(4)

$model->datalocacao

Follow here too, um video where you can learn to make better use of Tinker.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.