Unable to collect data from my PostController using Laravel - laravel

I am trying to create a feature on my web application that allows the user to see the posts between two dates. However, I am having problems trying to pass data from my database to my blade template. Instead of retrieving the created_at date of the post I receive the date "1/01/1970" and the job number does not appear.
First I added the routes in my web.php file:
Route::get('/search', function () {
return view('search');
});
Route::post('/select', 'PostController#getDate');
In my PostController.php file, I added my getDate Function:
public function getDate(Request $request){
$posts = DB::table('jobs')
->whereBetween('created_at', [$request->fdate, $request->sdate])
->get();
$posts->created_at = $request->created_at;
$posts->job_number = $request->job_number;
return view('result', ['posts' => $posts]);
}
My search.blade.php which is the form:
<form method="POST" action="/select">
{{ csrf_field() }}
<div class="form-group">
<label>First Date:</label>
<input type="date" class="form-control" name="fdate">
</div>
<div class="form-group">
<label>Second Date:</label>
<input type="date" class="form-control" name="sdate">
</div>
<input type="submit" value="Get Post Between" class="btn btn-primary">
</form>
My result.blade.php file which shows the post.
#if(count( $posts )>0)
<table class="table table-bordered table-striped">
<thead>
<th>Date created</th>
<th>Job Number</th>
</thead>
<tbody>
#foreach($posts as $post)
<tr>
<td>{{ date('j/m/Y', strtotime($posts->created_at)) }}</td>
<td>{{ $posts->job_number }}</td>
</tr>
#endforeach
</tbody>
</table>
#else
<h3 class="text-center">No Post from Selected Range</h3>
#endif
I am honestly confused, I have used the correct variables.

Here you are appending properties to a collection object not to each element
$posts->created_at = $request->created_at;
$posts->job_number = $request->job_number;
to add these properties to each element you should use map() or loop the elements.
foreach($posts as $post) {
$post->created_at = $request->created_at;
$post->job_number = $request->job_number;
}
you should use the single post not the whole array to get the date
change
<td>{{ date('j/m/Y', strtotime($posts->created_at)) }}</td>
with
<td>{{ date('j/m/Y', strtotime($post->created_at)) }}</td>
same stuff for the {{ $posts->job_number }} -> {{ $post->job_number }}

Related

how to update an array in laravel

i have an array of data in a form that i want to update.when i update only the last column updated while the inputs that i updated do change.for example, we have a column size, price, and stock. the size include small,medium, and large and their respective prices and stock number. when i update the price of small size,it doesnt update but rather it updates the medium size.same for the large size also.i havent understood why only a specfic column is updating yet i have added a foreach to loop all the columns when updating.here is my update function.
public function editattributes(Request $request,$id)
{
$merchadisedata=Merchadise::select('id','merch_name','merch_code','merch_image')->find($id);
if ($request->isMethod('post')){
$data=$request->all();
foreach($data['attrid'] as $key=>$attr){
if(!empty($attr)){
Productattribute::where(['id'=>$data['attrid'][$key]])
->update([
'productattr_price'=>$data['productattr_price'][$key],
'productattr_stock'=>$data['productattr_stock'][$key],
]);
}
$message='Product attributes have been updated successfully';
Session::flash('success_message',$message);
return redirect()->back();
}
}
return view('backend.merchadise.editproductattributes')->with(compact('merchadisedata'));
}
on dd($data);die(); i get the inputs i have filled
here is the form in the blade file that am submitting
<form method="post" action="{{ url('admin/edit-attributes/'.$merchadisedata->id) }}">
{{csrf_field()}}
<table id="products" class="table table-striped table-bordered nowrap" style="width:100%;">
<thead>
<tr>
<th>Attribute Size</th>
<th>Attribute Stock</th>
<th>Attribute Price</th>
<th>Attribute Sku</th>
</tr>
</thead>
<tbody>
#foreach ( $merchadisedata->merchadiseattributes as $attribute)
<input type="text" name="attrid[]" value="{{ $attribute->id }}" style="display: none"/>
<tr>
<td>{{ $attribute->productattr_size}}</td>
<td>
<input type="number" name="productattr_stock[]" value="{{ $attribute->productattr_stock }}" required=""/>
</td>
<td>
<input type="number" name="productattr_price[]" value="{{ $attribute->productattr_price }}" required=""/>
</td>
<td>{{ $attribute->productattr_sku }}</td>
</tr>
#endforeach
</tbody>
</table>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-sm btn-block">Submit</button>
</div>
</form>
where might i be going wrong in my code

