nitrofoska
nitrofoska

Reputation: 9

Laravel 5.7.2 Undefined variable in View

I can not get the variable from the controller, if someone can help me will be great. Im working with 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>

the web.php

Route::resource('detect', 'DetectController');

Error:

ErrorException (E_ERROR) Undefined variable: detect_user (View: /var/www/html/laravel/test/resources/views/home.blade.php)

The database is updated but that variable do not go to the view. Can someone help with this, Thank!

EDIT: I try to change for this return view('home',['detect_user' => $detect_user ]); but don`t work neither. Thanks

Solution: return view('home',['detect_user' => $detect_user ]); its was very helpful. But my problem was the Undefined variable: detect_user becouse my class Detect dont have a view, well apparently when something call a view, first run the code of the index function, so in my home view code I just create an Detect Object and put the data from the database, so the user can watch and edit the previous saved data.

The code, maybe can help someone:

    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]);
              }
   } 

and the blade file looks like this:

        <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>

Upvotes: 0

Views: 951

Answers (3)

user8034901
user8034901

Reputation:

Replace return view('home',['data' => $detect_user ]); with:

return view('home',['detect_user' => $detect_user ]);

If you have more "data" you want in your view you could do:

$data['detect_user'] = $detect_user;
$data['otherdata'] = $otherData;
$data['andanotherdata'] = $andanotherdata;
return view('home',['data => $data ]);

and access $detect_user, $otherData and $andanotherdata in your view.

Upvotes: 1

Irfan Mumtaz
Irfan Mumtaz

Reputation: 31

you will get data in $data variable as you passed your data into variable name data from controller, return view('home',['data' => $detect_user ]); you named array key as data so you have to call every thing from view like this $data[0], change array key name or change variable name to $data in blade file

Upvotes: 2

J Ha
J Ha

Reputation: 1262

return view('home',['data' => $detect_user ]); change to return view('home',['detect_user' => $detect_user ]);

Upvotes: 1

Related Questions