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

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

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:

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

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 :

			@foreach($expiredMaterials as $material)
			<tr>
				<td class="id">{{ $material->id }}</td>
				<td class="type">{{ $material->type }}</td>
				<td class="série">{{ $material->série }}</td>
				<td class="marque">{{ $material->marque }}</td>
				<td class="réfference">{{ $material->réfference }}</td>
				<td class="service">{{ $material->service }}</td>
				<td class="date">{{ $material->date->format('Y-m-d H:i:s') }}</td>
				<td class="date">{{ $material->expires_at->format('Y-m-d H:i:s') }}</td>
				<td class="status">{{ $material->status }}</td>
				<td class=" text-center">
					<a class="m-r-15 text-muted update" data-toggle="modal" data-id="'.$value->id.'" data-target="#update">
						<i class="fa fa-edit" style="color: #2196f3"></i>
					</a>
					<a href="{{ url('form/delete'.$value->id) }}" onclick="return confirm('Voulez-vous le supprimé?')">
						<i class="fa fa-trash" style="color: red;"></i>
					</a>
				</td>
			</tr>
			@endforeach

and this is my controller

<?php

namespace App\Http\Controllers;
use App\Models\Matériels;
use Illuminate\Http\Request;
use Auth;
use DB;

class MaterielsController extends Controller
{
   

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


    public function report(Request $request)
    {
        if (Auth::guest()) {
            return redirect()->route('/');
        }
    
        $fromdate = $request->fromdate;
        $todate = $request->todate;
        $name = $request->name;
    
        $data = DB::table('Matériels')
            ->where('date', '>=', $fromdate . ' 00:00:00')
            ->where('date', '<=', $todate . ' 23:59:59')
            ->where('série', 'like', '%' . $name . '%')
            ->orderBy('date', 'desc')
            ->get();
    
        $latestEntry = Matériels::latest()->first();
    
        return view('materiels_expires', compact('data', 'latestEntry'));
    }
}

another controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Matériels;
use Auth;
use DB;


class ReportController extends Controller
{
    // report
    public function report(Request $request)
    {
        if (Auth::guest()) {
            return redirect()->route('/');
        }
    
        $fromdate = $request->fromdate;
        $todate = $request->todate;
        $name = $request->name;
    
        $data = DB::table('Matériels')
            ->where('date', '>=', $fromdate . ' 00:00:00')
            ->where('date', '<=', $todate . ' 23:59:59')
            ->where('série', 'like', '%' . $name . '%')
            ->orderBy('date', 'desc')
            ->get();
    
        $latestEntry = Matériels::latest()->first();
    
        return view('report.report', compact('data', 'latestEntry'));
    }
    
}

my model

<?php

namespace App\Models;

use Illuminate\Support\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Matériels extends Model
{
    protected $dates = [
        'expire' => 'datetime',
    'dis' => 'datetime'
    ];

    protected $table = 'Matériels';
    
    use HasFactory;

    protected $fillable = [
        'type',
        'série',
        'marque',
        'réfference',
        'service',
        'status',
        'dis',
        'expire',
    ];

    protected static function boot()
    {
        parent::boot();
    
        static::creating(function ($model) {
            $model->expire = Carbon::parse(request()->input('expire'));
        });
    
        static::updating(function ($model) {
            if ($model->expire->isPast()) {
                $model->status = 'expiré';
                $model->save();
            }
        });
    }
    
    

    public function scopeExpired($query)
    {
        return $query->whereNotNull('expires_at')
                     ->where('expires_at', '<=', now())
                     ->where('status', '=', 'expiré');
    }
}

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:

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

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

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:

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

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

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:

确定