作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 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> <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> <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/
我正在尝试在 Laravel 中生成 pdf 文件,但收到错误 ErrorException (E_WARNING)fopen(C:\xampp\htdocs\prolearning\storage\
这个问题已经有答案了: "Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key",
我是一名优秀的程序员,十分优秀!