如果有人可以帮助我,我将无法从控制器获取变量。 我正在使用PHP Laravel 5.7.2。
DetectController.php:
public function store(Request $request){
if($request->input('Adult_Person') == "on") $checkboxPerson = 1;
else $checkboxPerson = 0;
if($request->input('Child') == "on") $checkboxChild = 1;
else $checkboxChild = 0;
$detect = \App\Detect::where('user_id', (int)auth()->user()->id);
if( $detect == NULL ) true;
else{
$detect = $detect->where('user_id', (int)auth()->user()->id);
$detect->delete();
}
$detect = new \App\Detect;
$detect->user_id = (int)auth()->user()->id;
$detect->checkbox_Adult_Person = $checkboxPerson;
$detect->checkbox_Child = $checkboxChild;
$detect->save();
$detect_user[] = $checkboxPerson;
$detect_user[] = $checkboxChild;
return view('home',['data' => $detect_user ]);
//return view('home', compact($detect_user)); //Dont work
//return view('home')->with('data',$detect_user); //Dont work
}
home.blade.php:
<form method="post" action="{{ route('detect.store') }}">
{!! csrf_field() !!}
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="checkbox_Adult_Person" name="Adult_Person" {{ $detect_user[0] ? "checked" : "" }} >
<label class="custom-control-label" for="checkbox_Adult_Person">Adult Person</label>
</div>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="checkbox_Child" name="Child"
{{ $detect_user[1] ? "checked" : "" }}>
<label class="custom-control-label" for="checkbox_Child">Child</label>
</div></form>
web.php
Route::resource('detect', 'DetectController');
错误:
ErrorException (E_ERROR) Undefined variable: detect_user (View: /var/www/html/laravel/test/resources/views/home.blade.php)
数据库已更新,但该变量未进入视图。 有人可以帮忙吗,谢谢!
EDIT :我尝试为此return view('home',['detect_user' => $detect_user ]);
进行更改,但两者都不起作用。谢谢
解决方案:return view('home',['detect_user' => $detect_user ]);
非常有用。
但是我的问题是Undefined variable: detect_user
,因为我的类Detect没有视图,很显然,当有人调用视图时,首先运行index函数的代码,因此在主视图代码中,我只创建了Detect Object并放入数据库中的数据,因此用户可以观看和编辑以前保存的数据。
代码,也许可以帮助某人:
public function index()
{
$detects = \App\Detect::all();
for( $i = 0; $i < count($detects); $i++ )
if( (int)$detects[$i]->user_id == (int)auth()->user()->id ) {
$detect = new \App\Detect;
$detect->user_id = (int)auth()->user()->id;
$detect->checkbox_Adult_Person = $detects[$i]->checkbox_Adult_Person;
$detect->checkbox_Child = $detects[$i]->checkbox_Child;
$detect->checkbox_Gun = $detects[$i]->checkbox_Gun;
$detect->checkbox_knife = $detects[$i]->checkbox_knife;
$detect->checkbox_Fire = $detects[$i]->checkbox_Fire;
$detect->checkbox_Rain = $detects[$i]->checkbox_Rain;
$detect->checkbox_Dog = $detects[$i]->checkbox_Dog;
$detect->checkbox_Cat = $detects[$i]->checkbox_Cat;
$detect->checkbox_Bird = $detects[$i]->checkbox_Bird;
$detect->checkbox_Violence = $detects[$i]->checkbox_Violence;
$detect->checkbox_Blood = $detects[$i]->checkbox_Blood;
return view('home', ['detect' => $detect]);
}
}
刀片文件如下所示:
<form method="post" action="{{ route('home.store') }}">
{!! csrf_field() !!}
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="checkbox_Adult_Person" name="Adult_Person"
{{ $detect->checkbox_Adult_Person == 1 ? "checked" : "" }}
>
<label class="custom-control-label" for="checkbox_Adult_Person">Adult Person</label>
</div>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="checkbox_Child" name="Child"
{{ $detect->checkbox_Child == 1 ? "checked" : "" }}
>
<label class="custom-control-label" for="checkbox_Child">Child</label>
答案 0 :(得分:2)
当您将数据从控制器传递到变量名数据时,您将在$ data变量中获取数据,return view('home',['data' => $detect_user ]);
将数组键命名为数据,因此必须从视图中调用诸如$ data [0]之类的所有内容,更改阵列键名称或将变量名称更改为刀片文件中的$ data
答案 1 :(得分:1)
return view('home',['data'=> $ detect_user]); 改成 return view('home',['detect_user'=> $ detect_user]);
答案 2 :(得分:1)
将return view('home',['data' => $detect_user ]);
替换为:
return view('home',['detect_user' => $detect_user ]);
如果您希望在视图中拥有更多“数据”,可以执行以下操作:
$data['detect_user'] = $detect_user;
$data['otherdata'] = $otherData;
$data['andanotherdata'] = $andanotherdata;
return view('home',['data => $data ]);
并在您的视图中访问$detect_user
,$otherData
和$andanotherdata
。