【问题标题】:Laravel 5 Eloquent Model Object in AngularJS ControllerAngularJS 控制器中的 Laravel 5 Eloquent 模型对象
【发布时间】:2015-05-10 22:05:37
【问题描述】:

我对 AngularJS 还很陌生,我正在使用 Laravel 5 和 AngularJS 开展一个新项目。

所以基本上,我正在尝试根据一些过滤器更新排名。

这是我如何调用我的 api:

$http.post('/api/standings', {
        category: $scope.current_category.id,
        division: $scope.current_division.id,
        gender: $scope.current_gender.slice(0,1),
        season: $scope.current_season.id
    }).
    success(function(data, status, headers, config) {
        var standings_array = [];
        var stats_array = [];

        angular.forEach(data, function(value,key){
            standings_array[key] = value[0];
            stats_array[key] = value[1];
        });

        $scope.standings = standings_array;
        $scope.team_stats = stats_array;
    });

这就是我返回数组的方式:

 $teams_array[] = array($team,$stats);
 return $teams_array

我的看法:

  <tr ng-repeat='team in standings'>
        <td><% $index+1 %></td>
        <td><% team.name %></td>
        <td><% team_stats[$index].games_played %></td>
        <td><% team_stats[$index].points %></td>
        <td><% team_stats[$index].win_pct %></td>
  </tr>

一切都很好,除了 win_pct,因为它是一个模型访问器:

public function getWinPctAttribute()
{
    $pct = Utils::calculatePercentage($this->points,$this->games_played*3);
    $pct = number_format($pct, 2);

    return $pct;
}

但是 win_pct 什么也没返回...有没有办法让我访问那个值?

谢谢, 阿拉

【问题讨论】:

    标签: php angularjs eloquent laravel-5


    【解决方案1】:

    您需要在模型中添加protected $appends = ['win_pct'];。检查 Laravel 5 文档 http://laravel.com/docs/5.0/eloquent 最后一段的 Eloquent 部分。

    【讨论】:

      猜你喜欢
      • 2012-11-28
      • 2015-03-12
      • 2017-09-13
      • 2017-09-02
      • 1970-01-01
      • 1970-01-01
      • 2016-08-17
      • 2019-07-16
      • 2015-12-09
      相关资源
      最近更新 更多