ErrorException Undefined variable in Laravel - laravel-7

I am new PHP.I am starting Laravel. Please kindly help. I cannot find why ErrorException Undefined variable.
ProductController
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use app\Models\Product;
use DB;
class ProductController extends Controller
{
public function index(){
$products = DB::table('products')->get();
return view('product.index',compact('products'));
}
}
This my View
<tr>
#foreach($products as $pro)
<td>{{ $pro->product_name }}</td>
<td>000012</td>
<td>It is the product of France.</td>
<td>image.png</td>
<td>
Details
Edit
Delete
</td>
#endforeach
</tr>

Related

How to create drop down list in excel using laravel-excel or phpspreadsheet?

I am using laravel-excel 3.0 in my laravel project and i am unable to create drop-down list while downloading the list.
Below is Export class code
class MasterFields implements FromView
{
public function view(): View
{
return view('master-fields', [
'master_fields' => [
[
'name' => 'Povilas',
'surname' => 'Korop',
'email' => 'povilas#laraveldaily.com',
'twitter' => ['#povilaskorop', 'test', 'test']
],
[
'name' => 'Taylor',
'surname' => 'Otwell',
'email' => 'taylor#laravel.com',
'twitter' => ['#povilaskorop', 'test', 'test']
]
]
// MasterDataField::get_master_data_set()
]);
}
}
Below is blade code
<table>
<thead>
<tr>
<th style="color:blue;"><b> ID </b></th>
<th style="color:blue;"><b> Field Name </b></th>
<th style="color:blue;"><b> Data Type </b></th>
</tr>
</thead>
<tbody>
#foreach($master_fields as $field)
<tr>
<td>{{ $field['name'] }}</td>
<td>{{ $field['surname'] }}</td>
<td>
<select>
#foreach($field['twitter'] as $twitter)
<option> {{ $twitter }} </option>
#endforeach
</select>
</td>
</tr>
#endforeach
</tbody>
I am using below method to download
return Excel::download(new MasterFields, 'test.xlsx');
As you mentioned the old way of definition doesn't work. But here is an example of the new way: https://laraveldaily.com/laravel-excel-3-0-export-custom-array-excel/

How to use search filter in Angular for nested JSON object?

I am bringing data from my mongoose to display on my HTML table. I am able to filter a JSON object normally but unable to filter nested JSON object?
I have used "npm i ng2-search-filter --save " to import filter pipe, but this only works for the first level. Doesn't do filter for the nested JSON object.
My JSON object is:
{
packageName:test,
packagePrice:200,
userid:{
userName:John,
userAge: 27
}
}
<input type="text" class="form-control" [(ngModel)]="searchtext"
placeholder="Search">
<table class="table table-hover">
<thead>
<tr>
<th scope="col">Package Name</th>
<th scope="col">Package Price</th>
<th scope="col">Customer Name</th>
<th scope="col">Customer Age</th>
</tr>
</thead>
<tbody *ngFor="let use of user | filter:searchtext">
<tr class="table-active">
<td> {{use.packageName}}</td>
<td>{{use.packagePrice}}</td>
<td>{{use.userid.userName}}</td>
<td>{{use.userid.userAge}}</td>
</tr>
</tbody>
</table>
When I enter packageName and packagPrice in a textbox for search filter it filters out and shows me correct result, but on userName and userAge its not working.
Please help.
Thank you.
I solved this with by just installing the latest version ng2 search filter.
Run below command:
npm i ng2-search-filter#0.5.1
You can use custom search pipe for this
I have create demo on Stackblitz
searchPipe.ts
transform(value: any, searchText?: any): any {
if(!value) return [];
if(!searchText) return value;
searchText = searchText.toLowerCase();
return value.filter( it => {
return it.packageName.toLowerCase().includes(searchText) || it.packagePrice.toString().includes(searchText) || (it.userid && it.userid.userAge.toString().includes(searchText) || it.userid.userName.toLowerCase().includes(searchText));
});
}
component.html
<tr class="table-active" *ngFor="let use of user | search:searchtext">
<td> {{use.packageName}}</td>
<td>{{use.packagePrice}}</td>
<td>{{use.userid.userName}}</td>
<td>{{use.userid.userAge}}</td>
</tr>
Try the below:-
function getResult(filterBy, objList) {
return objList.hightlights.filter(function(obj) {
return obj.queries.some(function(item){
return item.indexOf(filterBy) >= 0;
});
});
}

Exporting from blade view to Excel with font size and styling

