Object of class 'modelName' could not be converted to int in laravel - php

I am trying to create an invoice number like - HCL/LF/02/2018 in where the number will be incremented by one for the new invoice. Would someone help me please to get expected invoice_no? I tried something like this in my controller -
public function create()
{
$check = OrderProformaInvoice::orderBy('created_at', 'DESC')->get();
$number = 01;
$year = date('Y');
if (count($check) > 0) {
$invoiceNo = OrderProformaInvoice::latest()->first(['invoice_no']);
$arr = array('HCL','LF', $invoiceNo + 1, $year);
$newInvoiceNo = implode("/",$arr);
}else{
$arr = array('HCL','LF', $number, $year);
$newInvoiceNo = implode("/",$arr);
}
return view('admin.marchendaising.order-proforma-invoices.create', compact('newInvoiceNo'));
}
And in my view form input field is-
<input type="text" name="invoice_no" class="form-control" value="{{ $newInvoiceNo }}">

You have mistake in this line:
$invoiceNo = OrderProformaInvoice::latest()->first(['invoice_no']);
Correct:
$invoiceNo = OrderProformaInvoice::latest()->get()->invoice_no;

You made a mistake here:
$invoiceNo = OrderProformaInvoice::latest()->first(['invoice_no']);
There was no need for this line since you already have a count of all of the OrderProformaInvoice so we may aswell set this to a variable and just increase it for a new one rather than making another call to the database.
If you ever were to delete a database record, you could use
$invoice = OrderProfromaInvoice::orderBy('created_at', 'desc')->first();
Then instead of $numOfInvoices++ you could do $invoice->id++ which will take the ID of the latest invoice and increment it by one.
public function create()
{
$check = OrderProformaInvoice::orderBy('created_at', 'DESC')->get();
$year = date('Y');
$numOfInvoices = count($check)
$arr = array('HCL','LF', $numOfInvoices++, $year);
$newInvoiceNo = implode("/",$arr);
// You could also just do this
$newInvoiceNo = 'HCL/LF/' . $numOfInvoices++ . '/' . $year;
// $newInvoiceNo will equal example: 'HCL/LF/12/2018';
return view('admin.marchendaising.order-proforma-invoices.create', compact('newInvoiceNo'));
}

Related

ErrorException strtotime() expects parameter 1 to be string, array given

Can anyone help me? this is my controller, when i try to store 'purchase' data it show this message strtotime() expects parameter 1 to be string, array given
The error was on the $purchase->date = date('Y-m-d', strtotime($request->date)); and $purchaseDetail->date = date('Y-m-d', strtotime($request->date));
public function store(Request $request){
if($request->category_id == null){
return redirect()->back()->with('error', 'Please Purchase The Product');
} else{
// Multipale Data Insert start //
$purchase = new purchase();
$purchase->purchase_no = $request->purchase_no;
$purchase->date = date('Y-m-d', strtotime($request->date));
$purchase->description = $request->description;
$purchase->status = '0';
$purchase->created_by = Auth::user()->id;
DB::transaction(function() use($request,$purchase) {
if($purchase->save()) {
// Purchase Details Insert Start //
$category_id = count($request->category_id);
for ($i=0; $i < $category_id; $i++) {
$purchaseDetail = new purchaseDetail();
$purchaseDetail->date = date('Y-m-d', strtotime($request->date));
$purchaseDetail->purchase_id = $purchase->id;
$purchaseDetail->supplier_id = $request->supplier_id[$i];
$purchaseDetail->category_id = $request->category_id[$i];
$purchaseDetail->product_id = $request->product_id[$i];
$purchaseDetail->buying_qty = $request->buying_qty[$i];
$purchaseDetail->unit_price = $request->unit_price[$i];
$purchaseDetail->buying_price = $request->buying_price[$i];
$purchaseDetail->discount_amount = $request->discount_amount[$i];
$purchaseDetail->ppn = $request->ppn[$i];
$purchaseDetail->status = '0';
$purchaseDetail->save();
}
}
});
}
// Redirect
return redirect()->route('purchase.view')->with('success', 'Purchase Added Successfully');
}
Hello, i try to change $purchase->date = date('Y-m-d', strtotime($request->date)); into $purchase->date = $request->date;
But the result was TypeError
Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given, called in D:\Project Laravel\alc-pos\vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\Grammar.php on line 869
As for my opnion you must not format your date when saving to database.
Instead you can format it when outputit from database into your blade templates.
Anyway make a check on $request->date, seems to be an array.
You can format your date using Laravel accessor in your model like this (example):
public function getFormattedStartDateAttribute()
{
return $this->start_date->format('d-m-Y');
}
and then call it in your blade template like this (example):
<input type="text" name="start_date" class="form-control" value="{{$trip->formatted_start_date}}">
More info about Laravel Mutators & Accessors : Laravel Docs
Also you can do another thing. On your schema::create make:
$table->date('date_name_column');
and then just save it like follow:
$purchaseDetail->date = $request->input('date');
This way the date in your database will be save as follow : YYYY-MM-DD
Hope it helps!

