Undefined variable: type (View: D:\admin\hospital\resources\views\Dashboard\default\employees\create.blade.php)"

Published 1 week ago by devMoaa

controller

public function listType(Type $type) { $users = User::all(); $type = Type::find($type->id); $lists = Employee::where('type_id' ,'=', $type->id)->orderBy('first_name')->paginate(20); return view('Dashboard.default.employees.list' , compact('lists' ,'type','users')); }

create employee view

@foreach($filterTypes as $filterType) {{$filterType->name}} @endforeach

web.php:

Route::get('/list/{type}/listType', '[email protected]');

l have hospital employee types like

'doctor' 'accountant' 'laboratorist' 'nurse' 'pharmacist' 'receptionist' 'representative' 'casemanager'

and I would like to add types dynamic and then show all employees belong To certain type in certain list page

aurawindsurfing
Snapey
Snapey
1 week ago (770,895 XP)

why do you say that? your error is undefined type in create.blade?

devMoaa

yes

Snapey
Snapey
1 week ago (770,895 XP)

ok, so did you send $type to the view?

Show the full create.blade file

and make sure you put ``` on its own line before and after code blocks

devMoaa

@extends('Dashboard.default.layouts.main')

@section('title', ' Add employee')

@section('content')

<div class="panel panel-default">
    <div class="panel-heading no-print">
        <div class="btn-group">
            @foreach($filterTypes as $filterType)
            <a class="btn btn-primary" href="/list/{$type->id}/listType"> <i class="fa fa-list"></i> {{$filterType->name}}</a>
            @endforeach  
        </div>
    </div>
    <div class="panel-body panel-form">
            <div class="row">
                <div class="col-md-9 col-sm-12">

                {!! Form::open(['method' => 'POST', 'files' => true,'route' => ['store'],'style'=>'display:inline']) !!} 
                    {!! Form::label('name', ' الادوار') !!}
                    {!! Form::select('role_id', $roles , null, 
                    ['class' => 'form-control chosen-type']) !!}
                    </br>
                    {!! Form::label('name', 'اسم المستخدم') !!}
                    {!! Form::text('name', ' ' , ['class' => 'form-control'] )!!}
                    </br>
                    {!! Form::label('name', 'اسم الاول') !!}
                    {!! Form::text('first_name', ' ' , ['class' => 'form-control'] )!!}
                    </br>
                    {!! Form::label('name', 'اسم الاخير') !!}
                    {!! Form::text('last_name', ' ' , ['class' => 'form-control'] )!!}
                    </br>
                    {!! Form::label('name', 'الايميل') !!}
                    {!! Form::text('email', ' ' ,  ['class' => 'form-control']) !!}
                    </br>
                    {!! Form::label('name', ' الباسورد') !!}
                    {!! Form::password('password', ['class' => 'form-control']) !!}
                    </br>
                    {!! Form::label('name', ' النوع') !!}
                    {!! Form::select('sex', ['0' => 'male', '1' => 'female'], null, 
                    ['class' => 'form-control chosen-type']) !!}
                    </br>
                    {!! Form::label('file', ' صورة') !!}
                    {!! Form::file('image')!!}
                    </br>
                    {!! Form::label('name', 'وظيفة المستخدم') !!}
                    {!! Form::select('type_id',  $types , null, 
                    ['class' => 'form-control chosen-type']) !!}
                    </br>
                    {!! Form::label('name', ' العنوان') !!}
                    {!! Form::text('address', ' ' , ['class' => 'form-control'] )!!}
                    </br>
                    {!! Form::label('name', ' الهاتف') !!}
                    {!! Form::text('phone_no', ' ' , ['class' => 'form-control'] )!!}
                    </br> 
                    {!! Form::label('name', ' Status ') !!}</br>
                    {!! Form::label('name', 'متوفر' , ['class' => 'radio-inline']) !!}
                    {!!Form::radio('status', '1', true)!!} 
                    {!! Form::label('name', 'غير متوفر' , ['class' => 'radio-inline']) !!}
                    {!!Form::radio('status', '0')!!} 
                    </br>
                    {!! Form::reset('افراغ', array('class'=>'btn btn-primary')) !!}
                    {!! Form::submit('اضافة', array('class'=>'btn btn-primary')) !!}
                {!! Form::close() !!}
                    
                <div class="col-md-3"></div>
            </div>
        </div>
    </div>
</div>

@endsection

devMoaa
 public function listType(Type $type )
    {
        $users = User::all();
        $types = Type::where('name' , $type->id)->first();
        $lists = Employee::where('type_id' , $type->id)->orderBy('first_name')->paginate(20);
        return view('Dashboard.default.employees.list' , compact('lists' ,'types','users'));
    }

devMoaa
Route::get('list/{id}/listType', '[email protected]'); 

calder12

Your Controller is passing the variable types you're trying to use $type in the view which even if you change the variable name won't work because types will return multiples so it won't have an id. Where is filterTypes even coming from?

devMoaa
public function create()
    {
        $users = User::all();
        $filterTypes = Type::all();
        $types = Type::pluck('name' ,'id')->toArray();
        $roles = Role::pluck('name' ,'id')->toArray();
        return view('Dashboard.default.employees.create',compact( 'users ' , 'roles' , 'types' , 'filterTypes' ));
    } 
devMoaa
<div class="btn-group">
            @foreach($filterTypes as $filterType)
            <a class="btn btn-primary" href="/employees/{{$type->id}}/listType"> <i class="fa fa-list"></i> {{$filterType->name}}</a>
            @endforeach  
        </div>
calder12

In your Controller you're passing types as an array. In your view you're using $type.

Is type a field or relation on filterTypes? At a guess without seeing your DB structure it looks like your a tag should be:

<a class="btn btn-primary" href="/employees/{{$filterType->id}}/listType"> <i class="fa fa-list"></i> {{$filterType->name}}</a>
devMoaa

it is relation

type table

Schema::create('types', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->timestamps();
        });

employees table

Schema::create('employees', function (Blueprint $table) {
            $table->increments('id');
            $table->string('first_name');
            $table->string('last_name');
            $table->string('address')->nullable();
            $table->string('phone_no')->nullable();
            $table->string('image')->nullable();
            $table->boolean('sex')->default(0);
            $table->boolean('status')->default(1);

            $table->integer('type_id')->unsigned();;
            $table->foreign('type_id')->references('id')->on('types')
                  ->onUpdate('cascade')->onDelete('cascade');
                  
            $table->integer('user_id')->unsigned();;
            $table->foreign('user_id')->references('id')->on('users')
                  ->onUpdate('cascade')->onDelete('cascade');
        });
calder12

It is a relation between employees and types, where does the $fitlerTypes come into it?

Snapey
Snapey
1 week ago (770,895 XP)

you understand the problem?

here

@foreach($filterTypes as $filterType)
            <a class="btn btn-primary" href="/list/{$type->id}/listType"> <i class="fa fa-list"></i> {{$filterType->name}}</a>

you use $type->id in the button url when you probably meant $filterType->id

Please sign in or create an account to participate in this conversation.