首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel 8项目中未定义的变量(php 7.4)

Laravel 8项目中未定义的变量(php 7.4)
EN

Stack Overflow用户
提问于 2022-02-20 15:17:18
回答 2查看 199关注 0票数 0

大多数函数我的项目工作,但当涉及到注册结果,我得到一个错误。

这是一个广泛的手枪竞赛系统,管理员可以在其中创建不同类型的竞赛。射击运动员可以注册并报名参加比赛。当比赛结束时,它将被输入到系统中。这时问题就出现了。

该系统从Laravel 5.3升级到8,在5.3中运行良好。下面是一个功能良好的测试系统:https://test.webshooter.se

未定义变量:巡逻错误:

代码语言:javascript
复制
    [2022-02-20 14:14:12] local.ERROR: Undefined variable: patrols {"userId":1,"exception":"[object] (ErrorException(code: 0): Undefined variable: patrols at /Users/ralph/laravel9/webshooter_web/app/Repositories/ResultsRepository.php:156)

可能的patrol_type: null,决赛,区分。在本例中,它是空类型。

这是发生错误的PatrolRepository.php的一部分:

代码语言:javascript
复制
        public function getPatrols($competitionsId)
    {
        if (!$this->request->has('patrol_type')) :
            $patrols = Patrol::where('competitions_id', $competitionsId)->orderBy('sortorder')->get();
        elseif ($this->request->get('patrol_type') == 'finals') :
            $patrols = PatrolFinals::where('competitions_id', $competitionsId)->orderBy('sortorder')->get();
        elseif ($this->request->get('patrol_type') == 'distinguish') :
            $patrols = PatrolDistinguish::where('competitions_id', $competitionsId)->orderBy('sortorder')->get();
        endif;

        return $patrols;
    }

使用此代码,它运行得非常好:

代码语言:javascript
复制
public function getPatrols($competitionsId)
{
    if ($this->request->get('patrol_type') == 'finals'):
        return PatrolFinals::where('competitions_id', $competitionsId)->orderBy('sortorder')->get();
    endif;
    
    if ($this->request->get('patrol_type') == 'distinguish'):
        return PatrolDistinguish::where('competitions_id', $competitionsId)->orderBy('sortorder')->get();
    endif;

    return Patrol::where('competitions_id', $competitionsId)->orderBy('sortorder')->get();
}

它不检查null,而是检查其他状态,如果没有,则为null。

在本例中,patrol_type为空。函数应该从数据库获取信息:表巡逻。然后在数据库表:结果中创建posts。这些帖子应该显示一个表单,在那里可以输入结果。但什么都没显示出来。这是“filter_default.blade.php”,它将显示空巡逻的表单:

代码语言:javascript
复制
    <div class="row">
    <div class="col-sm-3">
        <div class="panel panel-default">
            <div class="panel-heading">{{_('Inmatning')}}</div>
            <table class="table table-bordered">
                <tbody>
                <tr>
                    <td>{{_('Datum')}}</td>
                    <td><% competitions.competition.date %></td>
                </tr>
                <tr>
                    <td>{{_('Tävlingsgrupp')}}</td>
                    <td><% (competitions.competition.championship.name) ? competitions.competition.championship.name : '-' %></td>
                </tr>
                <tr ng-if="competitions.competitions.competitiontype">
                    <td>{{_('Tävlingstyp')}}</td>
                    <td><% competitions.competitions.competitiontype.name %></td>
                </tr>
                </tbody>
            </table>
            <div class="panel-body">
                <div class="row form-group">
                    <div class="col-lg-12">
                        <label><% competitions.competition.translations.patrols_name_singular | ucfirst %></label>
                        <select class="form-control" ng-options="patrol.sortorder as (patrol.sortorder+ ' ('+patrol.start_time_human+')') for patrol in results.patrols" ng-model="results.filter.patrol">
                            <option value=""><% competitions.competition.translations.patrols_name_singular | ucfirst %></option>
                        </select>
                    </div>
                </div>
                <div class="row form-group">
                    <div class="col-lg-6">
                        <label><% competitions.competition.translations.stations_name_singular | ucfirst %> ({{_('från')}})</label>
                        <select class="form-control" ng-options="n for n in [] | range:1:competitions.competition.stations_count+1" ng-model="results.filter.station_start">
                            <option value=""><% competitions.competition.translations.stations_name_singular | ucfirst %></option>
                        </select>
                    </div>
                    <div class="col-lg-6">
                        <label><% competitions.competition.translations.stations_name_singular | ucfirst %> ({{_('till')}})</label>
                        <select class="form-control" ng-options="n for n in [] | range:1:competitions.competition.stations_count+1" ng-model="results.filter.station_end">
                            <option value=""><% competitions.competition.translations.stations_name_singular | ucfirst %></option>
                        </select>
                    </div>
                </div>
                <div class="row form-group">
                    <div class="col-lg-6">
                        <label><% competitions.competition.translations.patrols_lane_singular | ucfirst %> ({{_('från')}})</label>
                        <select class="form-control" ng-options="n for n in [] | range:1:competitions.competition.patrol_size+1" ng-model="results.filter.lane_start">
                            <option value=""><% competitions.competition.translations.patrols_lane_singular | ucfirst %></option>
                        </select>
                    </div>
                    <div class="col-lg-6">
                        <label><% competitions.competition.translations.patrols_lane_singular | ucfirst %> ({{_('till')}})</label>
                        <select class="form-control" ng-options="n for n in [] | range:1:competitions.competition.patrol_size+1" ng-model="results.filter.lane_end">
                            <option value=""><% competitions.competition.translations.patrols_lane_singular | ucfirst %></option>
                        </select>
                    </div>
                </div>
                <div class="row form-group">
                    <div class="col-lg-12">
                        <div class="checkbox">
                            <label>
                                <input type="checkbox" ng-model="results.filter.per_shot" ng-true-value="1" ng-false-value="0"> {{_('Registrera alla skott')}}
                            </label>
                        </div>
                    </div>
                </div>
                <div class="row form-group">
                    <div class="col-lg-12">
                        <div class="checkbox">
                            <label>
                                <input type="checkbox" ng-model="results.filter.show_empty_lanes" ng-true-value="1" ng-false-value="0"> {{_('Visa tomma %s')}}
                            </label>
                        </div>
                    </div>
                </div>
                <a class="btn btn-primary" ui-sref-opts="{reload: true}" ui-sref="competitions.admin.results.registration({competitions_id: competitions.competition.id, patrol:results.filter.patrol, patrol_type:results.filter.patrol_type, station_start: results.filter.station_start, station_end: results.filter.station_end, lane_start:results.filter.lane_start, lane_end:results.filter.lane_end, show_empty_lanes:results.filter.show_empty_lanes, per_shot:results.filter.per_shot})">{{_('Visa')}}</a>
            </div>
        </div>
    </div>

    <div class="col-sm-9">
        <div ng-if="results.signups">
            <div ui-view="military" ng-if="competitions.competition.results_type == 'military'"></div>
            <div ui-view="field" ng-if="competitions.competition.results_type == 'field' || competitions.competition.results_type == 'pointfield' || competitions.competition.results_type == 'magnum'"></div>
            <div ui-view="precision" ng-if="competitions.competition.results_type == 'precision'"></div>
        </div>
    </div>
</div>

下面是模型/Result.php:

代码语言:javascript
复制
    <?php
    namespace App\Models;
    use Illuminate\Database\Eloquent\Model;
    use Illuminate\Database\Eloquent\SoftDeletes;
    class Result extends Model
{
    use SoftDeletes;
    protected $table = 'results';
    protected $fillable = [
        'stations_id',
        'figure_hits',
        'hits',
        'points',
        'station_figure_hits',
    ];

    protected $casts = [
        'station_figure_hits' => 'array',
    ];

    public function Signup()
    {
        return $this->belongsTo(\App\Models\Signup::class, 'signups_id', 'id');
    }
}

调试器的输出:

代码语言:javascript
复制
 "patrol" => "1"
  "patrol_type" => null
  "station_start" => "1"
  "station_end" => "2"
  "lane_start" => "1"
  "lane_end" => "20"
  "show_empty_lanes" => "1"
  "per_shot" => "1"

会很感激你的一些想法。你好,瑞典的拉尔夫

EN

回答 2

Stack Overflow用户

发布于 2022-04-08 21:00:25

似乎任何其他条件都匹配并返回$patrols;为了避免错误,您可以这样返回:return $patrols ?? null;或在函数开始时启动$$patrols。相反,null需要检查所期望的内容。

票数 0
EN

Stack Overflow用户

发布于 2022-04-10 10:12:39

问题解决了!诀窍是重新排列语句,这样它就不会首先检查patrol_type,因为它可能是null,但是检查另外两个语句。如果它不是其中之一,则假设为null,然后它工作得很完美,如下所示:

代码语言:javascript
复制
public function getPatrols($competitionsId)
{
    if ($this->request->get('patrol_type') == 'finals'):
        return PatrolFinals::where('competitions_id', $competitionsId)->orderBy('sortorder')->get();
    endif;
    
    if ($this->request->get('patrol_type') == 'distinguish'):
        return PatrolDistinguish::where('competitions_id', $competitionsId)->orderBy('sortorder')->get();
    endif;

    return Patrol::where('competitions_id', $competitionsId)->orderBy('sortorder')->get();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71195785

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档