LARAVEL 8 - 无法将数据从控制器传递到视图 -> 未定义的变量 $test
LARAVEL 8 - cannot pass data from controller to view -> undefined variable $test
大家好,我遇到了一个我在 SO 中多次看到的问题,但我不明白为什么人们提供的 none 解决方案对我有用..
我正在尝试在下拉菜单中打印我的数据库中的所有 tables(这样我就可以选择一个 table 然后用一个 crud 修改它)使用 web.php 作为路由、控制器和视图。
在下面的示例中,我只是想传递一个变量,而不是整个数据库
这是我的控制器代码:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\View;
class EditController extends Controller
{
public function index(){
$tables = 'test';
return view('edit')->with('tables',$tables);
}
}
这是 edit.blade.php 的变量
<div class="card-body">
@if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
@endif
<div>
Select the table you want to edit
</div>
<div class="form-group row">
<label for="table_name" class="col-md-4 col-form-label text-md-right">Tables</label>
<div class="col-md-6" style="width: border-box">
<SELECT id= "table_name" type="text" class="form-control @error('tables') is-invalid @enderror" name='table_name' style="width: 300px">
<option> {{ $tables }}</option>
</SELECT>
@error('tables')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
</div>
最后,这是web.php
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/edit', [App\Http\Controllers\EditController::class, 'index']); // <-this is the controller
Route::get('/edit', [App\Http\Controllers\HomeController::class, 'openEditBlade'])->name('edit');
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('/logout', '\App\Http\Controllers\Auth\LoginController@logout');
同一路由不能有多个 Route::get() 函数。因此,如果您需要在控制器中调用任何额外的函数,请在控制器中执行。
Route::get('/edit', [App\Http\Controllers\EditController::class, 'index']);
在EditController.php
public function index() {
// run the codes
// call other function.
$this->openEditBlade();
// return view
return view('edit')->with('tables',$tables);
}
大家好,我遇到了一个我在 SO 中多次看到的问题,但我不明白为什么人们提供的 none 解决方案对我有用.. 我正在尝试在下拉菜单中打印我的数据库中的所有 tables(这样我就可以选择一个 table 然后用一个 crud 修改它)使用 web.php 作为路由、控制器和视图。 在下面的示例中,我只是想传递一个变量,而不是整个数据库
这是我的控制器代码:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\View;
class EditController extends Controller
{
public function index(){
$tables = 'test';
return view('edit')->with('tables',$tables);
}
}
这是 edit.blade.php 的变量
<div class="card-body">
@if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
@endif
<div>
Select the table you want to edit
</div>
<div class="form-group row">
<label for="table_name" class="col-md-4 col-form-label text-md-right">Tables</label>
<div class="col-md-6" style="width: border-box">
<SELECT id= "table_name" type="text" class="form-control @error('tables') is-invalid @enderror" name='table_name' style="width: 300px">
<option> {{ $tables }}</option>
</SELECT>
@error('tables')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
</div>
最后,这是web.php
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/edit', [App\Http\Controllers\EditController::class, 'index']); // <-this is the controller
Route::get('/edit', [App\Http\Controllers\HomeController::class, 'openEditBlade'])->name('edit');
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('/logout', '\App\Http\Controllers\Auth\LoginController@logout');
同一路由不能有多个 Route::get() 函数。因此,如果您需要在控制器中调用任何额外的函数,请在控制器中执行。
Route::get('/edit', [App\Http\Controllers\EditController::class, 'index']);
在EditController.php
public function index() {
// run the codes
// call other function.
$this->openEditBlade();
// return view
return view('edit')->with('tables',$tables);
}