Home > database >  Laravel foreach variable not working ($tickets is undefined)
Laravel foreach variable not working ($tickets is undefined)

Time:11-30

Hello When I run page it tells me that: ErrorException Undefined variable: tickets (View: D:\xampp\htdocs\new-helpdesk\resources\views\tickets\index.blade.php) http://localhost:8000/tickets Hide solutions $tickets is undefined

when I dd it works.

index.blade.php


@extends('layouts.ticket')

@section('content')

@if ($tickets)
    

**
**@foreach ($tickets as $ticket)
    

<div >
    {!! Form::label('company', 'Company:', ['class' => 'form-label fs-6 d-inline-block']) !!}
    {!! Form::select('company', [' '=> 'Choose Company'], null,['class'=>'form-control']);!!}
</div>

@endforeach
@endif
@endsection

TicketsController

    <?php

namespace App\Http\Controllers;

use App\Models\Companies;
use App\Models\Tickets;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;



class TicketsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        // $tickets = Tickets::with('companies')->get();
        return view('tickets.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
    
        //
      //  return view('tickets.create');
         $tickets = Companies::all();
dd($tickets->toArray());
//  return view('tickets.index', compact('tickets'))->with('tickets', $tickets);
return view('tickets.index')->with(['tickets' => $tickets]);
         
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $request->validate([

            'ticket_title'=> 'required',
            'description'=>'required'
        ]);

        $query = DB::table('tickets')->insert([

        'ticket_title'=>$request->input('ticket_title'),

        'description'=>$request->input('description'),

        ]);
        
        if($query){
            return back()->with('success','Data have beeen inserted');
        } else {

            return back()->with('fail', 'Data not inserted');
        }

        

    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Tickets  $tickets
     * @return \Illuminate\Http\Response
     */
    public function show(Tickets $tickets)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Tickets  $tickets
     * @return \Illuminate\Http\Response
     */
    public function edit(Tickets $tickets)
    {
        //
        

        

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Tickets  $tickets
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Tickets $tickets)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Tickets  $tickets
     * @return \Illuminate\Http\Response
     */
    public function destroy(Tickets $tickets)
    {
        //
    }

    public function showcategory(){

        // $companies = Companies::pluck('name', 'id');

        // dd($companies);


    }
}

web.php

Route::resource('/tickets', TicketsController::class);

Route::resource('/companies', CompaniesController::class);

Tickets Model


public function companys(){

         return $this->belongsTo(Companies::class);
    }

Companies Model

public function tickets(){

        return $this->HasMany(Tickets::class);


    }

How can I solve this problem?

I try to change it as layout

CodePudding user response:

You have a little chaos in your return value, you either pass the collection in compact() or in with(). with() needs an array, so you will have to do one of the following:


return view('tickets.index', compact('tickets'));

//or

return view('tickets.index')->with(['tickets' => $tickets]);

=====EDIT

The /tickets route is calling your index() action. https://laravel.com/docs/9.x/controllers#actions-handled-by-resource-controller
This action also returns the index.blade.php view, but you are not passing the tickets collection.

public function index()
    {
       $tickets = Tickets::with('companies')->get();
       return view('tickets.index')->with(['tickets' => $tickets]);
    }

As TimLewis pointed out, the @if($tickets) in your index.blade.php has no purpose and can be removed.

  • Related