I want to fetch only one columns which has name image_url from relationship table. relationship on table is belongsTO. i am confused how to fetch only single column value with belongsTo relation.
below is my model.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class PropertyImageGallery extends Model
{
use HasFactory;
protected $fillable = ['property_id', 'name', 'size','image_url'];
public function property()
{
return $this->belongsTo(Property::class);
}
}
please help me.
CodePudding user response:
BelongsTo relationship like below:
public function parent()
{
return $this->belongsTo(Parent::class,'foreign_key','owner_key');
}
it would be:
public function property(){
return $this->belongsTo(Property::class,'property_id','id');
}
CodePudding user response:
you should call it like this in your controller :
$yourParentElement = ParentModel::find(1);
return $yourParentElement ->Property->image_url ;
check this for more infos: Eloquent: Relationships (Belongs To)