1

I've passed a variable temp from controller to view page. While debugging the search method, I am getting data from database to the variable temp. When I pass this variable to view page, it is showing error as

 ErrorException (E_ERROR)
Undefined variable: temp (View: C:\xampp\htdocs\laravel\lsapp\resources\views\pages\MySearch.blade.php)

I doubt the error is most likely due to route configuration but I am not sure. There might be other errors too. But the process of passing variable from controller to view page is same, but also I am getting the error.

controller code block

public function searchDev()
    {
        return view ( 'pages.MySearch');
    }
public function search(Request $request)
{
    $UserName = $request->input('MyName');

    if($UserName != ""){
        $temp = temp::where ( 'NAME', 'LIKE', '%' . $UserName . '%' )->get (['id','NAME','CONTACT','TEMP_ADDRESS']);
        if (count ( $temp ) > 0)
        {
            /*
                getting result from database
                dump($temp);                 
                return response()->json($temp);
                 */
            return view('pages.MySearch', [
                'temp' => $temp
            ]);
        }                
        else
        {
            return view ( 'pages.MySearch')->with('alert-danger', 'Sorry No details found');
        }

    }
}

view page code block

@foreach($temp as $data)
 <tr>
      <td> {{$data['NAME']}} </td>
     <td> {{$data['CONTACT']}} </td>
     <td> {{$data['TEMP_ADDRESS']}} </td>
</tr>
@endforeach

route code

Route::get('/MySearch','MyController@searchDev');
Route::post('/MySearch','MyController@search');

The error is general, but I can't debug the error what is the cause of the error. Please help!!!

2 Answers 2

2

As you are not always passing temp variable to view, you must check that whether the variable exists or not before using it in your blade view. Check that whether the $temp variable exists before using it anywhere in blade

@if(isset($temp))
@foreach($temp as $data)
 <tr> 
       <td> {{$data['NAME']}} </td>
       <td> {{$data['CONTACT']}} </td> 
       <td> {{$data['TEMP_ADDRESS']}} </td> 
</tr>
@endforeach
@endif

Or alternatively, you should check at the start of blade view that a variable exists or assign assign it some default value so the page won't break.

@php
$temp = isset($temp) ? $temp : [];
@endphp
Sign up to request clarification or add additional context in comments.

3 Comments

Thank it solved my problem. I want to know, what this code block do @if(isset($temp)), keeping this if my problem was solved. Please reply. Thank You!!!
this condition will be true, if the $temp vaiable is set and it is not null. You @foreach loop will be excuted only if $temp exists. You can find further details here. link
Thank you for the explanation
2

In your else section you are not passing any variable

So use this instead

return view ( 'pages.MySearch', compact('temp'))->with('alert-danger', 'Sorry No details found');

for blade:

 @foreach($temp as $data)
 <tr>
      <td> {{$data->name}} </td>
     <td> {{$data->CONTACT}} </td>
     <td> {{$data->TEMP_ADDRESS}} </td>
</tr>
@endforeach

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.