Laravel find() function giving error

Asked

Viewed 669 times

2

When I launch a search with an id that exists in the bank, it works normally.

Laravel 5.6

    public function findProduto($id)
{
    $produto = Produto::find($id)->with('imagemgame')->first();
    return $dados_check = $produto->count()>0 ? $produto : false ;
}

but when I launch a search with an id that does not exist in the database, instead of returning "false", it returns an error, there seems to be something wrong with find or something like that.

Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR)
Call to a member function find() on null

Model-> Products

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\ImageUploads;

class Produto extends Model
{
use SoftDeletes;

public $timestamps = true;

protected $table = 'produtos';

protected $fillable = [
    'titulo', 'status', 'console', 'edicao', 'genero', 'classificacao', 'custo', 'venda', 'promocao',
    'indatepromo', 'outdatepromo', 'quantidade', 'descricao', 'produtora', 'lancamento',
    'idioma', 'legenda', 'onplayers', 'offplayers', 'obs1', 'obs2', 'obs3', 'video',
];

protected $hidden = ['custo'];

protected $dates = ['deleted_at'];

/////////////////////////////////////////////////////////////////
/// RELACIONAMENTOS /////////////////////////////////////////////
public function imagemgame(){
    return $this->hasMany(ImageUpload::class, 'produtos_id', 'id')->orderBy('capa', '1');
}

}

1 answer

4


Analyzing your code has some problems, example: when using the find which is the return of information by primary key of your table if the data is not found is returned null and right after you make a with that does not exist, that is the reason for the problem and the exception launched, then within the method at the time of return you put count, there is no such method because it is not a list is a given, so also have to exchange the criticism and solved propror a minimal example:

public function findProduto($id)
{
    $produto = Produto::with('imagemgame')->where('id', $id)->first();
    return is_null($produto) ? false: $produto;
}

Observing: I made a where and I named the primary key, you should also put the same name, I ended up leaving id which is a pattern, but of course the name can be another.


It can also be done like this:

public function findProduto($id)
{
    $produto = Produto::find($id);
    if ($produto)
    {
        $produto->load('imagemgame'); //carregamento a relação
        return $produto;
    }
    return false;
}

Reference: Eloquent: Relationships

  • even if I take everything.. and leave only Products::find($id)->first() it error

  • Because find is the end of command, no need to put first() find already does it for you @Michaelcosta

  • however if I use Where('id', '=', $id ai yes.. it returns null should not return null either using find($id) ?

  • 1

    minutes I understand.. I will test here

  • If you type Produtos::find($id); this is how it should be done, either returns the class object or returns null. @Michaelcosta

  • @Michaelcosta edited putting another way, I think cool this too.

  • 1

    Top bro! That’s right. Thanks!!!

  • 1

    Cool using load.. did not know this way. I will use so now. vlw mano! thanks!

Show 3 more comments

Browser other questions tagged

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