簡體   English   中英

如何解決 laravel 5.8 中的 compact undefined variable 錯誤

[英]how to solve compact undefined variable error in laravel 5.8

我想從表中的數據庫中獲取數據,但它在視圖中顯示未定義的變量錯誤,請幫助我解決這個問題

我的 controller

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

Controller:

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 Illuminate\Database\Eloquent\Model;

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

}

然后你可以在你的 controller 中使用它

     $deposit = deposit::find($request->deposit_id);
            if($deposit){
             return $deposit
            } else { 
             return 'Some Error'}

首先,您必須在 controller 中使用 DB

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

比你必須確認你的返回視圖文件位置是否正確。例如,如果你的 showAttendence 文件在你必須使用的文件夾中

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM