Object of class Illuminate Database Eloquent Builder could not be converted to string. Standard 5.2

Asked

Viewed 2,272 times

2

I’m having problems with a relative search on Laravel. I give option of several searches on the screen, are they:

  • initial date
  • final date
  • status,
  • and whose is

but one can search in a unitary way, for example: I want to take all with status Active, or all of a particular person, and so on.

Two of this search are mandatory, ie in all cases will have the initial date and final.

In the Controller I’m getting this data I’m doing so:

$inicial = DateTime::createFromFormat('d/m/Y', $request->get('dataInic'))->format('Y-m-d');
$final = DateTime::createFromFormat('d/m/Y', $request->get('dataFim'))->format('Y-m-d');
$flgStatus =  $request->get('FlgStEncomenda');
$pessoa = $request->get('CdPessoa');
$clientes = Pessoa::lists('NmPessoa', 'id')->all();

$pedidos = Encomenda::where('created_at', '<=', $inicial)
                    ->where('created_at', '>=', $final)
                   .(($flgStatus == 'null')? '' : "->('FlgStEncomenda', ".$flgStatus.")")
                   .(($pessoa == '')? '->get()' : "->('CdPessoa', ".$pessoa.")");

When doing this the screen returns me the following error:

Object of class Illuminate Database Eloquent Builder could not be converted to string.

What is the best way to do this research, taking into account that the data may or may not come and this needs to be addressed when doing the research?

1 answer

2


The error is conversion: an object Illuminate\Database\Eloquent\Builder cannot be converted to string, in fact, its concatenation is invalid.

How to solve:

In parts, working with the ease of chaining of methods that the Illuminate\Database\Eloquent\Builder has implemented.

Observing: utilize $request->input if there is no return its default value is NULL and in the if utilize is_null to check whether it has information or is void.

Your code would basically look like this:

$inicial = DateTime::createFromFormat('d/m/Y', $request->get('dataInic'))->format('Y-m-d');
$final = DateTime::createFromFormat('d/m/Y', $request->get('dataFim'))->format('Y-m-d');
$flgStatus =  $request->input('FlgStEncomenda');
$pessoa = $request->input('CdPessoa');
$clientes = Pessoa::lists('NmPessoa', 'id')->all();

$pedidos = Encomenda::where('created_at', '<=', $inicial)
                    ->where('created_at', '>=', $final);


if (!is_null($flgStatus))
{
    $pedidos = $pedidos->where('FlgStEncomenda',$flgStatus);
}    

if (!is_null($pessoa))
{
    $pedidos = $pedidos->where('CdPessoa',$pessoa);
}

$resultado = $pedidos->get();

Note that in the code the variable $pedido received several times $pedidos->where (can be any command of the builder) if in the if were satisfied, this ensures that I always return the last change of builder and in the end is called a get() with the filters that were chosen.

References:

  • my date in the bank has day, month, year, hour, second, minute and in my research it has only day and year, it has importance this ?

  • All research cases are coming back empty, even if there are some results

  • I think you’re in trouble at the moment

  • 1

    found the error, I was confusing the date items, the right is >= after <=. It worked perfectly now.

  • 1

    @Renanrodrigues now that I saw your comments. kkk

Browser other questions tagged

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