Reputation: 155
I have this in my controller:
public function detail($id) {
$data = DB::table('data_api')->where('id', $id)->get();
$carousel = DB::table('data_carousel')->where('data_api_id', $id)->get();
return view('detail', ['data_api' => $data]);
return view('detail', ['data_carousel' => $carousel]);
}
But when I try to echo-ing, $carousel
by {{ $carousel }}
, it says not found. But $data
work perfectly. Any idea?
Undefined variable: carousel (View: /mylaravelproject/resources/views/detail.blade.php)
Upvotes: 2
Views: 2454
Reputation: 4959
in my case i use
@if(isset($users))
before my foreach like this example:
<div class="form-group" id="boardAdminUserIdCon">
<p><span class="glyphicon glyphicon-briefcase" aria-hidden="true"></span> مدیر بورد</p>
<select name="boardAdminUserId" id="boardAdminUserId" class="form-control" required="required">
<option value="">{{ __('auth.CHOOSEYOURADMIN') }}...</option>
@if(isset($users))
@foreach($users as $user)
<option value="{{ $user['id'] }}">{{ $user["name"] }}
</option>
@endforeach
@endif
</select>
</div>
Upvotes: 0
Reputation: 49
is that really working now? You tell us you are getting
Undefined variable: carousel (View: /mylaravelproject/resources/views/detail.blade.php)
And you will get that because you are not passing the variable carousel to your view, you are naming your variables as data_api
and data_carousell
Second, you should pass your variables as an asociative array in only one sentence not two view calls like this
return view('detail', ['carousel' => $carousel,'data' => $data]);
Upvotes: 0
Reputation: 1952
You need to return view like below
public function detail($id) {
$data = DB::table('data_api')->where('id', $id)->get();
$carousel = DB::table('data_carousel')->where('data_api_id', $id)->get();
return view('detail', compact('data','carousel'));
}
Upvotes: 0
Reputation: 6544
You cannot return two times from a function and expect both to actually return something. After the first return
, execution of the function is stopped.
Try returning both variables at once instead:
return view('detail', [
'data_api' => $api,
'data_carousel' => $carousel
]);
Upvotes: 1
Reputation: 3450
Replace your code with following:
public function detail($id) {
$data = DB::table('data_api')->where('id', $id)->get();
$carousel = DB::table('data_carousel')->where('data_api_id', $id)->get();
return view('detail')->with('data_api', $data)->with('data_carousel', $carousel);
}
Upvotes: 0
Reputation: 1094
Update:
public function detail($id) {
$data = DB::table('data_api')->where('id', $id)->get();
$carousel = DB::table('data_carousel')->where('data_api_id', $id)->get();
return view('detail', ['data_carousel' => $carousel,'data_api' => $data]);
}
You are returning two views from the same controller. After the first return
execution of code is halt and it will not return
the second view. That's why you are unable to get the second view parameters
Upvotes: 1
Reputation: 1986
you need to change the double return statement to a single return
return view('detail', ['data_api' => $data]);
return view('detail', ['data_carousel' => $carousel]);
to
return view('detail', ['data_api' => $data, 'data_carousel' => $carousel]);
Upvotes: 4
Reputation: 2984
you returning view two times that's why only $data_api
is available in view,
try this
public function detail($id) {
$data = DB::table('data_api')->where('id', $id)->get();
$carousel = DB::table('data_carousel')->where('data_api_id', $id)->get();
return view('detail', ['data_api' => $data, 'data_carousel' => $carousel]);
}
Upvotes: 1