如何解决 Laravel 5.8 中紧凑的未定义变量错误

发布于 2025-01-14 07:44:54 字数 802 浏览 5 评论 0原文

我想从表中的数据库中获取数据,但它在视图中显示未定义的变量错误,请帮助我解决这个问题

我的控制器

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');

I want to fetch data from database in table but it show me undefined variable error in view please help me to solve this problem

my controller

class showAttendanceController extends Controller
{

    public function index()
    {
        $users=DB::select('select * from requests');

        return view('showAttendance',compact('users'));
    }

my view

@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>

My Route

Route::get('/showrecord','showAttendanceController@index')->name('showrecord');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

智商已欠费 2025-01-21 07:44:55

控制器:

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

Controller:

public function index()
{
  $users = DB::table('requests')->get();  
  return view('showAttendance',compact('users'));
}

View:

@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
心舞飞扬 2025-01-21 07:44:55

首先,最好使用迁移来创建表,以获得数据库一致性。
进一步尝试使用模型与数据库交互
例如

namespace App;

use Illuminate\Database\Eloquent\Model;

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'}

first of all, it's better to create your table using migrations in order to have Database Consistency.
Further try to use models to interact with Database
for example

namespace App;

use Illuminate\Database\Eloquent\Model;

class deposit extends Model
{
    //
    protected $keyType = 'string';
    protected $table = "deposit";
    protected $fillable = [
        'uid', 'amount', 'title', 'description'
    ];

}

and then you can use it in your controller

     $deposit = deposit::find($request->deposit_id);
            if($deposit){
             return $deposit
            } else { 
             return 'Some Error'}
灰色世界里的红玫瑰 2025-01-21 07:44:55

首先,您必须在控制器中使用数据库,

public function index()
{
 $users = DB::table('table_name')->get();  
 return view('showAttendance',compact('users'));
}

而不是确认您的返回视图文件位置是否正确。例如,如果您的 showAttendence 文件位于您必须使用的文件夹内

return view('foldername.showAttendence',compact('users));

first of all you have to use DB in your controller

public function index()
{
 $users = DB::table('table_name')->get();  
 return view('showAttendance',compact('users'));
}

than you have to confirm your return view file location is right or not.Exmple if your showAttendence file is inside a folder you have to use

return view('foldername.showAttendence',compact('users));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文