【问题标题】:Laravel 5 Undefined variable Controller 2 ViewLaravel 5未定义变量控制器2视图
【发布时间】:2017-05-15 13:36:26
【问题描述】:

我对 laravel 中的未定义变量错误感到疯狂,希望有人能指出我做错了什么,已经尝试了几种方法将变量从我的控制器传递到我的视图,但似乎没有任何效果。 提前致谢

控制器:

   public function busquedaGet()
{
   return view('frontend.user.busquedaBoxeadores',compact('posts'));

}




public function busquedaPost(Request $request, BusquedaRepository $userRepo)

{
     $validator = Validator::make($request->all(), [
        'gender' => 'required|not_in:null',
        'weight' => 'required|not_in:null',
        'country' => 'required|not_in:null'
    ]);


    if ($validator->fails()) {
        return redirect(route('busquedaPostA'))->withErrors($validator);
    }

     $posts=$userRepo->busquedaBoxers($request->all());
      //dd($posts);
    return view('frontend.user.busquedaBoxeadores',compact('posts'));

}

查看:

@extends('frontend.layouts.app')

@section('title')
    {{{ trans('app.contrataBoxeadores') }}}
@endsection

  <div class="container">
    <div class="row">
      <div class="col-md-3 visible-lg">
        <div class="side-bar">
        </div>
      </div>

      <div class="col-md-6">
       <div class="updates">
        <div class="panel panel-default panel-update mg-top20">
          <div class="panel-heading">
           <h3 align="center">{{{ trans('app.red') }}} </h3>
          </div>
           <div class="panel-body"> 
                     {!! Form::open(['url' => route('busquedaPostA')]) !!}
                                <div class="form-group {{ $errors->has('gender') ? ' has-error' : '' }}">
                                    {!! Form::label('gender', trans('app.gender')); !!}
                                    {!! Form::select('gender', ['null' => trans(''),'F' => trans('app.female'), 'M' => trans('app.male')], null, ['class' => 'form-control']) !!}
                                    @if ($errors->has('gender'))
                                        <span class="help-block">
                                            <strong>{{ $errors->first('gender') }}</strong>
                                        </span>
                                    @endif
                                </div>
                                 <div class="form-group {{ $errors->has('weight') ? ' has-error' : '' }}">
                                   {!! Form::label('weight', trans('app.weight')); !!}
                                   {!! Form::select('weight', ['null' => trans(''),
                                   ' 1' => trans('Mini flyweight'),
                                   ' 2' => trans('Fyweight'),
                                   ' 3' => trans('Super flyweight'),
                                   ' 4' => trans('Bantamweight'),
                                   ' 5' => trans('Super bantamweight'),
                                   ' 6' => trans('Featherweight'),
                                   ' 7' => trans('Super featherweight'),
                                   ' 8' => trans('Lightweight'),
                                   ' 9' => trans('Super lightweight'),
                                   '10' => trans('Welterweight'),
                                   '11' => trans('Super welterweight'),
                                   '12' => trans('Middleweight'),
                                   '13' => trans('Super middleweight'),
                                   '14' => trans('Light heavyweight'),
                                   '15' => trans('Cruiserweight'),
                                   '16' => trans('Heavyweight')],
                                    null, ['class' => 'form-control']) !!}
                                    @if ($errors->has('country'))
                                        <span class="help-block">
                                            <strong>{{ $errors->first('weight') }}</strong>
                                        </span>
                                    @endif
                                 </div>
                                <div class="form-group {{ $errors->has('country') ? ' has-error' : '' }}">
                                    {!! Form::label('country', trans('country.countries')); !!}
                                    {!! Form::select('country', ['null' => trans(''),
                                    'us' => trans('country.united'), 'bz' => trans('country.brazil'), 'mx' => trans('country.mexico'), 'cl' => trans('country.colombia'),'ar' => trans('country.argentina'), 'ca' => trans('country.canada'), 'pu' => trans('country.peru'), 'vz' => trans('country.venezuela'), 'cl' => trans('country.chile'), 'ec' => trans('country.ecuador'), 'gt' => trans('country.guatemala'), 'cb' => trans('country.cuba'), 'hi' => trans('country.haiti'),  'bl' => trans('country.bolivia'), 'dr' => trans('country.dominican'), 'hnd' => trans('country.honduras'), 'pa' => trans('country.paraguay'), 'nic' => trans('country.nicaragua'),'slv' => trans('country.salvador'), 'cr' => trans('country.costa'), 'pnm' => trans('country.panama'), 'pr' => trans('country.puerto'), 'urg' => trans('country.uruguay'),'jam' => trans('country.jamaica'), 'tt' => trans('country.trinidad'), 'blz' => trans('country.belize'), 'brb' => trans('country.barbados'), 'ot' => trans('country.other')],
                                    null, ['class' => 'form-control']) !!}
                                    @if ($errors->has('country'))
                                        <span class="help-block">
                                            <strong>{{ $errors->first('country') }}</strong>
                                        </span>
                                    @endif
                                </div>
                                <div class="form-group">
                                 {!! Form::submit(trans('app.buscar'), ['class' => 'btn btn-lg btn-primary btn-block signup-btn']); !!} 
                                </div>
            {!! Form::close() !!}
            </div>           
         </div>
        </div>
      </div>

     <div class="col-md-3 visible-lg"> 
     <p> Hello, {{ $posts }} </p>
     </div>
   </div>
 </div> 

