How to count line with runquery(Laravel)

Asked

Viewed 30 times

-1

$id= $this->id;
    $consulta = DB::select('select *from locacao where id = ?',[$id]);

    $consulta->count();

I tried it :(but it didn’t work out the $query->Count(); I only find things related to the eloquent, but I didn’t like it, I’m using this runningquery method of Laravel, how can I know the Qt of lines returned in the query? :(

CODIGO TODO:
<?php
namespace App;
use Illuminate\Support\Facades\DB;

class LocacaoBanco {

    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 = 0;


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

            DB::insert('insert into locacao (datalocacao,datadevolucao,valorlocacao,estadodevolucao) values (?,?,?,?,?)',[$datalocacao,$datadevolucao,$valorlocacao,$estadodevolucao,$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() {
            $id= $this->id;
            $consulta = DB::select('select *from locacao where id = ?',[$id]);

            $consulta->count();

        }


}




?>
  • Guys?? Help me please

  • Lorena you’re using the wrong Laravel, it’s Complicated help you

1 answer

0

I’ll try to help you in the best way possible, you’re trying to use Laravel in a kind of "weird" way, so I’ll try to answer as many things as I see that I find wrong, all good ?

First question:

$id= $this->id;
    $consulta = DB::select('select *from locacao where id = ?',[$id]);

    $consulta->count();

What I’ve noticed is that you’re putting the entire query inside the select(), but it only serves to define which columns will be consulted.

The shape you’re using is a little strange, the correct would be more for it here:

$consulta = DB::table('locacao')->where('id', $this->id)->count();

if you were using some model for this lease it would be even easier.

What I did, I set the search table called the method where() and defined the parameters of the query.

I hope I’ve been able to help.

References:

https://laravel.com/docs/5.7/queries

https://laravel.com/docs/5.7/eloquent

Browser other questions tagged

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