How to export/download Excel View from Laravel - php

Following this Tutorial and this Git package I tried implementing a new feature option to export or download excel data from a specific project
but the issue at hand when clicking on the download button nothing happens. any ideas what I did wrong ?
Edit : I already tried changing button type value
app\Exports\UsersDataExport
namespace App\Exports;
use Illuminate\Contracts\View\View;
use App\Models\User;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
class UsersDataExport implements FromView,ShouldAutoSize
{
use Exportable;
private $users;
function __construct()
{
$this->users = User::all();
}
public function view() : View
{
return view('excel.usersdetails', [
'users' => $this->users
]);
}
}
app\Http\HomeControllers
use App\Exports\UsersDataExport;
use Maatwebsite\Excel\Facades\Excel;
public function exportExcel()
{
return Excel::download(new UsersDataExport, 'users-data.xlsx');
}
routes\web.php
Route::post('users/export-excel', [HomeController::class, 'exportExcel'])->name('users.download-excel')
resources\views\index.blade.php
<div>
<form action="{{ route('users.download-excel') }}" method="POST" target="__blank">
#csrf
<div>
<button type="button" class="btn btn-success" value="button">Excel</button>
</div>
</form>
</div>
resources\views\excel\usersdetails.blade.php
<body>
<table style="position: relative; top: 50px">
<thead>
<tr>
<th>nom et prenom du destinaire</th>
<th>telephone</th>
<th>code wilaya</th>
<th>commune de livraison</th>
<th>adresse de livraison</th>
<th>montant du colis</th>
</tr>
</thead>
<tbody>
#foreach($users as $user)
<tr>
<td data-column="nom et prenom du destinaire">John Doe</td>
<td data-column="telephone">000</td>
<td data-column="code wilaya">56</td>
<td data-column="commune de livraison">11</td>
<td data-column="adresse de livraison">AAA</td>
<td data-column="montant du colis">100</td>
</tr>
#endforeach
</tbody>
</table>
<script src="js/main.js"></script>
</body>

Related

How to show image using storage folder for looping data

