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');
}
}
even if I take everything.. and leave only Products::find($id)->first() it error
– MichaelCosta
Because
find
is the end of command, no need to putfirst()
find already does it for you @Michaelcosta– novic
however if I use Where('id', '=', $id ai yes.. it returns null should not return null either using find($id) ?
– MichaelCosta
minutes I understand.. I will test here
– MichaelCosta
If you type
Produtos::find($id);
this is how it should be done, either returns the class object or returnsnull.
@Michaelcosta– novic
@Michaelcosta edited putting another way, I think cool this too.
– novic
Top bro! That’s right. Thanks!!!
– MichaelCosta
Cool using load.. did not know this way. I will use so now. vlw mano! thanks!
– MichaelCosta