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

huangapple go评论96阅读模式
英文:

how to solve undefind variable in laravel

问题

It seems like you're encountering an "Undefined variable $expiredMaterials" error. To fix this, make sure you have defined and passed the variable $expiredMaterials in your controller. In your MaterielsController, you already have the code to retrieve and pass it to the view:

  1. public function index()
  2. {
  3. $expiredMaterials = Matériels::expired()->get();
  4. return view('materiels_expires', compact('expiredMaterials'));
  5. }

Ensure that the Matériels model is correctly defined and that the expired() scope is working as expected. If you're still experiencing issues, please double-check your model and scope implementation.

英文:

i'm getting undefind variable error how to fix the error please
Undefined variable $expiredMaterials (View: C:\Users\Fatima\Desktop\PFE\Gpi_projet\resources\views\materiels_expires.blade.php

here is my code :

  1. @foreach($expiredMaterials as $material)
  2. <tr>
  3. <td class="id">{{ $material->id }}</td>
  4. <td class="type">{{ $material->type }}</td>
  5. <td class="série">{{ $material->série }}</td>
  6. <td class="marque">{{ $material->marque }}</td>
  7. <td class="réfference">{{ $material->réfference }}</td>
  8. <td class="service">{{ $material->service }}</td>
  9. <td class="date">{{ $material->date->format('Y-m-d H:i:s') }}</td>
  10. <td class="date">{{ $material->expires_at->format('Y-m-d H:i:s') }}</td>
  11. <td class="status">{{ $material->status }}</td>
  12. <td class=" text-center">
  13. <a class="m-r-15 text-muted update" data-toggle="modal" data-id="'.$value->id.'" data-target="#update">
  14. <i class="fa fa-edit" style="color: #2196f3"></i>
  15. </a>
  16. <a href="{{ url('form/delete'.$value->id) }}" onclick="return confirm('Voulez-vous le supprimé?')">
  17. <i class="fa fa-trash" style="color: red;"></i>
  18. </a>
  19. </td>
  20. </tr>
  21. @endforeach

and this is my controller

  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Matériels;
  4. use Illuminate\Http\Request;
  5. use Auth;
  6. use DB;
  7. class MaterielsController extends Controller
  8. {
  9. public function index()
  10. {
  11. $expiredMaterials = Matériels::expired()->get();
  12. return view('materiels_expires', compact('expiredMaterials'));
  13. }
  14. public function report(Request $request)
  15. {
  16. if (Auth::guest()) {
  17. return redirect()->route('/');
  18. }
  19. $fromdate = $request->fromdate;
  20. $todate = $request->todate;
  21. $name = $request->name;
  22. $data = DB::table('Matériels')
  23. ->where('date', '>=', $fromdate . ' 00:00:00')
  24. ->where('date', '<=', $todate . ' 23:59:59')
  25. ->where('série', 'like', '%' . $name . '%')
  26. ->orderBy('date', 'desc')
  27. ->get();
  28. $latestEntry = Matériels::latest()->first();
  29. return view('materiels_expires', compact('data', 'latestEntry'));
  30. }
  31. }

another controller

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Models\Matériels;
  5. use Auth;
  6. use DB;
  7. class ReportController extends Controller
  8. {
  9. // report
  10. public function report(Request $request)
  11. {
  12. if (Auth::guest()) {
  13. return redirect()->route('/');
  14. }
  15. $fromdate = $request->fromdate;
  16. $todate = $request->todate;
  17. $name = $request->name;
  18. $data = DB::table('Matériels')
  19. ->where('date', '>=', $fromdate . ' 00:00:00')
  20. ->where('date', '<=', $todate . ' 23:59:59')
  21. ->where('série', 'like', '%' . $name . '%')
  22. ->orderBy('date', 'desc')
  23. ->get();
  24. $latestEntry = Matériels::latest()->first();
  25. return view('report.report', compact('data', 'latestEntry'));
  26. }
  27. }

my model

  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Support\Carbon;
  4. use Illuminate\Database\Eloquent\Factories\HasFactory;
  5. use Illuminate\Database\Eloquent\Model;
  6. class Matériels extends Model
  7. {
  8. protected $dates = [
  9. 'expire' => 'datetime',
  10. 'dis' => 'datetime'
  11. ];
  12. protected $table = 'Matériels';
  13. use HasFactory;
  14. protected $fillable = [
  15. 'type',
  16. 'série',
  17. 'marque',
  18. 'réfference',
  19. 'service',
  20. 'status',
  21. 'dis',
  22. 'expire',
  23. ];
  24. protected static function boot()
  25. {
  26. parent::boot();
  27. static::creating(function ($model) {
  28. $model->expire = Carbon::parse(request()->input('expire'));
  29. });
  30. static::updating(function ($model) {
  31. if ($model->expire->isPast()) {
  32. $model->status = 'expiré';
  33. $model->save();
  34. }
  35. });
  36. }
  37. public function scopeExpired($query)
  38. {
  39. return $query->whereNotNull('expires_at')
  40. ->where('expires_at', '<=', now())
  41. ->where('status', '=', 'expiré');
  42. }
  43. }

i'm trying to diplay a list of records entered by the user in an html table and these records have an expiration datetime when they are expired they are displayed in another section

答案1

得分: 0

Variables must be passed to the view like this:

  1. return view('view', ['variableName' => 'variableValue']);

So in your case, you will need to write your return like so:

  1. return view('materiels_expires', ['expiredMaterials' => compact('expiredMaterials')]);

Then you should be able to access $expiredMaterials in your view.

英文:

Variables must be passed to the view like this:

  1. return view('view', ['variableName' => 'variableValue']);

So in your case, you will need to write your return like so:

  1. return view('materiels_expires', ['expiredMaterials' => compact('expiredMaterials')]);

Then you should be able to access $expiredMaterials in your view.

huangapple
  • 本文由 发表于 2023年5月13日 08:45:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76240628.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定