Reputation: 61
Im Going crazy with the undefined variable error in laravel , Hope someone could point out what im doing wrong, already tried several ways to pass variable from my controller to my view but nothings seems to work. Thanks in advance
Controller:
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'));
}
view:
@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>
routes
Route::post('/profile/busquedaBoxeadores',['as' => 'busquedaPostA', 'uses' => 'UserController@busquedaPost']);
Route::get('/profile/busquedaBoxeadores', ['as' => 'busquedaGetA', 'uses' => 'UserController@busquedaGet']);
repository
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;
}
Upvotes: 0
Views: 488
Reputation: 11906
You're trying to display the posts as string in the blade, but it's not defined in busquedaGet
and in busquedaPost
it's a collection.
Hello, {{ $posts }}
First you need to define a default value for it in busquedaGet
and then try to loop through the collection to display it's content.
Upvotes: 0
Reputation: 2533
You are using same view for both methods: busquedaGet
and busquedaPost
but in busquedaGet
you're compact
-ing variable posts
that is not defined. So you can add empty string if you're not using them in get-method:
public function busquedaGet()
{
$posts = [];
return view('frontend.user.busquedaBoxeadores',compact('posts'));
}
And modify view. Replace:
<p> Hello, {{ $posts }} </p>
With:
<p> Hello,
@foreach($posts as $post)
{{ $post->username }}
@endforeach
</p>
Upvotes: 1