How to get Laravel Blade value as an Array data using Post Method?

I Have a Blade Form that will execute Post Method, this is my blade
#foreach ($dataku as $row => $order)
<div class="row">
<div class="col-md-12">
<div class="row">
<div class="col-md-12 text-center">
<b>{{ $order->delivery_order_no }}</b>
<input type="hidden" name="order[{{ $row }}][do_id]" value="{{ $order->id }}">
<input type="hidden" name="order[{{ $row }}][so_id]" value="{{ $order->sales_order->id }}">
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="table-responsive m-t-40" style="clear:both;">
<table class="table table-hover" style="font-size: 9pt;">
<thead>
<tr><th class="text-center">No</th>
<th class="text-center">SKUID</th>
<th class="text-center">Item Name</th>
<th class="text-center">UOM</th>
<th class="text-center">Qty So</th>
<th class="text-center">Qty Do</th>
<th class="text-center">Qty Confirm</th>
<th class="text-center">Qty Minus</th>
<th class="text-center">Remark Confirm</th>
</tr>
</thead>
<tbody>
#foreach ($order->delivery_order_details as $do =>$detOrder )
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $detOrder->skuid }}</td>
<td>{{ $detOrder->sales_order_detail->item_name }}</td>
<td>{{ $detOrder->uom->name }}</td>
<td>{{ $detOrder->sales_order_detail->qty }}</td>
<td>{{ $detOrder->qty_do }}</td>
<td>
<input type="hidden" class="form-control" name="order[{{ $row }}][detail[][{{ $do }}][skuid]]" value="{{ $detOrder->skuid}}">
<input type="number" class="form-control" name="order[{{ $row }}][detail[][{{ $do }}][qty_do]]" value="{{ $detOrder->qty_do }}">
</td>
<td>
<input type="number" min="0" class="form-control" name="order[{{ $row }}][detail[][{{ $do }}][qty_minus]]" value="0">
</td>
<td>
<input type="text" placeholder="Remark Confirm" class="form-control" name="order[{{ $row }}][detail[][{{ $do }}][remarks]]">
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
#endforeach
I Just want to get the data from the blade as an array ,... and then this is my controller
public function update(Request $request)
{
return $request->all();
}
I Get The Data Like This
is the return value from my blade $request->all(); correct? looks like something wrong ???
#sta provided an answer but I would make it more detailed:
When you build more complex structure (nested) in a html form input, every next key or list has to be surrounded by [].
So if you do just a nested keys it will be something[key1][key2][key3].
And if you do an array and a nested keys it will be something[key1][key2][][key3], where [] means that [key2] will be an array (and every array element will have a key3 key with provided value in input value attribute.
That's why this input name can't work:
order[0][detail[][0][skuid]]
but this one will work:
name="order[0][detail][0][qty_minus]

How to fill the second html table by clicking a button from the first table when both of them are on the same page in Laravel?

I am using Laravel with mySql. I have two table in blade view, one in main div which contains a button to open the second which is in aside div and fill it with data retrieved from the controller. Please find below my code:
my routes
// showing list of orders
Route::get('orders/show/all', 'OrderController#showOrders')->name('orders.show.all');
// showing order details
Route::get('orders/details/{id}', 'OrderController#showOrderDetails')->name('orders.details');
my controller (OrderController), copied only two functions which have to do with this
public function showOrders(){
$orders = Order::orderBy('total_amount', 'desc')
->where('user_id','=', Auth::user()->id)
->get();
return view('orders.undo_sales', compact('orders'));
}
public function showOrderDetails($id){
$ordered_item = Order::findOrFail($id);
return redirect('orders/show/all', compact('ordered_item'));
}
}
my blade.php file
<main class="col-sm-8">
#include('partials.messages')
<div class="card">
<div class="card-header">List of all sales</div>
<div class="card-body">
<table class="table ">
<thead>
<tr>
<th>Order #</th>
<th>Order Date</th>
<th>Total Amount</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
#foreach ($orders as $order)
<tr>
<td>{{ $order->id }}</td>
<td>{{ $order->created_at }}</td>
<td>{{ number_format($order->total_amount) }}</td>
<td>
Order details
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</main>
<aside class="col-sm-4" id="aside">
<div class="card">
<div class="card-header">Order Details</div>
<div class="card-body">
<table class="table ">
<thead>
<tr>
<th>Item Name</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
#foreach ($ordered_item->items as $value)
<tr>
<td>{{ $value->item_name }}</td>
<td>{{ number_format($value->pivot->quantity_on_order) }}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</aside>
</div>
<script>
let showDetails = document.querySelector('.show-details')
let aside = document.querySelector('#aside')
aside.style.display = 'none'
function showDiv(){
aside.style.display = 'block'
}
</script>
There is no problem with filling the main (first) table with data from the controller. The problem is getting the object returned by the function showOrderDetails($id) to the same view but on a different table and display those records. I tried to dd() the data in the controller on this method and I could see the item_name and the respective quantity. Which proved that there is no problem with this code in blade file:
#foreach ($ordered_item->items as $value)
<tr>
<td>{{ $value->item_name }}</td>
<td>{{ number_format($value->pivot->quantity_on_order) }}</td>
</tr>
#endforeach
But when I sent to the view I got the error in my view file:
Undefined variable: ordered_item
Please help me to get the data across.
Thank you.
What you're trying to do is to redirect to the same page with an additionnal variable. But it can't work like that.
A simple answer would be this:
web.php
Route::get('orders/show/all', 'OrderController#showOrders')->name('orders.show.all');
Controller.php
public function showOrders(){
$orders = Order::orderBy('total_amount', 'desc')
->where('user_id','=', Auth::user()->id)
->get();
// I have added the same condition. I believe, you don't want the user to be able to see all user's orders
$ordered_item = Order::where('user_id', '=', Auth::user()->id)
->find(request('order_id'));
return view('orders.undo_sales', compact('orders', 'ordered_item'));
}
}
blade.php
....
Order details
....
#if($ordered_item)
<aside class="col-sm-4" id="aside">
#foreach ($ordered_item->items as $value)
....
#endforeach
</aside>
#endif