My image is not displaying. I'm using Storage:: to save my image. Here how my folder looks like and php artisan storage:link has been created. I already try this code {{ asset('storage/complaint' . $c->image)}},{{ storage_path() . '/complaint' .$c->image}}and {{ url('/public/complaint' . $c->image) }} but it seems to be not working at all.
web.php
Route::get('/report-form','ComplaintController#create');
Route::post('/report-create','ComplaintController#store');
Route::get('/report-view','ComplaintController#index');
ComplaintController.php
<?php
namespace App\Http\Controllers;
use Auth;
use Validator;
use Response;
use Carbon\Carbon;
use App\Complaint;
use Illuminate\Support\Facades\Storage;
use Illuminate\Http\Request;
use Intervention\Image\ImageManagerStatic as Image;
class ComplaintController extends Controller
{
public function index(Request $request)
{
if (Auth::user()->role == 'buyer')
{
$complaint = Complaint::where('report_by',Auth::user()->id)->get();
return view('buyers.complaints.index',compact('complaint'));
}
}
public function create(Request $request)
{
return view('buyers.complaints.create');
}
public function store(Request $request)
{
if (count($request->defect_id) > 0) {
foreach($request->defect_id as $item=>$v) {
if (isset($request->image[$item])) {
$images = $request->file('image');
$image_resize = Image::make($images[$item]->getRealPath());
$image_resize->resize(900, 630);
$filename = $images[$item]->getClientOriginalName();
Storage::put($filename, $image_resize);
Storage::move($filename, 'public/complaint/' . $filename);
}
$data = array(
'defect_id' => $request->defect_id[$item],
'image' => $filename,
'description' => $request->description[$item],
'report_by' => Auth::user()->id,
'created_at' => Carbon::now()->toDateTimeString(),
'updated_at' => Carbon::now()->toDateTimeString()
);
Complaint::insert($data);
}
}
return redirect('/report-form')->with('success','Your report is submitted!');
}
create.blade.php
<div class="panel">
<form action="/report-create" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
<table class="table table-bordered">
<thead>
<tr>
<th><center>Type of Defect</center></th>
<th><center>Image</center></th>
<th><center>Description</center></th>
<th><center>Action</center></th>
</tr>
</thead>
<tbody>
<tr>
<td width="20%">
<select class="form-control" name="defect_id[]">
<option value="" selected>Choose Defect</option>
#foreach(App\Defect::all() as $defect)
<option value="{{$defect->id}}">{{$defect->name}}</option>
#endforeach
</td>
<td width="15%">
<input type="file" class="form-control-file" name="image[]">
</td>
<td width="45%">
<input type="text" class="form-control" name="description[]">
</td>
<td width="10%">
<button type="button" class="btn btn-info btn-sm" id="add-btn"><i class="glyphicon glyphicon-plus"></i></button>
</td>
</tr>
</tbody>
</table>
<center><button type="submit" class="btn btn-primary">Submit</button></center>
<br>
</form>
</div>
#section('footer')
<script>
$(document).ready(function () {
$('#add-btn').on('click',function () {
var html = '';
html += '<tr>';
html += '<td><select class="form-control" name="defect_id[]"><option value="" selected>Choose Defect</option>#foreach(App\Defect::all() as $defect)<option value="{{$defect->id}}">{{$defect->name}}</option>#endforeach</td>';
html += '<td><input type="file" class="form-control-file" name="image[]"></td>';
html += '<td><input type="text" class="form-control" name="description[]"></td>';
html += '<td><button type="button" class="btn btn-danger btn-sm" id="remove-btn"><i class="glyphicon glyphicon-minus"></i></button></td>';
html += '</tr>';
$('tbody').append(html);
})
});
$(document).on('click','#remove-btn',function () {
$(this).closest('tr').remove();
});
</script>
#stop
index.blade.php
<div class="panel-heading">
<h3 class="panel-title"><strong>List of Report</strong></h3>
</div>
<div class="panel-body">
<table class="table table-hover">
<thead>
<tr>
<th>Defect Name</th>
<th>Description</th>
<th>Image</th>
</tr>
</thead>
#foreach($complaint as $c)
<tr class="">
<td>{{$c->defect->name}}</td>
<td>{{$c->description}}</td>
<td><img src="{{ asset('storage/complaint' . $c->image)}}" class="" alt="{{$c->image}}"></td>
</tr>
#endforeach
</table>
</div>
What is my mistakes?
use Storage::url() ref link https://laravel.com/docs/8.x/filesystem#file-urls
<td><img src="{{ Storage::url('complaint/' . $c->image)}}" class="" alt="{{$c->image}}"></td>
NOTE: make sure APP_URL=http://exmaplet.test this is set

Laravel - How to Implement Search from Controller and View

I am using Laravel-5.8 for web application. I have this model class:
class HrEmployee extends Model
{
protected $table = 'hr_employees';
protected $fillable = [
'employee_code',
'address',
'company_id',
'email',
'employment_date',
'first_name',
'last_name',
'local_government_id',
'nationality_id',
'other_name',
'password',
'phone',
'resignation_date',
'is_hod',
'department_id',
];
protected $dates = [
'created_at',
'updated_at',
'date_of_birth',
'employment_date',
'resignation_date',
];
public function company()
{
return $this->belongsTo('App\Models\Organization\OrgCompany','company_id');
}
public function department()
{
return $this->belongsTo('App\Models\Organization\OrgDepartment','department_id');
}
public function fullName()
{
return $this->first_name . ' ' . $this->other_name . ' ' . $this->last_name;
}
}
Controller
class HrEmployeesController extends Controller
{
public function index()
{
$userCompany = Auth::user()->company_id;
if (Auth::user()->hasRole('Super Admin')) {
$employees = HrEmployee::paginate(6);
} else {
$employees = HrEmployee::where('company_id', $userCompany)->paginate(6);
}
return view('hr.employees.index')->with('employees', $employees);
}
}
view
<table class=" table table-bordered table-striped table-hover datatable">
<thead>
<tr>
<th width="10">
#
</th>
<th>Name</th>
<th>Email</th>
<th>Employee Code </th>
<th>Designation</th>
<th>Department</th>
<th>Employment Date</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
#foreach($employees as $key => $employee)
<td>
{{$key+1}}
</td>
<td>
{{$employee->first_name}} {{$employee->last_name}}
</td>
<td>
{{$employee->email}}
</td>
<td>
{{isset($employee->employee_code) ? $employee->employee_code : ''}}
</td>
<td>
{{isset($employee->designation) ? $employee->designation->designation_name : ''}}
</td>
<td>
{{isset($employee->department) ? $employee->department->dept_name : ''}}
</td>
<td>
{{$employee->employment_date ? Carbon\Carbon::parse($employee->employment_date)->format('d-m-Y') : 'N/A' }}
</tr>
#endforeach
</tbody>
</table>
I want the users to be able to search by Name (first_name and last_name) - textbox, department - dropdownlist and employee_code - textbox. When the user is done, he clicks on submit button, then the result is displayed.
How do I implement this from both controller and view.
Thank you.
you can add $request var or just use $_GET on index function to get param/query of search and use where "LIKE" to filter your data
make form in your view
<form action="">
<div class="input-group col-md-4">
<input type="text" name="s" class="form-control" value="{{ $_GET['s'] ?? '' }}" placeholder="Search something...">
<span class="input-group-btn">
<button class="btn btn-default" type="submit">Search!</button>
</span>
</div>
</form>
and your controller on index function
$s = ($_GET['s']??'');
$employees = HrEmployee::where(function($q){
$q->where('first_name','LIKE','%'.$s.'%')->orWhere('last_name','LIKE','%'.$s.'%');
})->paginate(10)->appends($_GET);
I try to give you code that i always do,
in your blade add this code
{{ Form::open(['action'=> 'HrEmployeesController#search', 'method'=>'POST']) }}
<label>Barcode : {{Form::text('FirstName','',['class'=>'form-control', ])}} </label>
{{Form::submit('Submit',['class'=>'btn btn-primary btn-sm button'])}}
{{Form::close()}}
while in your controller add search fuction
public function search(Request $request)
{
//
$employees = HrEmployee::where('first_name','like',"%".$request->input('FirstName')."%")->paginate(6)
return view('hr.employees.index')->with('employees', $employees);
}
and add it to your web.php
Route::post('employees/Search','HrEmployeesController#search');
You can understand the idea i give and hope it help

Data not fetching of current user in laravel

I am trying to get the data of current log in user but i am getting the result of all users in database. While in all views same problem is accure please help.
My code is given
My view is personal.blade.php
#extends('layouts.app')
#section('content')
<div class="container"><br>
<h1 class="text-success text-center">Profile</h1><br>
<table class="table table-bordered">
#foreach($data as $value)
<tr>
<th>Name</th>
<td>{{ $value ->tname}}</td>
</tr>
<tr>
<td>Father Name</td>
<td>{{ $value ->fname}}</td>
</tr>
<tr>
<td>Email</td>
<td>{{ $value ->email}}</td>
</tr>
<tr>
<td>Official Email</td>
<td>{{ $value ->off_email}}</td>
</tr>
#endforeach
</table>
<div class="form-group">
<label>Generate Report</label>
<div class="radio form-control">
<label class="col-md-4"><input type="radio" name="status" value="conf" >Personal</label>
<label class="col-md-4"><input type="radio" name="status" value="general">Publications</label>
</div>
</div>
<button class="btn btn-info" data-toggle="collapse" data-target="#demo"> Generate PDF</button>
<button class="btn btn-info" data-toggle="collapse" data-target="#demo"> Generate CSV</button>
<button class="btn btn primary">Go to Home</button><br>
<div id="demo" class="collapse">
<h3 class="text-success text-center">Complete your publication's detail</h3><br>
<div class="col-md-offset-3 col-md-6 m-auto d-block">
<form action="pub" method="post">
<input type="hidden" name="_token" value="{{csrf_token()}}">
<div>
#endsection
My controller is EducationController.php and its code is given.
<?php
namespace App\Http\Controllers;
use Auth;
use Illuminate\Http\Request;
use App\education;
use DB;
class EducationController extends Controller
{
public function show()
{
//
$data['data'] = DB::table('users')->get();
if(count ($data)>0){
return view('personal',$data);
}
else
{
return view('personal');
}
}
}
My route code is given here.
Route::get('/home', 'HomeController#index')->name('home');
Route::get('/personal','EducationController#personal');
Please help me. I dont know how to use the current user in all views. I am login while in result I am getting the result of all users which is stored in database. thanks in advance.
You use a #foreach which implies you expect multiple users in your view.
If you only want to show details for the logged in user you should do:
<?php
class EducationController extends Controller
{
public function show()
{
if(\Auth::check()){
$user = \Auth::user();
return view('personal',compact('user'));
}
else
{
return view('personal');
}
}
}
And in your view:
#extends('layouts.app')
#section('content')
<div class="container"><br>
<h1 class="text-success text-center">Profile</h1><br>
<table class="table table-bordered">
#if (isset($user))
<tr>
<th>Name</th>
<td>{{ $user->tname}}</td>
</tr>
<tr>
<td>Father Name</td>
<td>{{ $user->fname}}</td>
</tr>
<tr>
<td>Email</td>
<td>{{ $user->email}}</td>
</tr>
<tr>
<td>Official Email</td>
<td>{{ $user->off_email}}</td>
</tr>
#endif
</table>
// Rest of view
It is because your code is actually for getting all users. You should do it like this :
<?php
namespace App\Http\Controllers;
use Auth;
use Illuminate\Http\Request;
use App\education;
use DB;
class EducationController extends Controller
{
public function show()
{
$user = Auth::user();
return view('personal',compact('user'));
}
}
}
then in your view:
<tr>
<th>Name</th>
<td>{{ $user->tname}}</td>
</tr>
<tr>
<td>Father Name</td>
<td>{{ $user->fname}}</td>
</tr>
<tr>
<td>Email</td>
<td>{{ $user->email}}</td>
</tr>
<tr>
<td>Official Email</td>
<td>{{ $user->off_email}}</td>
</tr>
Note that if you have multiple auth, you must specify your guard :
Auth::guard('yourguard')->user();
otherwise, your user will return a null.

