I am developing an e-commerce project in which I have the following tables,
1. Products table :
- ID (int)
- Title (String)
- fabric (unsignedInteger)
- created_at (timestamp)
- I have also used this code below in migration.
$table->foreign('fabric')->references('id')->on('fabrics');
2. Fabric Table
ID (int)
Title (string)
My models are:
class Product extends Model{
public function fabric(){
return $this->hasOne('App\Fabric','id', 'fabric');
}
}
class Fabric extends Model{
public function products(){
return $this->belongsTo('App\Product', 'fabric', 'id');
}
}
I want to get the product fabric in view using this
{{ $product->fabric()->title }}
However, it returns
Object of class Illuminate\Database\Eloquent\Relations\HasOne could not be converted to string