validation using required_with in laravel for input filed as array? - laravel-5

i have a table with checkbox as array and textbox as also array. what i want to achieve is when the user checked a checkbox it should validate that input textbox are not be empty.
public function roombooking(Request $request)
{
$messsages = array(
'check.required'=>'No room was selected.Please select room to proceed for booking !',
'txtnos.required|numeric'=>'Please enter no of persons',
);
$rules = array(
'check'=>'required',
'txtnos'=>'required_with:data', //txtnos is a array input filed and data is a array checkbox
);
$validator = Validator::make($request->all(), $rules,$messsages
);
if ($validator->fails()) {
return Redirect::back()
->withErrors($validator)
->withinput();
}
}
Html code
<table class="table table-hover" data-toggle="table" id="table"
data-click-to-select="true">
<thead>
<tr>
<th style="width:10%;" data-field="ActivityId">Select</th>
<th style="width:30%;" data-field="ActivityName">Activity Name</th>
<th style="width:30%;" data-field="Rate">Rate/Person</th>
<th style="width:30%;">Nos. of person</th>
</tr>
</thead>
<tbody>
#foreach($loadactivity as $key=>$activity)
<tr>
<td>
<input type="checkbox" name="data[]" value="0;{!! $activity->ActivityId !!};{!! $activity->Rate !!};0;0;{!! $activity->ActivityName !!}" />
</td>
<td>{!! $activity->ActivityName !!}</td>
<td>{!! $activity->Rate !!}</td>
<td >{!! Form::text('txtnos[]','',['class' => 'form-control small-textbox ','txtnoid'=>$activity->ActivityId]) !!}</td>
</tr>
#endforeach
</tbody>
</table>
please help me

Change your form:
...
<td><input type="checkbox" name="row[{{$key}}][data]" value="0;{!! $activity->ActivityId !!};{!! $activity->Rate !!};0;0;{!! $activity->ActivityName !!}" />
</td>
<td>{!! $activity->ActivityName !!}</td>
<td>{!! $activity->Rate !!}</td>
<td>{!! Form::text('row[{{$key}}][txtnos]','',['class' => 'form-control small-textbox ','txtnoid'=>$activity->ActivityId]) !!}</td>
...
So the only thing that's changed is the name of data and txtnos, it will give you the following:
$exampleResult = [
'row' => [
// old $key as new key
0 => [
'txtnos' => 'entered value',
'data' => '1', // But only if checked
],
1 => [
'txtnos' => 'entered value',
'data' => '1', // But only if checked
],
]
];
Validation rules
$rules = [
'row.*.txtnos' => 'required_with:row.*.data'
];
In the example, txtnos on each row is required if data on the same row isset.
Validation message
$messages = [
'row.*.txtnos.required_with' => 'Enter a value or uncheck the checkbox..'
];
Important:
The validation for .*. was added in Laravel 5.2, you didn't specify your exact version so I'm not sure if it will work for you. Anyway, there is another way to do this.
For versions < 5.2, loop the input rows and replace the * in my example with the current key.

Related

not able to show array in Table #foreach

I am new to Laravel and coding. Trying to show Month name (Array) in table but not able to do the same. I tried few things like "json_encode" but got error each time.
My controller code is
$months = array();
for ($i = 0; $i < 12; $i++) {
$timestamp = mktime(0, 0, 0, date('n') - $i, 1);
$months[date('n', $timestamp)] = date('M-Y', $timestamp);
}
Output is
array:12 [▼
3 => "Mar-2022"
2 => "Feb-2022"
1 => "Jan-2022"
12 => "Dec-2021"
11 => "Nov-2021"
10 => "Oct-2021"
9 => "Sep-2021"
8 => "Aug-2021"
7 => "Jul-2021"
6 => "Jun-2021"
5 => "May-2021"
4 => "Apr-2021"
]
View file
<table id="incometable" class="table m-0 table table-bordered table-hover table" data-order='[[ 0, "desc" ]]'>
<thead>
<tr>
<th align="center">Month</th>
<th>Milk Sale Amount (Rs.)</th>
<th>Animal Sale Amount (Rs.)</th>
<th>Other Sale Amount (Rs.)</th>
</tr>
</thead>
<tbody>
#foreach ($months as $item )
<tr>
<td>{{ $item->$months }}</td>
</tr>
#endforeach
</tbody>
</table>
Thanks in Advance
During your #foreach() loop, $item is not an Object, it is a String 'Mar-2022' through 'Apr-2021'. You need to adjust your code:
#foreach($months as $key => $label)
<tr>
<td>{{ $label }}</td>
<tr>
#endforeach
If you need the 1 through 12 values, they are available in each iteration as $key.

How to check if checkbox is checked for each row

