Я пытаюсь получить данные и отобразить их в моем представлении с помощью laravel, но получаю указанную выше ошибку. Помогите, пожалуйста.
Мой взгляд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)
Если у кого-то есть идея, в чем проблема, пожалуйста, покажите мне мой код.






Вы запросили объект $data в education.blade.
Но вы не предоставили $data при возврате education.blade.
вот как вы можете это сделать.
EducationController
public function index()
{
$data = .. // whatever your data
return view('education', ['data' => $data]);
}
Я обнаружил ошибку, ошибки в маршрутах Spelling Mistake. При вызове Route::get('education','EducationController@show'); код работает.
В вашем контроллере
//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
вы исправили орфографическую ошибку, но есть еще один нюанс. Вы вызываете 2 отдельные функции контроллера по одному и тому же маршруту. Когда вы переходите к https: // localhost / образование, на одном и том же маршруте вызываются 2 функции. Это может вызвать проблемы. И вам нужно проверить $data в вашем представлении
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 недоступен, вам нужно обработать его либо в блейд-файле, либо установив пустую переменную в контроллере. Надеюсь, это решит вашу проблему. :)
Или вы можете обновить файл лезвия, как показано ниже
<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>
Пытаться
$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> <a href = ""><button>Delete</button></a></td>
</tr>
@endforeach
@endif