我想从表中的数据库中提取数据,但它在视图中显示了未定义的变量错误,请帮助我解决这个问题
我的控制器
class showAttendanceController extends Controller
{
public function index()
{
$users=DB::select('select * from requests');
return view('showAttendance',compact('users'));
}
我的观点
@foreach ($users as $user)
<td>{{$user->date}}</td>
<td>{{$user->Name}}</td>
<td>{{$user->Misid}}</td>
<td>{{$user->semester}}</td>
<td>{{$user->Department}}</td>
<td>{{$user->section}}</td>
<td>{{$user->Attendance}}</td>
我的路线
Route::get('/showrecord','showAttendanceController@index')->name('showrecord');
控制器:
public function index()
{
$users = DB::table('requests')->get();
return view('showAttendance',compact('users'));
}
视图:
@foreach($users as $user)
<td>{{$user->date}}</td>
<td>{{$user->Name}}</td>
<td>{{$user->Misid}}</td>
<td>{{$user->semester}}</td>
<td>{{$user->Department}}</td>
<td>{{$user->section}}</td>
<td>{{$user->Attendance}}</td>
@endforeach
首先,最好使用迁移来创建表,以获得数据库一致性。进一步尝试使用模型与数据库交互例如
namespace App;
use IlluminateDatabaseEloquentModel;
class deposit extends Model
{
//
protected $keyType = 'string';
protected $table = "deposit";
protected $fillable = [
'uid', 'amount', 'title', 'description'
];
}
然后你可以在你的控制器中使用它
$deposit = deposit::find($request->deposit_id);
if($deposit){
return $deposit
} else {
return 'Some Error'}
首先,您必须在控制器中使用DB
public function index()
{
$users = DB::table('table_name')->get();
return view('showAttendance',compact('users'));
}
然后您必须确认您的返回视图文件位置是否正确。举个例子,如果你的showAttendance文件在你必须使用的文件夹中
return view('foldername.showAttendence',compact('users));