How to solve: Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given

I want to store the date but problems happen.
Input
<div class="col-lg-6">
<div class="form-group">
<label>Date</label>
<input type="date" name="date" id="date" class="form-control form-control-sm">
</div>
</div>
Controller
public function store(Request $request){
if($request->category_id == null){
return redirect()->back()->with('error', 'Please Purchase The Product');
} else{
// Multipale Data Insert start //
$purchase = new purchase();
$purchase->purchase_no = $request->purchase_no;
$purchase->date = $request->date;
$purchase->description = $request->description;
$purchase->status = '0';
$purchase->created_by = Auth::user()->id;
DB::transaction(function() use($request,$purchase) {
if($purchase->save()) {
// Purchase Details Insert Start //
$category_id = count($request->category_id);
for ($i=0; $i < $category_id; $i++) {
$purchaseDetail = new purchaseDetail();
$purchaseDetail->date = date('Y-m-d', strtotime($request->date));
$purchaseDetail->purchase_id = $purchase->id;
$purchaseDetail->supplier_id = $request->supplier_id[$i];
$purchaseDetail->category_id = $request->category_id[$i];
$purchaseDetail->product_id = $request->product_id[$i];
$purchaseDetail->buying_qty = $request->buying_qty[$i];
$purchaseDetail->unit_price = $request->unit_price[$i];
$purchaseDetail->buying_price = $request->buying_price[$i];
$purchaseDetail->discount_amount = $request->discount_amount[$i];
$purchaseDetail->ppn = $request->ppn[$i];
$purchaseDetail->status = '0';
$purchaseDetail->save();
}
}
});
}
// Redirect
return redirect()->route('purchase.view')->with('success', 'Purchase Added Successfully');
}
When I store it, an error appears like this:
Argument 1 passed to Illuminate\Database\Grammar::parameterize() must
be of the type array, string given, called in D:\Project
Laravel\alc-pos\vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\Grammar.php
on line 869
I try to change this $purchase->date = $request->date;
into this $purchase->date = date('Y-m-d', strtotime($request->date)); its appears an error like ErrorException strtotime() expects parameter 1 to be string, array given:
strtotime() expects parameter 1 to be string, array given
It seems like $request->date is an array. Confirm it by doing echo print_r($request->date, true); to confirm that $request->date really is an array, and if it is, you can see how it's structured. Take it from there.
There's not really that much to go on here, yet.
Edit:
Final solution (courtesy of #JulienBaldy in the comment section):
Change
$purchase->date = $request->date;
into
$purchase->date = date('Y-m-d', strtotime( $request->date[0]));

Count Array where Column Value Equals Given

I have a controller where an array of checkbox values are passed on to the controller and then sent on to do a variety of tasks. My problem is focused on getting a count of the arrays' shipment's customer's billing method, but I'm not entirely sure how I would get those values since I'm posting an array and I want a count.
Here's my controller:
public function sendNow(Request $request)
{
$now = Carbon::now();
$sendNowShipment = array();
$method = array();
$sendNowShipment = request('send');
foreach($sendNowShipment as $SNS){
$shipment = Shipment::findOrFail($SNS);
if($shipment->billtoAccount->billingMethods->label == "Mail"){
$timestamp = Carbon::now()->timestamp;
$shipment_details = $shipment->shipment_details;
$path = 'temp/freightbill_'.$shipment->pro_number.'-'.$timestamp.'.pdf';
$sessionPath[] = $path;
$pdf = PDF::loadView('shipments.pdf', compact('shipment', 'shipment_details'))
->save($path);
}elseif($shipment->billtoAccount->billingMethods->label == "Email"){
$billToAccount = $shipment->billtoAccount;
$billToAccountUsers = $billToAccount->users;
if ($billToAccount->primary_email){
$billToEmail[] = $billToAccount->primary_email;
}
if ($billToAccountUsers->count() > 0){
foreach ($billToAccountUsers as $billToAccountUser){
$billToEmail[] = $billToAccountUser->email;
}
}
foreach ($billToEmail as $bte){
Mail::to($bte)->send(new newBillToShipment($shipment));
}
}
}
if(count($shipment->billtoAccount->billingMethods->label == "Mail") > 0){// count where shipments->customers->billingMethods = Mail) > 0
$pdf = new PDFMerger();
// Add 2 PDFs to the final PDF
foreach($sessionPath as $sp){
$pdf->addPDF($sp, 'all');
}
// Merge the files and retrieve its PDF binary content
$timestamp = Carbon::now()->timestamp;
$binaryContent = $pdf->merge('download', $timestamp."-printBatch.pdf");
// Return binary content as response
//return response($binaryContent)
// ->header('Content-type' , 'application/pdf');
return back();
//dd($sendNowShipment,$sendMethod);
}
}
If you look at this line (a little past the middle):
if(count($shipment->billtoAccount->billingMethods->label == "Mail") > 0){
// count where shipments->customers->billingMethods = Mail) > 0
You'll see that I'm going through a multitude of relationships just to get the value of "Mail" that I am looking for.
So I'm wondering if this should be a DB:: query through laravel, but I'm not sure how I would perform the query using the given checkbox array I have being POSTed to this controller.

Laravel 5 ORM update request, like date_add

I beg to excuse me for my poor english.
So, I have Laravel 5 ORM, and i need to make request that should add date to some rows, like MySQL DATE_ADD. Input is single date interval and array of id's, output is rows of database, that was changed by adding date interval. Ideally, it should be one ORM request. I know that it is possible to use "bad" way and get all rows, update it in a code, and insert to database, but imho it's not good.
I hope answer will be link to some help site or some code if it's does not complicate you. Thanks for your attention!
public function update($id)
{
$user_id = Auth::user()->id;
$rep_task = RepTask::find($id);
$cl_task = \Request::has('json') ? json_decode(\Request::input('json'),true) : \Request::all();
$ids = [];
$task_id = $rep_task->task_id;
$rep_tasks = RepTask::where('task_id', '=', $task_id)
->select('id')
->get();
$new_date = date_create_from_format(self::DATE_FULLCALENDAR_FORMAT, $cl_task['new_date']);
$selected_task_date = date_create_from_format(self::DATE_MYSQL_FORMAT, $rep_task->start_date);
$diff = date_diff($selected_task_date, $new_date);
if(\Request::has('json'))
{
$ids = [1,2,3]; //this for easy understanding
DB::table('rep_task')
->whereIn('id', $ids)
->update(['start_date' => DB::raw('DATE_ADD(start_date, INTERVAL ' . $diff->d . ' DAY)')]);
$out_json = ['updated' => $ids];
return json_encode($out_json, JSON_UNESCAPED_UNICODE);
}
else
{
$start_date = 0;
$end_date = 0;
if (!isset($cl_task['name']) || !isset($cl_task['text']))
return "{'error':'columns are not defined'}";
if (isset($cl_task['start_date']) && isset($cl_task['end_date']))
{
$dt = date_create_from_format(self::DATE_FULLCALENDAR_FORMAT, $cl_task['start_date']);
$start_date = $dt->format(self::DATE_MYSQL_FORMAT);
$dt = date_create_from_format(self::DATE_FULLCALENDAR_FORMAT,$cl_task['end_date']);
$end_date = $dt->format(self::DATE_MYSQL_FORMAT);
}
$rep_task->name = $cl_task['name'];
$rep_task->text = $cl_task['text'];
$rep_task->start_date = $start_date;
$rep_task->end_date = $end_date;
$rep_task->users_id = $user_id;
$rep_task->save();
}
$user_id = Auth::user()->id;
$tasks = Task::getAllTasksByUserFullcalendar($user_id);
return view(
'task.index',
[
'tasks' => $tasks
]
);
}

I want to take a single value from one function and use it in another function - codeigniter/php/mysql

I want to take a value from a query in one function and use it for multiplication within another function.
Here i have a function producing the number 3:
function insert() {
$siteid = 1;
$field = 3;
$time = "2011-10-11 15:04:56";
$this->db->select('Offset_value', 1)
->from('offset')
->where('siteid', $siteid)
->where('Offset_field', $field)
->where('time <', $time);
$query = $this->db->get()->result_array();
foreach ($query as $row) {
echo $row['Offset_value'];
}
return $query;
}
At the moment this is just set to display the number 3 but i need that value to be stored in a variable of some sort and load it into this function:
function get_sap_estimate() {
$kwp = 5;
$direction = 34;
$tilt = 60;
$month = 4;
$sap_shadingid = 2;
$this->db->select('derating_factor')
->from('sap_shading')
->where('id', $sap_shadingid);
$query = $this->db->get()->result_array();
$query = $query[0];
$dr = $query['derating_factor'];
$this->db->select("kwh * $dr * $kwp AS kwhdata")
->from('expected_kwh')
->where('direction', $direction)
->where('tilt', $tilt)
->where('month', $month);
$query2 = $this->db->get()->result_array();
return $query2;
}
Can this be done and how??
Thanks
function A(){
return "value";
}
functionB(){
var value = A(); // here is your array
var value_you_need = value[0]['Offset_value'];
}
If they are both in the same class (read: model file) write
var $_var;
before any methods are made. Then inside one of your methods type
$this->_var = $stuff;
to give this var a value. Then to use that value type
$new_stuff = $this->_var;
If they're not in the same class, you could try calling the first method inside of the second.

Categories