gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 16:49:10 27 4
gpt4 key购买 nike

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

我的观点 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');

给出的错误消息:

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

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

最佳答案

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 文件上处理它,或者通过在 Controller 中设置空变量来处理它。希望这能解决您的问题。 :)

或者您可以像下面一样更新 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>

关于php - undefined variable : data (View: C:\xampp\htdocs\prolearning\resources\views\education.blade.php),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52914001/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com