路线

    Route::post('/profile/busquedaBoxeadores',['as' => 'busquedaPostA', 'uses' => 'UserController@busquedaPost']);
    Route::get('/profile/busquedaBoxeadores', ['as' => 'busquedaGetA', 'uses' => 'UserController@busquedaGet']);

存储库

    public function busquedaBoxers(array $data, $source = 'site')
{
 $query = DB::table('users')
        ->join('user_profiles', 'users.id', '=', 'user_profiles.user_id')
        ->join('boxings', 'users.id', '=', 'boxings.user_id')
        ->join('countries', 'users.id', '=', 'countries.user_id')           
        ->select('users.name', 'users.username', 'users.id')
        ->where('boxings.weight_id', '=', $data['weight'])
        ->where('user_profiles.gender', '=', $data['gender'])
        ->where('countries.country', '=', $data['country'])
        ->get();

        return $query;
}

【问题讨论】:

  • 添加错误描述
  • 确切的错误信息是什么?
  • 这是我得到的:未定义变量:帖子(视图:C:\Users\Bof3ss37\Desktop\socialwall_v1.0.3\resources\views\frontend\user\busquedaBoxeadores.blade.php)跨度>
  • busque‌​daBoxeadores.blade.p‌​hp 在此视图中出错。添加有关此文件的更多信息
  • busquedaGet 方法在您的控制器中没有 $posts 变量

标签: php laravel


【解决方案1】:

您对两种方法都使用相同的视图:busquedaGetbusquedaPost,但在 busquedaGet 中,您是 compact-ing 变量 posts 未定义。因此,如果您没有在 get-method 中使用它们,您可以添加空字符串:

public function busquedaGet()
{
    $posts = [];
    return view('frontend.user.busquedaBoxeadores',compact('posts'));
}

并修改视图。替换:

<p> Hello, {{ $posts }} </p>

与:

<p> Hello,
@foreach($posts as $post)
    {{ $post->username }}
@endforeach
</p>

【讨论】:

  • 谢谢 Alex 好的,我试过了,但现在当我执行搜索时,我得到了这个 htmlentities() 期望参数 1 是字符串,给定数组(查看:C:\Users\Bof3ss37\Desktop\socialwall_v1. 0.3\resources\views\frontend\user\busquedaBoxeadores.blade.php)
  • @CesarGarcia 那么你必须决定 $posts 应该是什么?如果它是一个数组 - 将其定义为数组并使用 foreach 语句显示它。在答案中添加示例
  • 谢谢哥们!!它正在运行!哦,在这个大声笑上花了几个小时应该先问一下哦..
【解决方案2】:

您试图在刀片中将帖子显示为字符串,但它没有在 busquedaGet 中定义,在 busquedaPost 中它是一个集合。

你好,{{ $posts }}

首先您需要在busquedaGet 中为它定义一个默认值,然后尝试遍历集合以显示其内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-02
    • 2014-03-04
    • 2017-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-18
    相关资源
    最近更新 更多