未定义的变量:数据(视图:C:\ xampp \ htdocs \ prolearning \ resources \ views \ education.blade.php)

时间:2018-10-21 09:49:38

标签: php mysql laravel

我正在尝试使用laravel获取数据并将其显示在视图中,但出现上述错误。请帮我。

我的视图 education.blade.php 代码:

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

我的 EducationController.php 代码:

<?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 路线:

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

给出错误消息:

  

未定义变量:数据(视图:C:\ xampp \ htdocs \ prolearning \ resources \ views \ education.blade.php)

如果有人知道问题出在哪里,请在我的代码中告诉我。

5 个答案:

答案 0 :(得分:0)

您已请求$data中的education.blade对象。
但是您在返回$data

时没有提供education.blade

这就是您可以这样做的方式。

EducationController

public function index()
{

    $data = .. // whatever your data

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

答案 1 :(得分:0)

我发现了错误,错误在于路线拼写错误。通过调用Route::get('education','EducationController@show');,代码即可工作。

答案 2 :(得分:0)

在您的控制器中,

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

您解决了拼写错误,但是还有另一件事。您在同一路径上调用控制器的2个独立功能。当您转到https://localhost/education时,将在同一路径上调用2个函数。那会引起问题。并且您需要在视图中检查$data

  

education.blade.php

也是,如果其中不包含某些东西。

答案 3 :(得分:0)

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

如果$ data不可用,则需要在刀片文件上或通过在控制器中设置空变量来处理它。希望这能解决您的问题。 :)

或者您可以如下更新刀片文件

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

答案 4 :(得分:0)

尝试

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

查看

@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