[英]Undefined Variable in Blade Laravel
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.但是当我尝试 echo-ing,
$carousel
by {{ $carousel }}
,它说没有找到。 But $data
work perfectly.但是
$data
工作得很好。 Any idea?任何的想法?
Undefined variable: carousel (View: /mylaravelproject/resources/views/detail.blade.php)
未定义变量:carousel(查看:/mylaravelproject/resources/views/detail.blade.php)
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]);
you returning view two times that's why only $data_api
is available in view,您返回视图两次,这就是为什么视图中只有
$data_api
可用,
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]);
}
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.第一次
return
后,代码停止执行,不会return
第二个视图。 That's why you are unable to get the second view parameters这就是为什么您无法获得第二个视图参数的原因
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.在第一次
return
,函数的执行就会停止。
Try returning both variables at once instead:尝试同时返回两个变量:
return view('detail', [
'data_api' => $api,
'data_carousel' => $carousel
]);
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);
}
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'));
}
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
你会得到它,因为你没有将变量 carousel 传递给你的视图,你将变量命名为
data_api
和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]);
in my case i use在我的情况下,我使用
@if(isset($users))
before my foreach like this example:在我的 foreach 像这个例子之前:
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.