Laravel: Automatic populate from database - php

First Laravel Project. I want to populate a table automiticaly from database
The table looks like this:
{{ Form::open(array('url' => '/invoicenew/$invoicenum')) }}
<table border=1 align=center>
<t>
<td colspan=3>
<table>
<tr>
<td>ACME INC.</td>
</tr>
<tr>
<td>Post code, City</td>
</tr>
<tr>
<td>Address</td>
</tr>
<tr>
<td>Tax number</td>
</tr>
</table>
</td>
<td colspan=3>
<table>
<tr>
<td>{{ $supplier[0]->name }}</td>
</tr>
<tr>
<td>{{ $supplier[0]->postcode}} {{ $supplier[0]->city}}</td>
</tr>
<tr>
<td>{{ $supplier[0]->address }}</td>
</tr>
<tr>
<td>{{ $supplier[0]->taxnumber }}</td>
</tr>
</table>
</tr>
<tr>
<td colspan=2>Invoice number:</td><td>{{ $invoicenum }}</td>
<td colspan=2>Invoice date:</td><td>{{ $invoicedate }}</td>
</tr>
<tr>
<td>Barcode</td><td>Name</td><td>Count</td><td>Netto Price</td><td>Brutto Price</td><td>VAT kex</td>
</tr>
<tr>
<td>{{Form::text('barcode')}}</td><td></td>{{Form::text('count')<td></td><td></td><td></td><td></td>
</tr>
<tr colspan=6>{{Form::submit('Next>>')}}</tr>
</table>
I want to fill the Name, Netto Price, Brutto Price and VAT key from table inventory instantly after I write the barcode in the field.
How can I accomplish it?

Use the form model binding feature.
Often, you will want to populate a form based on the contents of a model. To do so, use the Form::model method:
{!! Form::model($user, ['route' => ['user.update', $user->id]]) !!}
Now, when you generate a form element, like a text input, the model's value matching the field's name will automatically be set as the field value. So, for example, for a text input named email, the user model's email attribute would be set as the value. However, there's more! If there is an item in the Session flash data matching the input name, that will take precedence over the model's value.

Related

Nested foreach loop creating duplicate records with if condiotion - PHP Laravel 8

Problem: There are modules, users, and user_modules tables, where the admin can assign multiple modules with permissions to a user. Admin can update module's permission which is already assigned to that user, and the modules which are not assigned should be loaded on blade view on the same table.
But the issue is data is duplicating
I am posting my code with images
AdminController:
$modules = Module::all();
$user_modules = User_module::with('module')->where('user_id', $user_id)->get();
return view('admin/seller_assign_modules', compact('user','modules','user_modules'));
seller_assign_modules.blade.php
<table class="table table-striped">
<thead>
<tr>
<th>Modules</th>
<th>Add</th>
<th>Edit</th>
<th>View</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
#foreach ($user_modules as $user_mod)
#foreach ($modules as $mod)
#if ($mod->id == $user_mod->module_id)
<tr>
<td scope="row">{{$user_mod->module->name}}</td>
<td scope="row">{{$user_mod->add}}</td>
<td scope="row">{{$user_mod->edit}}</td>
<td scope="row">{{$user_mod->view}}</td>
<td scope="row">{{$user_mod->del}}</td>
</tr>
#else
<tr>
<td scope="row">{{$mod->name}}</td>
<td scope="row"></td>
<td scope="row"></td>
<td scope="row"></td>
<td scope="row"></td>
</tr>
#endif
#endforeach
#endforeach
</tbody>
</table>
modules table:
user_modules table:
result on seller_assign_modules.blade.php
I NEED THIS:
You can do something like below (this is not a good approach however)
$user_modules = User_module::with('module')->where('user_id', $user_id)->get();
//Get modules where user dont have records
$non_user_modules = Module::whereNotIn('id', (clone $user_modules)->pluck('module_id')->toArray())->get();
return view('admin/seller_assign_modules', compact('user','modules','user_modules','non_user_modules'));
Then in the blade
#foreach ($user_modules as $user_mod)
<tr>
<td scope="row">{{$user_mod->module->name}}</td>
<td scope="row">{{$user_mod->add}}</td>
<td scope="row">{{$user_mod->edit}}</td>
<td scope="row">{{$user_mod->view}}</td>
<td scope="row">{{$user_mod->del}}</td>
</tr>
#endforeach
#foreach ($non_user_modules as $non_user_module)
<tr>
<td scope="row">{{$non_user_module->name}}</td>
<td scope="row"></td>
<td scope="row"></td>
<td scope="row"></td>
<td scope="row"></td>
</tr>
#endforeach
Best approach you could do
Make a relationship at Module model like userModules()
Then get Modules like below
$modulesWithUser = Module::with(['userModules' => function($userQuery){
$userQuery->where('user_id',$user_id);
}])->get();
Then loop $modulesWithUser in blade and access its relationship inside loop to show users permissions.
The problem with here is,Module has manyUser_modules, but you only need one.
So in this case, one approach is map a new property like user_module to each Module.
AdminController
$modules = Module::all();
$userModeules = User_module::where('user_id', $user->id)->get();
$modules = $modules->map(function($module) {
$module->user_module = $userModules->firstWhere('module_id', $module->id);
return $module;
});
return view(
'admin/seller_assign_modules',
compact('modules')
);
seller_assign_modules.blade.php
<tbody>
#foreach($mdules as $module)
<tr>
<td scope="row">{{ $module->name }}</td>
<td scope="row">{{ optional($module->user_module)->add }}</td>
<td scope="row">{{ optional($module->user_module)->edit }}</td>
<td scope="row">{{ optional($module->user_module)->view }}</td>
<td scope="row">{{ optional($module->user_module)->del }}</td>
</tr>
#endforeach
</tbody>

How to get total / sum before first row in php

Please help me, I need output total on top of data shown...
I have Looping like this in my blade.php
<table>
<thead>
<tr>
<th>Name</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
#foreach($datas $data)
<tr>
<td>{{ $data->name }}</td>
<td>{{ $data->amount }}</td>
</tr>
#endforeach
</tbody>
and output will like this:
And now how to get output total before firs row like image below?
The laravel collection has an inbuilt function called sum which returns the sum of Collection.
Learn here about collection function
<table>
<thead>
<tr>
<th>Name</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>Total Pencil</td>
<td>{{ $datas->sum('amount') }}</td>
</tr>
#foreach($datas $data)
<tr>
<td>{{ $data->name }}</td>
<td>{{ $data->amount }}</td>
</tr>
#endforeach
</tbody>
convert object to array
$datasArray = json_decode(json_encode($datas), true);
collect all 'amount' in an array
$amountArray = array_column($datasArray,'amount');
Sum all element of $amountArray
$total = array_sum($amountArray );
print $total where you want

my pagination laravel is weird( i dont know to tell in english my english is suck)

so I recently learn how to use Laravel for my class project, and heres my problem
when I create pagination the result in my project is <
.1
>
like that, the pagination is not styled and the pagination like the picture I upload, I have done everything I could do , I browse the internet too, and the result is null , thanks heres the picture of my problem
$no = 1 {{-- buat nomor urut --}}
)
#if ($forms->count() > 0)
#foreach ($forms as $angs)
<tr align="center">
<td height="32">{{ $no++ }}<div align="center"></div></td>
<td>{{ $angs->username }}<div align="center"></div></td>
<td>{{ $angs->nama }}<div align="center"></div></td>
<td>{{ $angs->nik}}<div align="center"></div></td>
<td>{{ $angs->status_aktif}}<div align="center"></div></td>
<td bgcolor="#EEF2F7"><div align="center">Detail | Edit | Hapus</div></td>
<tr align="center" bgcolor="#DFE6EF">
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</tr>
#endforeach
{{ $forms->links()}}
`
this is my code in blade
and this is my code in my controller
`
$forms= login::paginate(3);
return view('crud.form.viewMember', compact('forms'));
`

How to set 'data is not found' in the html table if the data in the database is null?

I am using Laravel 5 and I have a page which have some tables. My problem is, when there is no data in database, it shows nothing. I want it show 'Data is not found'. This first picture below is when the data is available in the database, and the second picture is when the data is unavailable in the database.
1.
2.
This is my code:
#if($candidateskill != '')
#foreach($candidateskill as $getData)
<tr>
<td>{{ $getData->SKILL_NAME }}</td>
<td align="center">{{ $getData->SKILL_LEVEL }}</td>
</tr>
#endforeach
#else
<td>Data is Not Found</td>
<td>Data is Not Found</td>
#endif
And this is the output of the null data:
Illuminate\Database\Eloquent\Collection Object ( [items:protected] =>
Array ( ) )
You can use the #forelse (https://laravel.com/docs/5.6/blade#loops)
#forelse($candidateskill as $getData)
<tr>
<td>{{ $getData->SKILL_NAME }}</td>
<td align="center">{{ $getData->SKILL_LEVEL }}</td>
</tr>
#empty
<tr><td colspan="2">No Data</td></tr>
#endforelse
Try this:
#if($candidateskill && !$candidateskill->isEmpty())
#foreach($candidateskill as $getData)
<tr>
<td>{{ $getData->SKILL_NAME }}</td>
<td align="center">{{ $getData->SKILL_LEVEL }}</td>
</tr>
#endforeach
#else
<tr>
<td>Data is Not Found</td>
</tr>
#endif
You need to evaluate your database variable if it exists, and if its not empty aka null.

Create HTML table with headers in a vertical column dynamically with laravel?

I've been hunting around, but all the answers and documentation on this is for tables that aren't dynamically generating everything.
This is my blade file:
#extends('layout')
#section('content')
<h1>User Profile Data</h1>
<table class="table table-striped">
<tr>
#foreach ($columns as $column => $name)
<th><strong> {{ $name }} </strong></th>
#endforeach
#foreach ($profile as $person)
#foreach ($person as $name)
<td> {{ $name }} </td>
#endforeach
#endforeach
</tr>
</table>
#stop
It's all working, except for one problem, the heads go horizontally, rather than vertically. I'd like to be able to automatically populate the data from the database and have it displayed side by side.
Like so:
Name: John
Age: 25
Height: 176
etc.
What am I missing?
v.1 Directly generate the vertical headers table.
This can be easily solved after you realize how your table will actually look like in HTML, in order to have vertical headers.
<h1>User Profile Data</h1>
<table class="table table-striped">
<tr>
<th><strong> Name: </strong></th>
<td> John </td>
<td> Joane </td>
</tr>
<tr>
<th><strong> Surname: </strong></th>
<td> Doe </td>
<td> Donald </td>
</tr>
<tr>
<th><strong> Email: </strong></th>
<td> john.doe#email.com </td>
<td> jane#email.com </td>
</tr>
</table>
After you know your above goal you just have to nest the foreach loops so that you can populate the data.
For each column you'll have to return the matching data from the profile.
Instead of returning $person->name use $person[$col]
To nest the foreach use the same technique for your other question:
How to show data from (multidimensional?) arrays with Laravel 5.3 blade
v.2 Use normal tables combined with CSS tricks to reflow table
Import twitter bootstrap and use .table-reflow on a well formed table.
The generated table will look like this and can be generated with your initial laravel code:
<table class="table table-striped table-hover table-reflow">
<thead>
<tr>
<th ><strong> Name: </strong></th>
<th ><strong> Surname: </strong></th>
<th ><strong> Email: </strong></th>
</tr>
</thead>
<tbody>
<tr>
<td> John </td>
<td> Doe </td>
<td> john.doe#email.com </td>
</tr>
<tr>
<td> Joane </td>
<td> Donald </td>
<td> jane#email.com </td>
</tr>
</tbody>
</table>
Content order and complex tables:
Beware that the table-reflow style changes the visual order of content. Make sure that you only apply this style to well-formed and simple data tables (and in particular, don’t use this for layout tables) with appropriate <th> table header cells for each row and column.
In addition, this class will not work correctly for tables with cells that span multiple rows or columns (using rowspan or colspan attributes).
You should check the docs here:
twitter-bootstrap .table-reflow
Edit:
In addition to Kronmark's excellent points, I'm leaving this here for new people. This is what the automated table might look like:
<table class="table table-striped table-hover table-reflow">
#foreach($array as $key=>$value)
<tr>
<th ><strong> {{ $key }}: </strong></th>
<td> {{ $value }} </td>
</tr>
#endforeach
</table>
The key is the title from the table, the value is the data point associated with it. I.e. Full_Name = Key, John Magsson = value.
This is produced from an array, the process should be explained here:
Blade view not printing data from array
Good luck. :)

Categories