public function edit($ID){ $game = Game::find($ID); // build List of team names and IDs $allTeams = Team::all(); $team = []; foreach ($allTeams as $t) $team[$t->ID] = $t->name(); // build a List of competitions $allCompetitions = Competition::all(); $competition = []; foreach ($allCompetitions as $c) $competition[$c->ID] = $c->fullname(); return VIEw::make('games.edit',compact('game','team','competition'));}
我正在发送数据,以便在选择列表中显示.我知道Eloquent ORM方法列表,但问题是我知道它只能将属性名称作为参数,而不是方法(如name()和fullname()).
我怎样才能优化这一点,我还能使用Eloquent吗?
解决方法 我会调查attributes
and appends
.你可以通过调整模型来做你想做的事. 竞争
<?PHP namespace App;use Illuminate\Database\Eloquent\Collection;use Illuminate\Database\Eloquent\Model;class Competition extends Model{ protected $appends = ['fullname']; ... public function getFullnameattribute() { return $this->name.' '.$this->venue; }}
球队
<?PHP namespace App;use Illuminate\Database\Eloquent\Collection;use Illuminate\Database\Eloquent\Model;class Team extends Model{ protected $appends = ['name']; ... public function getNameattribute() { return $this->city.' '.$this->teamname; }}
调节器
public function edit($ID){ $game = Game::find($ID); $team = Team::get()->Lists('ID','name'); $competition = Competition::get()->Lists('ID','fullname'); return VIEw::make('games.edit','competition'));}总结
以上是内存溢出为你收集整理的php – 如何压缩此代码以便从Laravel Controller向视图发送信息?全部内容,希望文章能够帮你解决php – 如何压缩此代码以便从Laravel Controller向视图发送信息?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)