How to filter by method? Laravel 5.4

Asked

Viewed 151 times

1

I need to filter all images that have a specific link, I was able to filter through the status of the product table, but what connects the image to the product is the method image(); class Produto:

Method image() of the Product class:

public function image()
{
    return $this->hasOne('App\ProdutoImage', 'erp_productid', 'erp_productid');
}

I need to take the amount erp_image of ProdutoImage and pass to the filter I tried to do it that way, but it doesn’t filter:

if(request()->has('erp_image'))
{
    $produtos = Produtos::where('erp_image', request('erp_image'))
              ->paginate(25)
              ->appends('erp_image', request('erp_image'));
} 
else 
{
    $produtos = Produtos::paginate(25);
}

In my View added the Link:

<a href="/products/?erp_image=http://teste.s3-website-sa-east-1.amazonaws.com/produtos/teste.jpg">
    <span class="label label-primary">Sem Imagem</span>
</a>

Any suggestions?

1 answer

0

I didn’t really understand it, but that’s what it would be?

Produtos::whereHas('image', function ($query){
    $query->where('erp_image', request('erp_image'));
})->paginate();

Returns the products that have an image that satisfies the filter.

Browser other questions tagged

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