I am writing an assignment system for radio newscasts. The data tables:
station
id | calls
---| -----
1 | cxri
newscast
id | name_input | for_station_id
---|------------|---------------
1 | am | 1
assignment
id | newscast_id | user_id
1 | 1 | 5
The controller for Assignment.edit is as follows:
if (! Gate::allows('assignment_edit'))
return abort(401);
}
$relations = [
'anchors' => \App\User::get()->pluck('name', 'id')->prepend('Please select', ''),
'casts' => \App\Newscast::get()->pluck('name_input', 'id')->prepend('Please select', ''),
];
return view('assignment.create', $relations);
The Newscasts model:
public function for_station()
{
return $this->belongsTo(Station::class, 'for_station_id')->withTrashed();
}
Right now I get
"casts" => Collection {#451 ▼
#items: array:2 [▼
"" => "Please select"
1 => "am"
]
}
I want
"casts" => Collection {#451 ▼
#items: array:2 [▼
"" => "Please select"
1 => "cxri-am"
]
}
How do I make that happen?
Or should I denormalize the data and make name_input 'cxri-am' instead of 'am'?
What I've tried:
Accepted answer in Laravel pluck fields from relations
'casts' => \App\Newscast::with('station')->get()->pluck('name_input', 'id'),
errors with
Call to undefined relationship [station] on model [App\Newscast]
I included the Newscast model above based on the discussion in Laravel: get/show data from related tables. Based on that discussion it appears to me the newscast should already be able to access the Station calls. But as shown above, the calls are not in $casts.