我正在尝试获取数据并在我的视图中使用laravel显示它,但是我得到了上面的错误。请帮帮我。
My 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>My 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) )
如果有人知道问题出在哪里,请在我的代码中告诉我。
发布于 2018-10-21 13:13:01
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> <a href=""><button>Delete</button></a></td>
</tr>
@endforeach
</table>
@else
<div>No data available</div>
@endif
</div>发布于 2018-10-21 09:55:52
您已经在$data中请求了education.blade对象。
但是在返回$data时没有提供education.blade。
你可能就是这样做的。
EducationController
public function index()
{
$data = .. // whatever your data
return view('education', ['data' => $data]);
}发布于 2018-10-21 10:12:17
我发现了错误,错误是在拼写错误的路线上。通过调用Route::get('education','EducationController@show');,代码可以工作。
https://stackoverflow.com/questions/52914001
复制相似问题