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

o4tp2gmn  于 2021-06-19  发布在  Mysql
关注(0)|答案(5)|浏览(347)

这个问题在这里已经有答案了

使用php的“notice:未定义变量”、“notice:未定义索引”和“notice:未定义偏移量”(28个答案)
去年关门了。
我试图获取数据,并显示在我的视图中使用拉威尔,但我得到了上述错误。请帮帮我。
我的观点 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');

给定错误消息:
未定义变量:data(视图:c:\xampp\htdocs\prolearning\resources\views\education.blade.php)
如果有人知道问题出在哪里,请在我的代码中告诉我。
rqmkfv5c

rqmkfv5c1#

我发现了错误,这些错误都是拼写错误。通过呼叫 Route::get('education','EducationController@show'); 代码起作用了。

bksxznpy

bksxznpy2#

您已请求 $data 中的对象 education.blade .
但你没有提供 $data 当返回 education.blade 你可以这样做。
教育控制员

public function index()
{

    $data = .. // whatever your data

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

wsxa1bj13#

尝试

$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
2skhul33

2skhul334#

在你的控制器里,

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

你纠正了拼写错误,但还有一件事。您正在同一路线上调用控制器的两个独立函数。你什么时候去https://localhost/education ,在同一个路由上有两个要调用的函数。那会引起问题。你需要检查一下 $data 在你看来
教育.blade.php
也一样,如果它有什么东西或没有。

uyhoqukh

uyhoqukh5#

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不可用,您需要在blade文件上处理它,或者在控制器中设置空变量。希望这能解决您的问题。:)
或者你可以像下面这样更新blade文件

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

相关问题