I try to create a table that containt checkbox using laravel blade .
But checkbox does not appear in the table.
this is my code:
#section('content')
<div class="container">
<table class="table table-striped">
<thead>
<tr>
<th>Survey</th>
<th>Date</th>
<th>Published</th>
<th>Creator</th>
<th>seen</th>
</tr>
</thead>
<tbody>
#foreach($surveys as $survey)
<tr {!! !$survey->seen && session('statut') == 'admin'? 'class="warning"' : '' !!}>
<td>{{$survey->title}}</td>
<td>{{$survey->created_at->todatestring()}}</td>
<td>{!! Form::checkbox('active', $survey->id, $survey->active) !!}</td>
#foreach($users as $user)
#if($user->id == $survey->user_id)
<td>
{{$user->name}}
</td>
#endif
#endforeach
<td>{!! Form::checkbox('seen', $survey->id, $survey->seen) !!}</td>
<td><button type="button" class="btn btn-info">Detail</button></td>
</tr>
#endforeach
</tbody>
</table>
</div>
#endsection
help me please!thank you in advance
Please use in this way.
{{ Form::checkbox('agree', 'yes') }}
Refer to this URL
I've tried your code and the checkbox works fine without problem as long as you already done all of this things correctly.
Install & setup Laravel Collective (https://laravelcollective.com/docs/master/html)
$surveys already sent from your controller with its data
$survey->seen contains boolean value (true/false)
Related
I used to call functions from the template but I learned recently that it is not a good idea since it renders time. I tried to assign value to variable and subscribe it to change using setting service observables but it does not work for me.
public getMaxUnit(){
return MaxUnits.Dollars;
}
<div style="display: flex;">
<table style="width:100%;">
<thead>
<tr>
<th>Max</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let maxValue of maxValueTable">
<td> {{ maxValue | numberFormatter: getMaxUnit()}}</td>
</tr>
</tbody>
</table>
</div>
How can I assign please the value to a variable using .subscribe() method?
You can use observables in your html page with async pipe.
In your code, your pipe takes an observable parameter.
To achieve this you need to wrap your getMaxUnit with async pipe.
Here is a similar example you can check and you can change getDateFormat function to how its date is changing.
public getMaxUnit():Observable<string>{
return of(MaxUnits.Dollars);
}
<div style="display: flex;">
<table style="width:100%;">
<thead>
<tr>
<th>Max</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let maxValue of maxValueTable">
<td> {{ maxValue | numberFormatter: (getMaxUnit() | async)}}</td>
</tr>
</tbody>
</table>
</div>
In my table, I'm attempting to add a read more button to each row that has a paragraph. The button is displayed for each row, and when I click one, it operates concurrently for each row.
Here is my code:
<table class="table">
<thead>
<tr>
<th scope="col">Text</th>
<th scope="col">Date</th>
<th scope="col">Comments</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let p of posts[0].posts">
<div [ngClass]="{'limitTextHeight': isReadMore}">
<td>{{p.post_text}}</td>
</div>
<button type="button" (click)="showText()" style="margin-top:15px">
{{ isReadMore ? 'Read More': 'Read Less' }}
</button>
<td>{{p.post_date}}</td>
<td>{{p.post_comments.length}} comment</td>
</tr>
</tbody>
</table>
and my showText() function:
showText() {
this.isReadMore = !this.isReadMore
}
This is happening because you're using single boolean variable i.e, isReadMore for all the looped tr tag.
What you can do is posts[0].posts map this posts with one more key that can be anything for example isReadMore in your case, then you will get unique instance to handle for each para, like this.
{{ p?.isReadMore ? 'Read More': 'Read Less' }}.
Hope you understood what I'm trying to say.
You are using a common variable for all the rows. That's why when you click one they all change. You should use local ones, one for each row like so:
<table class="table">
<thead>
<tr>
<th scope="col">Text</th>
<th scope="col">Date</th>
<th scope="col">Comments</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let p of posts[0].posts">
<div [ngClass]="{'limitTextHeight': isReadMore}">
<td>{{p.post_text}}</td>
</div>
<button type="button" (click)="showText(p)" style="margin-top:15px">
{{ p.isReadMore ? 'Read More': 'Read Less' }}
</button>
<td>{{p.post_date}}</td>
<td>{{p.post_comments.length}} comment</td>
</tr>
</tbody>
</table>
and the showText() function:
showText(post) {
post.isReadMore = !post.isReadMore
}
You could also add an *ngIf to check if there is text long enough before showing the button:
<button type="button" *ngIf="p.post_text?.length > 20" (click)="showText(p)" style="margin-top:15px">
{{ p.isReadMore ? 'Read More': 'Read Less' }}
</button>
i want to update data "barang" using pop up.
before the pop up goes down completely, the page redirect http://localhost/koperasi/index.php/cukl/undefined & show
" The page you requested was not found" error.
....
this is my controller
public function ubahbarang(){
if ($this->input->post('ubahdatabarang')) {
$id_barang=$this->input->post('id_barang');
$id_jenis=$this->input->post('id_jenis');
$nm_barang=$this->input->post('nm_barang');
$stok=$this->input->post('stok');
$hrg_beli=$this->input->post('hrg_beli');
$hrg_jual=$this->input->post('hrg_jual');
$this->barang->mengubah_barang($id_barang, $id_jenis, $nm_barang, $stok, $hrg_beli, $hrg_jual);
redirect('cukl', 'refresh');
}
}
....
this is my model
public function mengubah_barang($id_barang, $id_jenis, $nm_barang, $stok, $hrg_beli, $hrg_jual)
{
$hasil = $this->db->query("UPDATE barang SET id_barang='$id_barang', id_jenis='$id_jenis', nm_barang='$nm_barang', stok='$stok',
hrg_beli='$hrg_beli', hrg_jual='$hrg_jual'");
return $hasil;
}
...
this is table view which I use to update data using popups
<table class="table table-borderless table-striped table-earning">
<h2>BARANG</h2><br>
<thead>
<tr>
<th>ID BARANG</th>
<th>ID JENIS</th>
<th>NAMA BARANG</th>
<th class="text-right">STOK</th>
<th class="text-right">HARGA BELI</th>
<th class="text-right">HARGA JUAL</th>
<th>ACTION</th>
</tr>
</thead>
<tbody>
<tr>
<?php
foreach($barang as $i): ?>
<td><?=$i->id_barang;?></td>
<td><?=$i->id_jenis;?></td>
<td><?=$i->nm_barang;?></td>
<td><?=$i->stok;?></td>
<td><?=$i->hrg_beli;?></td>
<td><?=$i->hrg_jual;?></td>
<td style="text-align: center">
<a data-toggle="modal" data-target="#modal_edit<?= $i->id_barang;?>" class="btn btn-success">
<i class="fa fa-pencil" color="white"></i>Ubah
</a>
</td>
</tr>
</tbody>
<?php endforeach ?>
</table>
and this is the popups code
(sorry i cant write it one the page :(( )here is the pict
Opening
Closing
just in case the source code
( https://drive.google.com/open?id=1LbKScF65ej2kT0iQfhU2egBwAGWGA7Ee )
You have used redirect('cukl', 'refresh'); to redirect
Redirect goes in ways :-
redirect('control_name/method_name','refresh')
Your Code is right,but I think there is problem with routing.
You can try it by 301 redirect
redirect('/cukl', 'location', 301);
if still problem not solved please provide .htaccess and config/routes.php here.
I have a blade where I'm printing the content of a table.
For some columns I need to add the CSS class according the value I'm printing.
E.g. if it's "OK", add class Green, otherwise class red.
Of course the logic will be more complex, but the point is that all the logic will be related to the style.
Which one is the best recommended place to save this type of function/method?
Do I need to create a Model?
** UPDATE **
<thead>
<tr>
<th> ID </th>
<th> Name </th>
<th> Last Checked </th>
<th> Status </th>
</tr>
</thead>
<tbody>
#foreach ($users as $u)
<tr>
<td> {{ $u->id }}</td>
<td> {{ $u->name }}</td>
<td> {{ $u->last_login }}</td>
<td> {!! statusWrapper( $u->status ) !!}</td>
</tr>
#endforeach
</tbody>
</table>
"statusWrapper" is the function that I'd like to call to decorate the Status value.
So the status is a number, and the output will be something like <span class="..."> .... </span>
If status should include HTML like showing different colors I recommend you use #include
// resources/views/statusWrapper
#if($status == 'good')
<span style="color: green;">thats really good</span>
#else
<span style="color: red;">not good</span>
#endif
and then in your table view
#foreach ($users as $u)
<tr>
<td> {{ $u->id }}</td>
<td> {{ $u->name }}</td>
<td> {{ $u->last_login }}</td>
<td>
#include('statusWrapper', ['status' => $u->status])
</td>
</tr>
#endforeach
You could also look at extending blade: https://laravel.com/docs/5.5/blade#extending-blade
But I will not recommend you to put HTML in your PHP code as it's easier to keep your HTML in your view files for future edits.
I recommend you what I always do myself. You don't need to make a model, you just have to make a helper file and write all your custom functions in it. For example you can make a file named helper.php in app/Http/Helpers/ path. Then you have to make your project aware of this file. To do that you just have to add it in your composer.json file in autoload -> files object like this:
"autoload": {
"files":[
"app/Http/Helpers/helper.php"
]
}
After this just run command composer dump-autoload. Now you can access to your custom functions which are in you helper.php file from anywhere.
app/providers/AppServiceProvider.php (You can create a different Service Provider, if you wish)
use Illuminate\Support\Facades\Blade;
...
class AppServiceProvider extends ServiceProvider
{
...
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
Blade::directive('statusWrapper', function ($status) {
return "<?php echo App\ViewComponents\StatusWrapperComponent::statusWrapper( $status ); ?>";
});
}
}
app/ViewComponents/StatusWrapperComponent.php
namespace App\ViewComponents;
class StatusWrapperComponent {
public static function statusWrapper ($status) {
if($status == 'good')
echo '<span style="color: green;">thats really good</span>';
else
echo '<span style="color: red;">not good</span>';
}
}
resources/views/yourview.blade.php
<thead>
<tr>
<th> ID </th>
<th> Name </th>
<th> Last Checked </th>
<th> Status </th>
</tr>
</thead>
<tbody>
#foreach ($users as $u)
<tr>
<td> {{ $u->id }}</td>
<td> {{ $u->name }}</td>
<td> {{ $u->last_login }}</td>
<td> #statusWrapper($u->status) </td>
</tr>
#endforeach
</tbody>
</table>
If you're planning to use the function in multiple blade templates, then Tohid's answer is the best.
If you just need the function in a single template, you can define it directly in the .blade.php file like this:
#php
if ( !function_exists( 'mytemplatefunction' ) ) {
function mytemplatefunction( $param ) {
return $param . " World";
}
}
#endphp
Then in you can call the function from within the same template:
<p>{{ mytemplatefunction("Hello") }}</p>
The conditional function definition is needed if you include the blade template multiple times in the same session.
I want to display the database values depending on the particular ID. I pass that id in the URL, and it redirects to another page with ID. In that page I have to display that particular user's records.
Here is the controller function:
public function consultants()
{
return view('Hr/view-request-candidates', [
'consultant' => HrRequestConsultant::all(),
]);
}
Route:
Route::get(
'/view-request-candidates/{hr_request_id}',
'Hr\HrDashboardController#consultants'
);
URL:
CANDIDATES
View:
<div class="col-md-12">
<table class="table table-striped">
<tbody>
<tr>
<th>Hr Request ID</th>
<th>Consultant ID</th>
<th>No Of Candidates</th>
<th>Actions</th>
</tr>
#foreach($consultant as $row)
<tr>
<td>{{$row->hr_request_id}}</td>
<td>{{$row->consultant_id}}</td>
<td>{{30}}</td>
<td>
<a href="/view-hr-candidates" class="btn btn-primary">View</button>
</td>
</tr>
#endforeach
How can I do this?
Routes::
Route::get('home','HrDashboardController#home');
Route::get('view-hr-candidates/{hr_request_id}','HrDashboardController#consultants');
Controller::
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\HrRequestConsultant;
use DB;
class HrDashboardController extends Controller
{
public function home(){
$home = HrRequestConsultant::select('id','hr_request_id','consultant_id')->get();
return view('home')->with('home', $home);
//Its View: home.blade.php file
}
public function consultants($id){
$result = HrRequestConsultant::select('id','hr_request_id','consultant_id')->where('hr_request_id',$id)->first();
return $returnview = view('Hr.view-request-candidates')->with('result', $result);
// Its view-request-candidates.blade.php file InSide the Hr Dir.
}
}
home.blade.php::
<body>
<div class="flex-center position-ref full-height">
<div class="content">
<div class="title m-b-md"> View Details </div>
<div class="col-md-12">
<table border="1" style='color: black'>
<tr>
<th>Hr Request ID</th>
<th>Consultant ID</th>
<th>No Of Candidates</th>
<th>Actions</th>
</tr>
#foreach ($home as $data){
<tr>
<td> {{ $data['hr_request_id'] }} </td>
<td> {{ $data['consultant_id'] }} </td>
<td> 30 </td>
<td> View </td>
</tr>
#endforeach
</table>
</div>
</div>
</div>
</body>
view-request-candidates.blade.php
<h1> Hello .. This Is Request Id: {{ $result['hr_request_id'] }} </h1>
<br>
<h1> Hello .. This Is Consultant Id: {{ $result['consultant_id'] }} </h1>
To get the single record use the find method instead of all:
public function consultants($id)
{
return view('Hr/view-request-candidates', [
'consultant' => HrRequestConsultant::find($id),
]);
}
It is best practice to name the route and use name wherever you need that route. So change the route like this :
Route::get(
'/view-request-candidates/{hr_request_id}',
'Hr\HrDashboardController#consultants')->name('view.consultant');
Remove the foreach loop from your view and change the route to named route:
<div class="col-md-12">
<table class="table table-striped">
<tbody>
<tr>
<th>Hr Request ID</th>
<th>Consultant ID</th>
<th>No Of Candidates</th>
<th>Actions</th>
</tr>
<tr>
<td>{{$consultant->hr_request_id}}</td>
<td>{{$consultant->consultant_id}}</td>
<td>{{30}}</td>
<td>
<a href="{{ route('view.consultant', ['hr_request_id' => $consultant->hr_request_id]) }}" class="btn btn-primary">View</button>
</td>
</tr>