mardi 16 janvier 2018

Export to excel a table from laravel

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') - </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=""></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="" 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="" 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=""></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="" 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="" 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=""></a></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td>@foreach ($user->company as $company)@endforeach</td>
                        <td>@foreach ($user->department as $department)@endforeach</td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></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?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2mLEpxy
via IFTTT

Aucun commentaire:

Enregistrer un commentaire