jeudi 29 août 2019

Laravel hasOne vs belongsTo returns null

I'm quite new to Laravel and I'm trying to return values of two tables that have relationship, but I'm receiving null.

Here in my Controller I'm passing with('image').

$products = Produtos::select('codigo', 'descricao', 'CT', 'Fotos')->with('image')->take(2)->get();
return response()->json($products);

Image Model

class Objetos extends Model
{
    protected $table = 'Imagens';

    public function produtos(){
        return $this->belongsTo(\App\Produtos::class);
    }
}

Product Model

class Produtos extends Model
{
    public function image(){
        return $this->hasOne(\App\Objetos::class, 'Chave');
    }
}

The columns that have relationship are:

Objetos.Chave = Produtos.id

I can't see my error, but I think it's probably in the arguments I'm passing in hasOne and belongsTo.

Note: I'm accessing an external database (MS SQL), but I think it is not the problem.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire