farshadf
4 days ago
126
16
Laravel

How to get properies of a relationship as comma separated values in laravel?

Posted 4 days ago by farshadf

I have a ManyToMany relationship for example Facility and Home so here is the example of the relationship:


public function facility()
{
    return $this->belongsToMany(facility::class)->withPivot('is_free');
}
and in facility I have:

public function home()
{
   return $this->belongsToMany(Home::class)->withPivot('is_free');
}

and in some controller I do like below :


$data = Home::with('facility')->get;

it gives me the result like below :



{
    name: example,
    id: 1,
    facility: {
        id: 1,
        name:item1,
    },
    {
        id: 1,
        name:item1,
    },
    {
        id: 1,
        name:something,
    }
}

now i want it comma separated like below :


{   
    name:example,
    id : 1,
    some other fileds ,
    ,
    , 
         facility: {item1,item2,item3,item4,. . .
    }
}

so I want them comma separated and only pick some certain columns from it

Please sign in or create an account to participate in this conversation.