I'm Using Maatwebsite/Excel to export my Blade view to excel but when I'm exporting it to excel the font size won't take effect and it won't bring it on center.
If it's not possible in maatwebiste/excel is there any other reporting service like RDLC reporting for laravel
My view
<html>
<head>
<title>asdf</title>
</head>
<body>
<table border="1" cellpadding="0px" cellspacing="0px" width="100%">
<thead>
<tr>
<td width="100%" colspan="12" align="center" style="text-align:
center;font-size:50pt">Should be large and in center</td>
</tr>
<tr>
<td>id</td>
<td>quantity</td>
<td>units_id</td>
<td>description</td>
<td>requster</td>
<td>checker</td>
<td>approver</td>
<td>created_at</td>
<td>updated_at</td>
<td>remarks</td>
<td>request_number</td>
<td>steps</td>
</tr>
</thead>
<tbody>
#foreach($posts as $post)
<tr>
<td>{{$post->id}}</td>
<td>{{$post->quantity}}</td>
<td>{{$post->units_id}}</td>
<td>{{$post->description}}</td>
<td>{{$post->requster}}</td>
<td>{{$post->checker}}</td>
<td>{{$post->approver}}</td>
<td>{{$post->created_at}}</td>
<td>{{$post->updated_at}}</td>
<td>{{$post->remarks}}</td>
<td>{{$post->request_number}}</td>
<td>{{$post->steps}}</td>
</tr>
#endforeach
</tbody>
</table>
</body>
</html>
My Controller
public function export()
{
return Excel::download(new UsersExport, 'posts.xlsx');
}
And my Export Controller
class UsersExport implements FromView
{
public function view(): View
{
return view('exporting', [
'posts' => Procurment_request::all()
]);
}
}
My View
My Exported Excel file
Thanks in advance
You need to configure it your self.
Take a look at the documentation on how to format Columns.
class InvoicesExport implements WithColumnFormatting, WithMapping
{
public function map($invoice): array
{
return [
$invoice->invoice_number,
Date::dateTimeToExcel($invoice->created_at),
$invoice->total
];
}
/**
* #return array
*/
public function columnFormats(): array
{
return [
'B' => NumberFormat::FORMAT_DATE_DDMMYYYY,
'C' => NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE,
];
}
}

How to export (oracle) database table to excel sheet using laravel

I am trying to export a (query structured) table (called formal) to excel using laravel. So far I can show table in browser as html using this blade. I want when user presses Export to excel button to export to excel (my blade is below):
<!DOCTYPE html>
<html lang="el">
<head>
<meta charset="UTF-8">
<title>Studies</title>
</head>
<body>
<b>Schedule: {{ $key }}</b>
<br>
<table border="1">
<tr>
<th>Lesson Category</th>
<th>Lesson Code</th>
<th>Lesson Title</th>
<th>Lesson Department</th>
/* extra columns
....
*/
<th>Audience</th>
</tr>
#foreach($star as $v2)
<tr>
<th>{{ $v2->lesson_category }}</th>
<td>{{ $v2->lesson_code }}</td>
<td>{{ $v2->lesson_title }}</td>
<td>{{ $v2->lesson_dep }}</td>
/* extra columns
....
*/
<td>{{ $v2->audience}}</td>
</tr>
#endforeach
</table>
<br>
<form action='/programma'>
<input type="submit" value="New Search" />
</form>
<br>
<form action='/export'>
<input type="submit" value="Export to excel" /> //I want this button to
//export to excel
</form>
</body>
</html>
I have followed this guide with no success.
https://docs.laravel-excel.com/3.1/getting-started/installation.html
https://docs.laravel-excel.com/3.1/exports/
The maatwebsite/excel has been installed successfully. But no excel is downloaded.
The problem is solved. I did it to export to excel file.
Below is my solution so far. Thank you all!
1) web.php
Route::get('/exagogi','UsersController#export');
2) UsersController.php
<?php namespace App\Http\Controllers;
use App\Exports\UsersExport;
use Maatwebsite\Excel\Facades\Excel;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
class UsersController extends Controller
{
public function export()
{
return Excel::download(new UsersExport, 'result.xlsx');
}
}
3) UsersExport.php
<?php
namespace App\Exports;
use App\User;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
class UsersExport implements FromCollection, WithHeadings
{
/**
* #return \Illuminate\Support\Collection
*/
public function collection()
{
return User::all();
}
public function headings(): array
{
return [
'Lesson Category',
'Lesson Title',
'Lesson Department'
// etc
];
}
}

Pass Id to ActionMethod

I have a Delete button inside my table and I m trying to delete the selected Row.
The problem is that I always get in the post method a null ID
<div>
<table class="table">
<thead>
<tr>
<th>Role Name</th>
<th>Action</th>
</tr>
</thead>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#using (Html.BeginForm("Delete","Role", new { id = item.Id },FormMethod.Post))
{
<div class="form-actions no-color">
<input type="submit" value="Delete" class="btn btn-default" /> |
</div>
}
</td>
</tr>
}
</table>
In My Controller
// POST: Jobs/Delete/5
[HttpPost, ActionName("Delete")]
public ActionResult Delete(int id)
{
IdentityRole role = _context.Roles.Find(id);
_context.Roles.Remove(role);
_context.SaveChanges();
return RedirectToAction("Index");
}
Any time I click on the button the id is null
From your comments, the html generated in <form> tags is
action="/Role/Delete/b1bc13ca-a855-48b0-90e2-9e5fc081ac86"
meaning that the Id property of your model is typeof Guid. Change the POST method signature to
public ActionResult Delete(Guid id)

Resources