Я не могу получить переменную из контроллера, если кто-то может мне помочь, будет здорово.Я работаю с 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)
База данных обновлена, но эта переменная не попадает в представление.Может кто-нибудь помочь с этим, Спасибо!
РЕДАКТИРОВАТЬ : я пытаюсь изменить для этого return view('home',['detect_user' => $detect_user ]);
, но не работает ни один.Спасибо
Решение : return view('home',['detect_user' => $detect_user ]);
было очень полезно.Но моя проблема была в Undefined variable: detect_user
, потому что у моего класса Detect нет представления, ну, очевидно, когда что-то вызывает представление, сначала запустите код индексной функции, поэтому в своем коде домашнего представления я просто создаю объект Detect и помещаю данныеиз базы данных, чтобы пользователь мог просматривать и редактировать предыдущие сохраненные данные.
Код, может быть, может кому-то помочь:
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>