I have a doubt in a controller with a relation of many (Product) to one (Brand), I try to search in Product to show the relationship 'Brand' but apparently I have an error because when using where()
to search In Product and then show the relation the controller does it to me the other way around, he uses the where()
in the related table giving me the error that the field 'link_id' does not exist in the table' brand 'but what I want is to find link_id in the product table.
$ejemplo = Producto::where('enlace_id', $id)->with('marca')->get();
Product Model
namespace App;
use Illuminate\Database\Eloquent\Model;
class Producto extends Model
{
protected $fillable = [
'enlace_id'
];
public function marca()
{
return $this->belongsTo('App\Marca', 'id', 'enlace_id');
}
}
Brand Model
namespace App;
use Illuminate\Database\Eloquent\Model;
class Marca extends Model
{
protected $fillable = ['nombre'];
public function producto()
{
return $this->hasMany('App\producto', 'enlace_id', 'id');
}
}