If one row is checked if($isset->has('test')) returns all rows.
I only want to save the marked rows.
My view file:
<tbody>
#foreach($match->homeTeam->players as $player)
<tr>
<th scope="row">{{$player->id}}</th>
<td class = "col-md-6" name = "player[]" value = "{{$player->id}}">{{$player->name}} {{$player->surname}}</td>
<td align="center" class= "col-md-2"><input class="form-check-input" type="checkbox" name="test" value="{{$player->id}}"></td>
<td class = "col-md-2"><input name="minutes[]" class="form-control"></td>
<td class = "col-md-2"><input name="goals[]" class="form-control"></td>
</tr>
#endforeach
</tbody>
Controller:
public function storeMatchFacts(Match $match, Request $request, Match_fact $match_fact, Player $player)
{
$home_team_players=$match->homeTeam->players;
if($isset->has('test')) {
foreach ( $home_team_players as $k => $p ) {
$data[] = [
'match_id' => $match->id,
'player_id' => $player->id,
'minutes' => $request['minutes'][$k],
'goals' => $request['goals'][$k]
];
}
dd($data);
} else {
dd('error');
}

I want to show status with order when i search order by created_at date using laravel

currently i am working on order report i am fetching all order records but except status i have last_status column id which is defined by id with every order but i want to show status with order please help me how can i do that ? Does anyone have an idea please help me thanks.
status codes
'0' = 'Pending',
'1' = 'Info Received',
'2' = 'In Transit',
'3' = 'Out for Delivery',
'4' = 'Failed Attempt',
'5' = 'Delivered',
'6' = 'Exception',
'7' = 'Expired',
'8' = 'Canceled',
'9' = 'Void',
reportController
public function report(Request $request)
{
$data = [];
// sets the start date at index 0 and ending date at index 1
$date = explode(' - ', $request->date);
$Order = Order::with('users');
$order = $Order->whereBetween('created_at', $date)
->orderBy('created_at', 'desc')
->get();
$data = [
'orders' => $order,
];
// return $data['orders'];
return view('cms.reports.report-list', $data);
}
html view
<table class="table table-bordered table-striped" cellspacing="0" cellpadding="0">
<thead>
<tr>
<th>Order Id</th>
<th>Customer Name</th>
<th>Status</th>
<th>Order Received Date</th>
</tr>
</thead>
<tbody>
#foreach($orders as $order)
<tr>
<td>{{$order->id}}</td>
<td>{{$order->users->fname ?? null}} {{$order->users->lname ?? null}}</td>
<td>{{$order->last_status}}</td>
<td>{{$order->created_at}}</td>
</tr>
#endforeach
</tbody>
<tfoot>
</tfoot>
</table>
Rather than having to add a CASE statement every time you query, you could add the status to a config file and reference from there.
config/orders.php
<?php
return [
'status' => [
0 => [
'label' => 'Pending',
'color' => 'grey',
],
1 => [
'label' => 'Info Received',
'color' => 'teal',
],
2 => [
'label' => 'In Transit',
'color' => 'blue',
],
...
],
];
create a blade component:
resources/views/components/status-label.blade.php
<span
class="text-sm font-medium bg-{{ config('orders.status.'.$status.'color') }}-100 py-1 px-2 rounded text-{{ config('orders.status.'.$status.'color') }}-500 align-middle">
{{ config('orders.status.'.$status.'label') }}
</span>
Then in your blade file, you can do:
<x-status-label :status="$order->last_status" />
you can do this way...
in order model class
public function orderStatus()
{
switch($this->order_status){
case '1':
return 'Info Received';
break ;
case '2':
return 'In Transit';
break ;
default:
return 'Pending';
}
}
in view
{{ $order->orderStatus() }}
solution with html markup
in order model class
public function orderStatus()
{
switch($this->order_status){
case '1':
return '<span class="label label-warning">Info Received</span>';
break ;
case '2':
return '<span class="label label-info">In Transit</span>';
break ;
default:
return '<span class="label label-danger">Pending</span>';
}
in view
{!! $order->orderStatus() !!}
and css class
.label {
color:white;
display:inline-bolck;
paddign:3px;
text-align: center;
line-height:25px;
min-width: 50px;
}
.label-info{
background:blue;
}
.label-danger{
background:red;
}

Adding rows and columns to a table dynamically using vue js

I am trying to implement a feature where a user would be able to add new columns and rows to a table using vue js. I am able to push a tag to the table however the method I'm using to add the I don't think its the right way especially since the td's are being added to the object but only to the view itself.
I am pulling the default html table content from laravel's helper file
(object)array(
'label' => 'Table',
'field_name' => '',
'type' => 'table',
'properties' => (object)array(
'headers' => [
'Header 1',
],
'rows' => [
(object) array(
'value' => 'Row 1 content',
),
],
)
)
Html(vue js)
...
<div v-if="field.type == 'table'">
<pre>{{field.properties.headers}}</pre><br>
<pre>{{field.properties.rows}}</pre>
<table :class="`table ` + field.properties.style" :id="`table`+index">
<thead>
<tr>
<th v-for="(header, index) in field.properties.headers" v-if="header.length > 0">
{{header}}
<i class="fa fa-trash"></i>
</th>
</tr>
</thead>
<tbody>
<tr v-for="(row, index) in field.properties.rows" :key="index" id="default-row">
<td>{{row.value}}</td>
</tr>
</tbody>
</table>
<table>
<tr>
<td>
Add Column
Add Row
</td>
<td></td>
<td></td>
</tr>
</table>
</div>
...
Script(vue js)
...
methods: {
addTableColumn(properties, index) {
properties.headers.push('Column Heading');
$(`#table${index} #default-row`).append(`<td>Row Content</td>`);
},
removeTableColumn(properties, index) {
properties.headers.splice(index, 1);
properties.rows.splice(index, 1);
},
addTableRow(properties, index) {
// let columnCount = properties.headers.length;
// for(let i = 0; i < columnCount; i++) {
// properties.rows.push({'value': 'Row content'});
// }
// console.log(columnCount);
// console.log(index);
// rows.push({ 'value': 'Row column' });
$(`#table${index}`).append(`<tr>${$('#default-row').html()}</tr>`);
}
},...
View
As seen in the picture below, I clicked the "add column" button and only the heading itself is pushed to the array and not the new td content(Row content).

Laravel Excel - blank sheets when creating multiple sheets

I had this working, using Laravel 4 and Laravel Excel to export data from mySQL. I have objects for Regtype and Attendee and I want one sheet per regtype with all related attendees. I had a single sheet of attendees exporting great. Now I've added a loop for the regtypes and I'm able to get multiple sheets but all sheets are blank!
Perhaps I have to use shareView? I wouldn't think so.. I get no errors and none of the data I pass to the blade template is displaying.
My export function:
public function export()
{
$date = date('Y_m_d');
\Excel::create('CMO_Connect_Attendees_Export_'.$date, function($excel) {
$regtypes = Regtype::all();
foreach ($regtypes as $regtype) {
if( $regtype->attendees(3)->count() ) {
$excel->sheet($regtype->name, array('regtype' => $regtype), function($sheet) {
$date = date('Y_m_d');
$attendees = new Attendee;
$atts = Attendee::where('block_id', '=', \Input::get('block_id'))
->where('regtype_id', '=', $regtype->id)
->get();
$sheet->setStyle(array(
'font' => array(
'name' => 'Arial',
'size' => 12,
'bold' => false
)
));
$sheet->loadView('attendees.export', array('atts' => $atts))->with('curdate',$date)->with('regtype_name',$regtype->name);
$atts = '';
});
} //endif
} //endforeach
$excel->export('xls');
});
}
And the blade template I'm passing data to (I pass the attendees and also the date and name of the regtype to display at the top of the sheet:
<html>
<table>
<tr>
<td colspan="11">Attendees Export - {{ $curdate }} - {{ $regtype_name }}</td>
</tr>
<tr>
<td>First Name</td>
<td>Last Name</td>
<td>Company</td>
<td>Title</td>
<td>Email</td>
<td>Phone</td>
<td>Address</td>
<td>Addres 2</td>
<td>City</td>
<td>State</td>
<td>Zip</td>
<td>Date Created</td>
</tr>
#foreach($atts as $att)
<tr>
<td> {{ $att->firstname }} </td>
<td> {{ $att->lastname }} </td>
<td> {{ $att->company }} </td>
<td> {{ $att->title }} </td>
<td> {{ $att->email }} </td>
<td> {{ $att->phone }} </td>
<td> {{ $att->address }} </td>
<td> {{ $att->address2 }} </td>
<td> {{ $att->city }} </td>
<td> {{ $att->state }} </td>
<td> {{ $att->zip }} </td>
<td> {{ $att->created_at }} </td>
</tr>
#endforeach
</table>
</html>
Thanks for any help!
I sorted out - below is my export function now. I updated the vendor files through composer (realized I was on a slightly older version) and revised my code with use($regtype when calling the $sheet method (passing the regtype object into the sheet). I assumed that you'd pass variables or objects into the sheet the same way you pass data into the blade template but that's not correct.
public function export()
{
$date = date('Y_m_d');
\Excel::create('CMO_Connect_Attendees_Export_'.$date, function($excel) {
$excel->setTitle('CMO Connect Attendee Data Export');
$excel->setCreator('Dylan Glockler')
->setCompany('Bryan Allen Events');
$excel->setDescription('All attendee event data for Adobe CMO Connect');
$regtypes = Regtype::all();
$summary = '';
foreach ($regtypes as $regtype) {
if( $regtype->attendees(3)->count() ) {
$summary = $summary . $regtype->name;
$summary = $summary . ': '.$regtype->attendees(3)->count();
$summary = $summary . ' | ' . $regtype->id . '<br />';
$excel->sheet($regtype->name, function($sheet) use($regtype) {
$date = date('Y_m_d');
$atts = Attendee::where('block_id', '=', \Input::get('block_id'))
->where('regtype_id', '=', $regtype->id)
->get();
$sheet->setStyle(array(
'font' => array(
'name' => 'Arial',
'size' => 12,
'bold' => false
)
));
$sheet->loadView('attendees.export')->with('curdate',$date)->with('atts',$atts)->with('regtype_name',$regtype->name)->with('att_count',$regtype->attendees(3)->count());
});
} //endif
} //endforeach
$excel->export('xls');
});
}

Resources