0

I am trying to fetch data and display it in my view using laravel but I am getting the above error. Please help me.

My view education.blade.php code:

<div class="container"><br>
    <h1 class="text-success text-center">Your Education Details</h1><br>
    <table  class="table table-bordered">
        <tr class="">
            <th>Degree</th>
            <th>University</th>
            <th>Country</th>
            <th>Year</th>
            <th>Research Area</th>
            <th>More Actions</th>
        </tr>
        @foreach($data as $value)
        <tr>
            <td> {{ $value ->degree}}</td>
            <td>{{ $value ->univ}}</td>
            <td>{{ $value ->country}}</td>
            <td>{{ $value ->year}}</td>
            <td>{{ $value ->research_area}}</td>
            <td><a href=""><button>Edit</button></a>&nbsp;<a href=""><button>Delete</button></a></td>
        </tr>
        @endforeach
    </table>

My EducationController.php code:

<?php

namespace App\Http\Controllers;
use Auth;
use DB;
use Illuminate\Http\Request;
use App\education;

class EducationController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        return view('education');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        //$education = new education($request->all());
       // $education->save();

        education::create([
            'user_id' => Auth::user()->id,
            'degree' => request('degree'),
            'univ' => request('univ'),
            'country' => request('country'),
            'year' => request('year'),
            'research_area' => request('research_area')
        ]);
        return 'inserted';
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show()
    {
        //
        $data['data'] = DB::table('education')->get();
        if(count ($data)>0){
            return view('education',$data);
        }
        else
        {
            return view('education');
        }
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

EducationController.php routes:

Route::get('education', 'EducationController@index');
Route::post('edu', 'EducationController@store');
Route::get('eudcation', 'EducationController@show');

Given error message:

Undefined variable: data (View: C:\xampp\htdocs\prolearning\resources\views\education.blade.php)

If some one have any idea that where is the problem please show me in my code.

0

5 Answers 5

1
public function index()
{
   $data['data'] = [];
   return view('education', $data);
}


public function show()
{
    $data['data'] = [];
    $db_data = DB::table('education')->get();
    if(count ($db_data)>0){
        $data['data'] = $db_data;
    }
    return view('education', $data);
 }

If $data not available, you need to handle it either on blade file, or by setting empty variable in the controller. Hope this will fix your issue. :)

Or you can update blade file like below

        <div class="container"><br>
            <h1 class="text-success text-center">Your Education Details</h1><br>
            @if(isset($data))
            <table  class="table table-bordered">
                <tr class="">
                    <th>Degree</th>
                    <th>University</th>
                    <th>Country</th>
                    <th>Year</th>
                    <th>Research Area</th>
                    <th>More Actions</th>
                </tr>
                @foreach($data as $value)
                <tr>
                    <td> {{ $value ->degree}}</td>
                    <td>{{ $value ->univ}}</td>
                    <td>{{ $value ->country}}</td>
                    <td>{{ $value ->year}}</td>
                    <td>{{ $value ->research_area}}</td>
                    <td><a href=""><button>Edit</button></a>&nbsp;<a href=""><button>Delete</button></a></td>
                </tr>
                @endforeach
            </table>
            @else
             <div>No data available</div>
            @endif
      </div>
0

You have requested $data object in education.blade.
But you have not provided the $data when returning the education.blade

this is how you may do it.

EducationController

public function index()
{

    $data = .. // whatever your data

    return view('education', ['data' => $data]);
}
0

In your controller,

//for education
Route::get('education', 'EducationController@index'); //Same route as show
Route::post('edu', 'EducationController@store');
Route::get('education', 'EducationController@show'); //Same Route as index

you fixed the spelling mistake, but there is one other thing. You are calling 2 separate functions of the controller on the same route. When you go to https://localhost/education , there are 2 functions to be called on the same route. That can cause problems. And you need to check $data in your view

education.blade.php

too, if it contains something or not.

0

I found the error, the errors are in routes Spelling Mistake. By calling Route::get('education','EducationController@show'); the code works.

0

Try

$this->data['data'] = DB::table('education')->get();
if(count ($data)>0){
return view('education',$this->data);
}
else
{
return view('education');
}

View

@if(isset($data))
@foreach($data as $value)
<tr>
<td> {{ $value ->degree}}</td>
<td>{{ $value ->univ}}</td>
<td>{{ $value ->country}}</td>
<td>{{ $value ->year}}</td>
<td>{{ $value ->research_area}}</td>
<td><a href=""><button>Edit</button></a>&nbsp;<a href=""><button>Delete</button></a></td>
</tr>
@endforeach
@endif

Not the answer you're looking for? Browse other questions tagged or ask your own question.