首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未定义变量:数据(视图: C:\xampp\htdocs\prolearning\resources\views\education.blade.php) )

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

Stack Overflow用户
提问于 2018-10-21 09:49:37
回答 5查看 3.6K关注 0票数 0

我正在尝试获取数据并在我的视图中使用laravel显示它,但是我得到了上面的错误。请帮帮我。

My education.blade.php 代码:

代码语言:javascript
复制
<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 代码:

代码语言:javascript
复制
<?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 路由:

代码语言:javascript
复制
Route::get('education', 'EducationController@index');
Route::post('edu', 'EducationController@store');
Route::get('eudcation', 'EducationController@show');

给出的错误消息:

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

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

EN

回答 5

Stack Overflow用户

发布于 2018-10-21 13:13:01

代码语言:javascript
复制
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不可用,则需要在刀片文件中或通过在控制器中设置空变量来处理它。希望这能解决你的问题。:)

或者您可以像下面这样更新刀片文件

代码语言:javascript
复制
        <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>
票数 1
EN

Stack Overflow用户

发布于 2018-10-21 09:55:52

您已经在$data中请求了education.blade对象。

但是在返回$data时没有提供education.blade

你可能就是这样做的。

EducationController

代码语言:javascript
复制
public function index()
{

    $data = .. // whatever your data

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

Stack Overflow用户

发布于 2018-10-21 10:12:17

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52914001

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档