get function returning empty array laravel - php

I need some help getting my user_id from my pdf_files table, but for some reason it keep giving me an empty array when I try using $request = all() --> return me {} and dd($downloads) return me this Collection {#233 ▼ #items: [] }. Do I have to use with statement or anything? I saw some people using it but do I really need it? Thanks for helping
Code:
Controller:
public function downfunc(Request $request){
// $downloads = new pdfFile;
$downloads=pdfFile::where('user_id', $request->user_id)->get();
//$downloads=DB::table('pdf_files')->get(); --> this code return results
//dd($downloads);
//return $request->all();
return view('download.viewfile',compact('downloads'));
}
download.blade.php
<table class="table table-bordered">
<thead>
<th>Name of File</th>
<th>Action</th>
</thead>
<tbody>
#foreach($downloads as $down)
<tr>
<td>{{$down->file_name}}</td>
<td>
<a href="download/{{$down->file_name}}" download="{{$down->file_name}}">
<button type="button" class="btn btn-primary">
<i class="glyphicon glyphicon-download">
Download
</i>
</button>
</a>
</td>
</tr>
#endforeach
</tbody>
</table>
pdfFile model:
protected $fillable = array('file_name','file_size','user_id');
public function personal_infos() {
return $this->belongsTo('App\personal_info', 'user_id', 'id');
}

Please make sure your $request->user_id is valid
You can check by run sql manually.

Related

how to get data with relation in Laravel trying to get Bill Products to view

I have 3 models connected together Vendors has many Bills and bills has many bill_products
what do I need is to display the data with this relation in the index view here is my controller index function
public function index()
{
$vendor = $this->vendor();
$data['vendor_id'] = $vendor->id;
$data['bills'] = $vendor->bills()->get();
return view('vendors.bills.index', $data);
}
this bills relation in vendor model
public function bills()
{
return $this->hasMany(VendorBill::class);
}
Vendor bills model where I put my relation to bill products
class VendorBill extends Model
{
const FILLABLE = [
'making_cost',
'gram_price',
'customer_name',
'customer_id',
'vendor_id',
'id_no',
'phone',
'bill_no',
'total',
];
protected $fillable = self::FILLABLE;
public function vendor()
{
return $this->belongsTo(Vendor::class);
}
public function billProducts()
{
return $this->hasMany(VendorBillProduct::class);
}
}
and this is the last Model that belongs to Vendor Bills where i need to get data from
class VendorBillProduct extends Model
{
use SoftDeletes;
protected $table = "bill_products";
const FILLABLE = [
'vendor_bill_id',
'cate_code',
'product_description',
'metal_weight',
'total_metal_weight',
'caliber',
'gem_stone_weight',
'diamond_weight',
'purity',
'color',
'dimond',
];
protected $fillable = self::FILLABLE;
public function vendorBill()
{
return $this->belongsTo(VendorBill::class);
}
}
this is the index blade view where I want to get data stored in BillProducts the belongs to the VendorBill
<div class="table-responsive">
<table id="myTable" class="table table-striped ">
<thead>
<tr>
<th scope="col">#lang('vendors.bill_no')</th>
<th scope="col">#lang('vendors.customer')</th>
<th scope="col">#lang('vendors.phone')</th>
<th scope="col">#lang('vendors.cate_code')</th>
<th class="text-right" scope="col">#lang('vendors.action')</th>
</tr>
</thead>
<tbody>
#if($bills->count())
#foreach(#$bills as $bill)
<tr>
<th scope="row">{{#$bill->bill_no}}</th>
<th scope="row">{{#$bill->customer_name}}</th>
<td>{{#$bill->phone}}</td>
<td>{{#$bill->cate_code}}</td>
<td class="text-right">
<div class="dropdown">
<a href="#" class="btn btn-outline-light tn-sm" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<i class="fal fa-ellipsis-h" aria-hidden="true"></i>
</a>
<div class="dropdown-menu dropdown-menu-right">
<a href="{{route('vendors.bills.edit',$bill->id)}}" class="dropdown-item" type="button">
#lang('vendors.edit')
</a>
<a class="dropdown-item" href="{{route('vendors.bills.show',$bill->id)}}">#lang('vendors.details')</a>
{{--<button class="dropdown-item" type="button">
#lang('vendors.delete')
</button>--}}
</div>
</div>
</td>
</tr>
#endforeach
#endif
</tbody>
</table>
</div>
enter image description here
in this image, I highlighted the column that I need to get in relation
I think there is a clever solution within your problem. Create a method on your VendorBill.php model, that concats the codes.
class VendorBill
{
function concatCodes(): string
{
return $this->billProducts->pluck('cate_code')->implode(', ');
}
}
Now in the context of a bill, you can convert all its vendor bills into that string and then implode that.
$bill->map->concatCodes()->implode(', ');
This will then concat the codes into the a string similar to this
code1, code2, code3

Laravel get All with Relationships and Paginate relationship data

I am trying to get all users and their associated (1:n) journals. However I want to add pagination to the associated journals, not the users
My Controller:
public function index()
{
$users = User::with(['journal'])->orderBy('name', 'asc')->get();
return view('journals/journals', ['users' => $users]);
}
My Blade:
#foreach($users as $user)
<a class="list-group-item list-group-item-action" data-toggle="collapse" href="#collapse{{$user->id}}" role="button" aria-expanded="false" aria-controls="collapse{{$user->id}}">
{{$user->name}}
</a>
<div class="collapse mt-1" id="collapse{{$user->id}}">
<div class="card card-body">
<div class="list-group">
<table class="table table-sm table-hover">
<thead>
<tr>
<th scope="col">Kunde</th>
<th scope="col">Betreff</th>
<th scope="col">Leistungsart</th>
<th scope="col">Beginn</th>
<th scope="col">Ende</th>
<th scope="col">Dauer</th>
<th scope="col">Arbeitszeit</th>
</tr>
</thead>
<tbody>
#foreach($user->journal as $journal)
<tr onclick="window.location='{{route('journal.show', [$journal->id])}}'">
<th scope="row">{{$journal->customer->name}}</th>
<td>{{$journal->title}}</td>
<td>{{$journal->type}}</td>
<td>{{$journal->started}}</td>
<td>{{$journal->ended}}</td>
<td>{{$journal->duration}}</td>
<td>{{$journal->worked}}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
#endforeach
My Journal Model:
public function user(){
return $this->belongsTo('App\User', 'user_id', 'id');
}
My User Model:
public function journal(){
return $this->hasMany('App\Journal', 'user_id', 'id');
}
Again, what I am trying to achieve is that I can print out every user and paginate their journals, not paginate the users
I really hope someone can help me there
Render in blade
Above your journals for-each loop in your blade, try and put something like this:
#php
$paginated_journals = $user->journal()->paginate(10);
#endphp
And then, your for-each loop should look like this:
#foreach($paginated_journals as $journal)
...
#endforeach
After the for-each loop you can just put:
$paginated_journals->links()
to get the links for pagination
Pre-load data in controller
You can do the same thing server-side. Create a custom array that is empty, go through each user, and add sub array to that custom array:
array_push($custom_array, ['user' => $user, 'journals' => $user->journal()->paginate(10)])
This way you can send the custom array to your blade, loop through it, and render user data and paginated journals.

How Export only specific columns from Laravel Blade table view to Ecxel file?

So I have a table on blade view file, I have successfully exported it into the excel file. Now I want to export without exporting the 'Actions" column. How can I do that?
I have attached my code and blade file below, it works perfectly. I just want to export everything except the Actions Column as it contains Buttons for Database Operations
THIS IS MY EXPORT CLASS CODE:
public function view(): View
{
return view ('ims.view_inventory_history_table', [
'data' => inbound_history::all()
]);
}
public function headings(): array
{
return [
'ID',
'Warehouse',
'SKU',
'Child SKU',
'Units',
'Invoice No.',
'Container No.',
'Entry Date'
];}
/**
* #return array
*/
public function registerEvents(): array
{
return [
AfterSheet::class => function(AfterSheet $event) {
$cellRange = 'A1:W1'; // All headers
$event->sheet->getDelegate()->getStyle($cellRange)->getFont()->setSize(14);
},
];
}}
THIS IS MY CONTROLLER FUNCTION:
public function export_view()
{
return Excel::download(new inboundHistory(), 'inboundHistory.xlsx');
}
THIS IS MY ROUTE:
Route::Get('inbound_history/export_view' ,'imsController#export_view')->name('inbound_history.export_view');
THIS IS MY BLADE TABLE VIEW:
<table id="datatables" class="table table-striped table-no-bordered table-hover" cellspacing="0" width="100%" style="width:100%">
<thead>
<tr>
<th style="width:5%">ID</th>
<th>Warehouse</th>
<th>SKU</th>
<th>Child SKU</th>
<th>Cases</th>
<th>Units</th>
<th>Invoice No.</th>
<th>Container No.</th>
<th>Entry Date</th>
<th class="disabled-sorting text-right" style="width:12%">Actions</th>
</tr>
</thead>
<tbody>
#foreach ($data as $row)
<tr>
<td>{{$row['id']}}</td>
<td>{{$row['warehouse']}}</td>
<td>{{$row['sku_parent']}}</td>
<td>{{$row['sku_child']}}</td>
<td>{{$row['total_cases']}}</td>
<td>{{$row['total_units']}}</td>
<td>{{$row['invoice_no']}}</td>
<td>{{$row['container_no']}}</td>
<td>{{$row['date_rec']}}</td>
<td class="td-actions text-right">
{{-- <a rel="tooltip" class="btn btn-success btn-link" href="{{action('imsController#edit',$row['id'])}}">
<i class="material-icons">edit</i></a> --}}
<a rel="tooltip" class="btn btn-danger btn-link" href="{{action('imsController#destroy',$row['id'])}}" onclick = "if (! confirm('Confirm: Press OK to delete the Entry.')) { return false; }"style="color: red;">
<i class="material-icons">close</i></a>
</td>
</tr>
#endforeach
</tbody>
I don't know if I got everything correctly, but why don't you do something like this:
Pass an additional variable to your blade template like $isView, when you want to create a view for the user.
And in your blade.php template you do something like this:
#isset($isView)
<th class="disabled-sorting text-right" style="width:12%">Actions</th>
#endisset
// do the same #isset test for the corresponding <td> element
When you want to render it to excel you just don't pass this variable and the column is not rendered.

Laravel route problem redirects 404 error

I have a problem, I made a support system, and when I want to enter the page where I watch the ticket, I will get 404 not found. Basically the route is that of the id in the database.
Routes:
Route::get('/viewTickets', 'TicketController#view_MyTickets')->name('viewTickets');
Route::get('/viewTickets/{ticket}', 'TicketController#view_MyTicketUpdate')->name('updateTicket');
Route::post('/viewTickets/{ticket}', 'TicketController#update_MyTicket');
Controller:
public function view_MyTickets() {
$tickets = Ticket::latest()->get();
return view('viewTickets', compact('tickets'));
}
public function view_MyTicketUpdate() {
$tickets = Ticket::latest()->get();
return view('updateTicket', compact('tickets'));
}
View:
<tbody>
#foreach($tickets as $ticket)
<tr>
<td>{{$ticket->id}}</td>
<td>{{$ticket->user_id}}</td>
<td>{{$ticket->title}}</td>
<td>{{$ticket->category}}</td>
<td>{{$ticket->status}}</td>
<td>
<form>
<i class="material-icons"></i>
<i class="material-icons"></i>
</td>
</form>
</tr>
#endforeach
</tbody>
I really don't understand the problem, a way to solve and did not receive error 404? Btw, I read the other topics and I couldn't find a solution.
The link you are setting in the href attribute is only the ticket id, but it should be, for example, "/viewTickets/1"
Try to put this:
href="{{route('updateTicket', ['ticket' => $ticket->id])}}"
In your route :
Route::get('/viewTickets', 'TicketController#view_MyTickets')->name('viewTickets');
Route::get('/viewTickets/{ticket}', 'TicketController#view_MyTicketUpdate')->name('updateTicket');
Route::post('/viewTickets/{ticket}', 'TicketController#update_MyTicket');
In your Controller :
public function view_MyTickets() {
$tickets = Ticket::latest()->get();
return view('viewTickets', compact('tickets'));
}
public function view_MyTicketUpdate(Request $request, $ticket) {
$tickets = Ticket::find($ticket);
return view('updateTicket', compact('tickets'));
}
In your Blade :
<tbody>
#foreach($tickets as $ticket)
<tr>
<td>{{$ticket->id}}</td>
<td>{{$ticket->user_id}}</td>
<td>{{$ticket->title}}</td>
<td>{{$ticket->category}}</td>
<td>{{$ticket->status}}</td>
<td>
<i class="material-icons"></i>
<i class="material-icons"></i>
</td>
</tr>
#endforeach
</tbody>

How to update record 'pay' laravel?

I'm trying to update my 'pay' record in the DB after pressing a button in a table row, but it shows this error when pressing the button:
ErrorException in 0db55b8fee884912074e1a4700061051aeb18bcc.php line
15: Undefined variable: users (View:
/Users/mauricio/code/cnr/resources/views/pages/users.blade.php)
I don't know why it gives me that error when I have use the users variable before in the blade file.
Here is my Registration Controller:
<?php
namespace App\Http\Controllers;
use DB;
use App\User;
class RegistrationController extends Controller
{
public function updatePay(User $id)
{
DB::table('users')->where('id', $id)->update(['pay' => 1]);
return view('pages.users');
}
}
Welcome controller:
public function usersAdmin()
{
$users = DB::table('users')->get();
return view('pages.users', compact('users'));
}
Routes file:
// for admin only
Route::get('/users', 'WelcomeController#usersAdmin');
Route::post('/users/{id}', 'RegistrationController#updatePay');
Here is my view
#extends('layouts.master')
#section('content')
<table class="highlight">
<thead>
<tr>
<th data-field="id" hidden="">ID</th>
<th data-field="name">Nombre</th>
<th data-field="last_name">Apellidos</th>
<th style="text-align: right;">Acciones</th>
</tr>
</thead>
<tbody>
#foreach($users as $user)
<tr>
<td>{{$user->id}}</td>
<td>{{$user->name}}</td>
<td>{{$user->last_name}}</td>
<td style="text-align: right; width: 30em">
#if( $user->isAdmin )
{{"ADMINISTRADOR"}}
#elseif( $user->pay )
<a class="waves-effect waves-light btn yellow darken-2"><i class="material-icons left" style="margin:0">create</i></a>
<a class="waves-effect waves-light btn red darken-1"><i class="material-icons left" style="margin:0">delete</i></a>
#else
<form method="POST" action="/users/{{$user->id}}">
{{ csrf_field() }}
<button type="submit" class="waves-effect waves-light btn green lighten-1"><i class="material-icons left" style="margin:0">done</i></button>
</form>
#endif
</td>
</tr>
#endforeach
</tbody>
</table>
#endsection
You need to retrieve users and send it to the view similar like this:
Controllers:
public function methodName()
{
$users = User::all();
return view('path.to.view')->with(compact('users'));
}
Also, You have two choice to use:
First
public function updatePay($id)
Second
public function updatePay(User $user)
{
DB::table('users')->where('id', $user->id)->update(['pay' => 1]);
and instead of return view('pages.users'); use return redirect()->url('users'); in the updatePay method.

Categories