Can't get the right view in laravel 5.6

"Undefined variable: Pro (View: E:\xampp\htdocs\mypro\resources\views\folder\product.blade.php)" I am using laravel 5.6
trying to get data from existing database.
Product Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $table='products';
protected $primaryKey = 'p_id';
protected $fillable = ['p_title', 'p_element', 'p_description', 'p_duration', 'p_amount', 'p_startDate', 'p_endDate', 'p_old_price', 'p_new_price', 'p_keyWords', 'p_category', 'p_status', 'prefertime'];
}
Product Controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ProductController extends Controller
{
public function index()
{
$pro=Product::all();
return view('Folder.product'), compact('pro'));
}
}
Product.blade.php
#extends('layouts.app')
#section('content')
<h1>Products</h1>
<table style="width:100%">
<tr>
<th>ID</th>
<th>Name</th>
</tr>
<tr>
#foreach($Pro as $row)
<td>{{$row['p_id']}}</td>
<td>{{$row['p_title']}}</td>
#endforeach
</tr>
</table>
#endsection
As identified pro is not capitalised so you should correct as follows. You should also check that the array is not empty before you execute the page otherwise you can find yourself with more errors;
Also note that I've moved your 's inside the foreach as this is needed to make the table display properly.
#section('content')
<h1>Products</h1>
<table style="width:100%">
<tr>
<th>ID</th>
<th>Name</th>
</tr>
#if(! empty($pro))
#foreach($pro as $row)
<tr>
<td>{{$row['p_id']}}</td>
<td>{{$row['p_title']}}</td>
</tr>
#endforeach
#else
<tr>
<td colspan="2">No records</td>
</tr>
#endif
</table>
#endsection
Also, in your controller the view should read
return view('Folder.product', compact('pro'));
not
return view('Folder.product'), compact('pro'));
The capital, you need to use $pro not $Pro.
#foreach($pro as $row)
<td>{{$row['p_id']}}</td>
<td>{{$row['p_title']}}</td>
#endforeach
I hope it works.