Error "Trying to get property 'pertanyaan' of non-object" on Dompdf Laravel 5.8

I have a view and I want to have a link that downloads a PDF.
View
<div class="col-lg-12 col-xs-12">
<div class="box">
Download PDF
<div class="box-header">
<div style="text-align:center"><h3 class="box-title">PEMELIHARAAN DAN PERAWATAN ALAT UJI </h3></div>
<div style="text-align:center">Jln. Kabupaten Sragen</div>
</div>
<p> Laporan : {{ $pemeliharaan->status }} </p>
<p> Tanggal : {{ $pemeliharaan->created_at }} </p>
<p> Jenis Alat : {{ $pemeliharaan->alat->nama_alat }} </p>
<p> User : {{ $pemeliharaan->user->name }} </p>
<div class="box">
<div class="box-header">
<h3 class="box-title"></h3>
</div>
<div class="box-body no-padding">
<table class="table table-condensed">
<tbody>
<tr>
<th style="width: 10px">#</th>
<th>Pertanyaan</th>
<th>Hasil</th>
</tr>
<tr>
<td>1.</td>
<td>{{ $pemeliharaan->pertanyaan['question1'] }}</td>
<td>{{ $pemeliharaan->pertanyaan['answer1'] }}</td>
</tr>
<tr>
<td>2.</td>
<td>{{ $pemeliharaan->pertanyaan['question2'] }}</td>
<td>{{ $pemeliharaan->pertanyaan['answer2'] }}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
The link 'download' has an error.
Just Showing 404
Controller
public function showQuestion(Request $request, $id)
{
$pemeliharaan = Pemeliharaan::findOrFail($id);
$pemeliharaan->pertanyaan = json_decode($pemeliharaan->pertanyaan, true);
if ($request->has('download')) {
$pdf = PDF::loadView('users.view_question', $pemeliharaan);
return $pdf->download('view_question.pdf');
}
return view('users.view_question', compact('pemeliharaan'));
}
Routes
Route::get('/user/show/question/pdf/{id}','userController#showQuestion')->name('pdf');
Route::get('user/show/question/{id}', 'userController#showQuestion')->name('usershowQuestion');
Can someone help me with the code for the download?
You are not supplying the id route parameter to the named route 'pdf'. That's causing the line $pemeliharaan = Pemeliharaan::find($id) to yield null and later an error.
Try this:
{{ route('pdf', [ $id ] }}
In order for it to work you must supply the variable $id to the view. You can do that something like
return view('your-view')->with([
"id" => $id
]);

Why is The Entire Body of the Web page loaded to the table instead of the data when retrieving data from the database using ajax in Larvel

I have this table showing all the encashment records, it works fine but when I try to get the rows from the database within a specific range of dates and display it to the same table, the whole body of the webpage is being loaded to the table instead of the rows from the database.
Please see image below:
this is my function from the tableController.php
function encashmentRecord_DateRange(Request $request){
if($request->ajax())
{
$start = $request->get('startDate');
$end = $request->get('endDate');
$data = DB::table('encashment_requests')->whereBetween('date',[$start,$end])->get();
return view('incashment',compact('data'));
}
}
the script in my encashment.blade.php
$(document).ready(function(){
$('#dataTable').DataTable();
$('#start_date').datepicker({
format:'yyyy-mm-dd',
autoclose:true
}).val();
$('#end_date').datepicker({
format:'yyyy-mm-dd',
autoclose:true
}).val();
var range = document.getElementById('btnrange');
range.addEventListener('click',function(){
var start_date = $('#start_date').val();
var end_date =$('#end_date').val();
alert(end_date);
$('#dataTable').DataTable().destroy();
listDateRange(start_date,end_date);
},false);
function listDateRange(startDate,endDate){
$.ajax({
method: 'GET',
url : "{{ route('encashmentRecord.action') }}",
data: {startDate:startDate, endDate:endDate},
success:function(data)
{
alert(data);
$('#dataTable').html(data);
}
})
}
});
If you only need the data, you shouldn't be returning view() from your controller.
Try replacing it with this: return response()->json($data, 200);
I may have found the solution to my problem, though it may look lengthy. But it worked.
I made another view, encashmentRecord.blade, containing only the table, so in the controller, instead of returning the data in the incashment.blade, I returned the data to the new view, encashentRecord.blade and I use "#include('encashmentRecord')" in my incashment.blade to display the table.
encashmentRecord.blade.php
<div class="card-body">
<table id="dataTable" class="table table-striped table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Account ID</th>
<th class="sort">Date</th>
<th>Desired Ammount</th>
<th>Destination</th>
<th>Status</th>
</tr>
</thead>
<tbody>
#if(isset($data))
#foreach($data as $row)
<tr>
<td>{{ $row->account_name}}</td>
<td>{{ $row->account_id}}</td>
<td>{{ $row->created_at}}</td>
<td>Php {{ $row->requested_ammount}}</td>
<td>{{ $row->destination}}</td>
<td>{{ $row->status}}</td>
</tr>
#endforeach
#else
<tr>
No Data
</tr>
#endif
</tbody>
</table>
and this is incashment.blade.php
<div class="row">
<div class="col-xl-3 ">
<input type="text" name="query" id="start_date" class="form-control input_daterange">
</div>
<div class="col-xl-3 ">
<input type="text" name="query" id="end_date" class="form-control">
</div>
<div class="col-xl-3 ">
<button class="btn" id="btnrange">Go</button>
</div>
</div>
<div class="card" style="margin-bottom: 0px">
<div class="card-header">
Encashment Record
</div>
<div id="table_data">
#include('tables.encashmentRecord')
</div>
</div>
I just change the view in my return on the controller
return view('tables.encashmentRecord',compact('data'));

Resources