php - Pull out the specific data when the relation ship is many to many in laravel -


i want ask how pull out specific data (some columns) laravel database

here code :

user model :

public function events(){     return $this->belongstomany('app\events')->withtimestamps(); } 

event controller :

public function showjson(){     $user_id=auth::user()->id;     $events = db::table('events')             ->select(                 'id',                 'calendar_title title',                 'startdate start',                 'enddate end',                 'calendar_color backgroundcolor',                 'calendar_color bordercolor')             ->where('user_id',$user_id)             ->get();     $user=auth::user();     $eventdata=$user->events;       return $eventdata; } 

i have relationship between two, can data through :

$user = auth::user(); $eventdata = $user->events; 

but want specific columns name, in select code above.

would way can call specific data , change column name?

i want display in json calendar_title, calendar_des only.

you can use lists retrieve list of column values, take @ database query builder retrieving results section :

$user->events->lists('calendar_title','calendar_des'); 

if want return json format can use tojson() method :

$user->events->lists('calendar_title','calendar_des')->tojson(); 

hope helps.


Comments