Undefined variable: users (View: C:\xampp\htdocs\rentalmobil\resources\views\admin\user.blade.php)

SOLVED
i am confusing that i think i have already make a true code with laravel. i have seen on many references for me to know how to read database. yes i'm newbie and still learning. well here is my code
from model. admin.php is the name of folder:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Admin extends Model
{
protected $table = "admin";
}
here is my controller. my controller name is UserController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Admin;
class UserController extends Controller
{
public function users(){
$users = Admin::all();
return view('admin.user', ['admin' => $users]);
}
}
and here is my view, it is located in view/admin/ named user.blade.php:
#extends('admin.header')
#section('content')
<div class="col-md-12">
<div class="card">
<div class="header">
<h4 class="title">Striped Table</h4>
<p class="category">Here is a subtitle for this table</p>
</div>
<div class="content table-responsive table-full-width">
<table class="table table-striped">
<thead>
<th>ID</th>
<th>Name</th>
<th>Salary</th>
<th>Country</th>
<th>City</th>
</thead>
<tbody>
<tr>
<td>1</td>
<td>{{ $users->email }}</td>
<td>$36,738</td>
<td>Niger</td>
<td>Oud-Turnhout</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
#endsection
and here is my error
i call from my browser with this http://localhost:8000/users
sorry for bad English :(
#foreach($admin as $users)
<tbody>
<tr>
<td>1</td>
<td>{{ $users->email }}</td>
<td>$36,738</td>
<td>Niger</td>
<td>Oud-Turnhout</td>
</tr>
</tbody>
#endforeach
if u not need for each
<tbody>
<tr>
<td>1</td>
<td>{{ $admin[0]->email }}</td>
<td>$36,738</td>
<td>Niger</td>
<td>Oud-Turnhout</td>
</tr>
</tbody>
First, the variable will be called admin in the view, because that's how you passed it when you wrote 'admin' => $users.
Second, it's going to be a collection of users, so $users->email isn't going to work. You need to #foreach through each user.
Please update your view/admin/user.blade.php and UserController like:
UserController:
class UserController extends Controller
{
public function users(){
$users = Admin::all();
return view('admin.user', compact('users'));
}
}
view/admin/user.blade.php:
<tbody>
#foreach($users as $user)
<tr>
<td>1</td>
<td>{{ $user->email }}</td>
<td>$36,738</td>
<td>Niger</td>
<td>Oud-Turnhout</td>
</tr>
</tbody>

Categories