0

I'm so confused why i can't export this to excel. Please help me. Theres an error where it can't read "all". Here's my code in my UsersController

public function userExport()
{
    Excel::create('data_function',function($excel){
        $excel->sheet('mysheet', function($sheet){
            $sheet->loadView('user.list');
        });
    })->download('xls');
}

Then here is it in my user/list.blade

<div class="row">
    <div class="col-lg-12">
        <h1 class="page-header">
            Employees
                <small>@lang('app.list_of_registered_users') - {{ $all }}</small> 
        </h1>
    </div>
</div>

@include('partials.messages')
    <form method="GET" action="" accept-charset="UTF-8" id="users-form">
<div class="row tab-search">
        <div class="col-md-2">
            <div class="form-group-sm"> 
                <select class="form-control form-control-sm" id="company" name="company">
                    <option selected  disabled>Company</option>
                    @foreach ($companies as $company)
                        @if (($company->id)=="1")
                        <option>All</option>
                        @else
                        <option value="{{ $company->id }}">{{ $company->name }}</option>
                        @endif
                    @endforeach
                </select>
            </div>
        </div>
        <div class="col-md-2">
            <button type="submit" class="btn btn-warning btn-sm btn-block" id="filter-user">
                <i class="glyphicon glyphicon-filter"></i>                
                Filter Employee
            </button>
        </div>
        <div class="col-md-1">
            <a href="{{ route('user.export') }}" class="btn btn-sm btn-success btn-block">
                <i class="fa fa-file-excel-o"></i>       
                Excel
            </a>
        </div>
        <div class="col-md-2">
            <a href="{{ route('user.create') }}" class="btn btn-primary btn-sm btn-block" id="add-user">
                <i class="glyphicon glyphicon-plus"></i>
                Add
                Employee
            </a>
        </div>
</div>

<div class="row tab-search">            <div class="col-md-2">
            <div class="form-group-sm">
                <select class="form-control form-control-sm" id="benefit" name="benefit">
                    <option selected  disabled>Benefits</option>
                    @foreach ($benefits as $benefit)
                        @if (($benefit->id)=="1")
                        <option>All</option>
                        @else
                        <option value="{{ $benefit->id }}">{{ $benefit->name }}</option>
                        @endif
                    @endforeach
                </select>
            </div>
        </div>
        <div class="col-md-3"> 
            <div class="form-group-sm">
                <div class="input-group custom-search-form-sm">
                    <input type="text" class="form-control form-control-sm" name="search" value="{{ Input::get('search') }}" placeholder="Search Name">
                    <span class="input-group-btn">
                        <button class="btn btn-default btn-sm" type="submit" id="search-users-btn">
                            <span class="glyphicon glyphicon-search"></span>
                        </button>
                        @if (Input::has('search') && Input::get('search') != '')
                            <a href="{{ route('user.list') }}" class="btn btn-danger btn-sm" type="button" >
                                <span class="glyphicon glyphicon-remove"></span>
                            </a>
                        @endif
                    </span>
                </div>
            </div>
        </div> 

</div>
</div>
    </form>
<div class="table-responsive" id="users-table-wrapper">
    <table class="table table-bordered table-striped table-hover table-condensed" id="datatable-default">
        <thead>
            <th>@lang('app.full_name')</th>
            <th>Sex<i type="button" class="fa fa-fw fa-sort"></th>
            <th>Birthday<button name="birthday_sort" style="background-color:#ffffff"><i class="fa fa-fw fa-sort"></button></th>
            <th>Age<i class="fa fa-fw fa-sort"></th>
            <th>Civil<br>Status<i class="fa fa-fw fa-sort"></th>
            <th>Company<i class="fa fa-fw fa-sort"></th>
            <th>Department<i class="fa fa-fw fa-sort"></th>
            <th>Employee<br>Status<i class="fa fa-fw fa-sort"></th>
            <th>Level<i class="fa fa-fw fa-sort"></th>
            <th>Date<br>Hired<i class="fa fa-fw fa-sort"></th>
            <th>Tenure</th>
        </thead>
        <tbody>
            @if (count($users))
                @foreach ($users as $user)
                    <tr class="gradeX">
                        <td><a href="{{ route('user.show', $user->id) }}">{{ $user->first_name . ' ' . $user->middle_name . ' ' . $user->last_name }}</a></td>
                        <td>{{ $user->gender[0] }}</td>
                        <td>{{ $user->birthday->format('M j/y') }}</td>
                        <td>{{ $user->age }}</td>
                        <td>{{ $user->civil_status }}</td>
                        <td>@foreach ($user->company as $company)@endforeach{{ $company->name }}</td>
                        <td>@foreach ($user->department as $department)@endforeach{{ $department->name }}</td>
                        <td>{{ $user->emp_status }}</td>
                        <td>{{ $user->level }}</td>
                        <td>{{ $user->date_hired }}</td>
                        <td>{{ $user->difference }}</td>
                    </tr>
                @endforeach
            @else
                <tr class="gradeX">
                    <td colspan="6"><em>@lang('app.no_records_found')</em></td>
                </tr>
            @endif
        </tbody>
    </table>


    {!! $users->render() !!}
</div>

The problem is that it says an error like "all","companies", and etc are undefined variables. Also because the companies is another table. How can define it in my excel that it won't be error again?

1 Answer 1

1

It seems you need to pass variables to view from controller which you are passing. Please check my code and update.

public function userExport()
{
    $companies = $this->companyModel->get();
    $all = $this->something->get();
    Excel::create('data_function',function($excel) use($all, $companies) {
        $excel->sheet('mysheet', function($sheet) use($all, $companies) {
            $sheet->loadView('user.list', ['all' => $all, 'companies' => $companies]);
        });
    })->download('xls');
}

and also pass the other variables with same as I passed to $sheet->loadView() in example. I think this will help you.

Sign up to request clarification or add additional context in comments.

3 Comments

Undefined property: Vanguard\Http\Controllers\Web\UsersController::$companyModel for the company model. What do i need to define in $companies = $this->companyModel->get(); @Lucky Saini
in your first line should i put the company model there?
@gwapo yes, you can add this code instead of first line $builder = new Company; $companies = $builder->get();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.