我是Laravel框架中的新手,我正在尝试构建一个管理数据库的工具。我已经创建了我的表,我设法从视图中填充它,我没有得到的是将表显示回视图。我构建了两个函数,一个只显示表单'Project_create@showpform'
,另一个应该使用插入的数据'Project_create@showtable'
更新相同的视图。我收到错误:"Undefined variable: project_data (View:/var/www/tcc/src/resources/views/form_project.blade.php)"
。
在这里你可以看到我的路线:
Route::get('/', 'DataController@overview');
Route::get('/myproject', 'Project_create@showpform');
Route::post('/myproject', 'Project_create@showtable');
Route::get('/upload','DataController@showform');
Route::post('/upload', 'DataController@read_xsl');
Route::get('/scrape', 'DataController@scrape');
这是我的Project_create
控制器:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
class project_create extends Controller
{
public function showpform()
{
return view('form_project');
}
public function showtable(Request $request)
{
$name = $request->input('name');
$description = $request->input('description');
\DB::table('projects')->insert(
[
'project_id' => 1,
'name' => $rawUrl,
'description' => $hashedUrl,
'created_at' => \Carbon\Carbon::now(),
]);
$project_data = \DB::table('projects')->select('*')->get();
return View::make('form_project', compact('project_data'));
}
}
以下是我的观点:
<html lang="en">
<head>
<title>File Upload</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
</head>
<body>
<div class="container">
<h3 class="jumbotron">Create here your project</h3>
<form class="w3-container w3-light-grey" action={{action('DataController@showform')}} enctype="multipart/form-data">
{{csrf_field()}}
<p>
<label>Project Name</label>
<input class="w3-input w3-border w3-round" name="name" type="text"></p>
<p>
<label>Project Description</label>
<input class="w3-input w3-border w3-round" name="description" type="text"></p>
<button type="submit" class="btn btn-primary" style="margin-top:10px">Create Project</button>
</form>
</div>
<table>
<thead>
<tr>
<th> id</th>
<th> name</th>
<th> description</th>
<th> created_at </th>
</tr>
</thead>
<tbody>
@foreach($project_data as $data)
<tr>
<td> {{$data->id}} </td>
<td> {{$data->name}} </td>
<td> {{$data->description}} </td>
<td> {{$data->created_at}} </td>
</tr>
@endforeach
</tbody>
</table>
</body>
</html>
感谢您的帮助。
答案 0 :(得分:0)
您的视图有一行
@foreach($project_data as $data)
但是当你打电话时
public function showpform()
你没有通过
$project data
喜欢
public function showtable(Request $request)
...
return View::make('form_project', compact('project_data'));
在Blade文件中添加isset
功能。 e.g。
@if(isset($project_data))
@foreach($project_data as $data)
<tr>
<td> {{$data->id}} </td>
<td> {{$data->name}} </td>
<td> {{$data->description}} </td>
<td> {{$data->created_at}} </td>
</tr>
@endforeach
@endif