Cannot Update On Laravel 5.5 - laravel

I get problem.
This is my controller
public function finish(Request $request)
{
$result = $request->input('data');
//$data = json_decode($result, true);
return $this->InvoiceBayar($result);
}
public function InvoiceBayar($result)
{
$data = json_decode($result, true);
$transaction = $data['transaction_status'];
$type = $data['payment_type'];
$order_id = $data['order_id'];
$fraud = $data['fraud_status'];
Fee::where('invoice',$order_id)
->update([
'status' => 'Paid',
]);
echo "Transaction order_id: " . $order_id ." successfully transfered using " . $type;
}
This is my Route
Route::POST('/notification', 'SnapController#finish');
When Payment gateway, send a parameter to me, I cannot update DB.
But when I use POSTMAN. I success update DB

You need to use $request->all() as it will contain all payment gateway data.
public function finish(Request $request)
{
$result = $request->all();
return $this->InvoiceBayar($result);
}

Alternately you can do this
$update = Fee::where('invoice',$order_id)->first();
$update->status = 'Paid';
$update->save();

You should try this:
public function InvoiceBayar($result)
{
$data = json_decode($result, true);
$transaction = $data->transaction_status;
$type = $data->payment_type;
$order_id = $data->order_id;
$fraud = $data->fraud_status;
Fee::where('invoice',$order_id)
->update([
'status' => 'Paid',
]);
echo "Transaction order_id: " . $order_id ." successfully transfered using " . $type;
}

Related

Undefined index: email in codeigniter

im trying to log in but i keep encountering the error below. attached is my controller and model
controller
public function login(){
//load session library
$this->load->library('session');
$this->load->model('Model_students');
$email = $_POST['email'];
$password = $_POST['password'];
$data = $this->Model_students->login($email, $password);
if($data)
{
$id=$data[0]->id;
$first_name=$data[0]->firstname;
$last_name=$data[0]->lastname;
$this->session->set_userdata('user_id',$id);
$this->session->set_userdata('lname',$last_name);
$this->session->set_userdata('user', $email);
$this->session->set_userdata('fname',$first_name);
$this->getImg();
redirect('Students/homepage');
}
else{
$this->session->set_flashdata('error','Invalid login. User not found');
}
}
model
public function login($email,$password)
{
// $query = $this->db->get_where('users', array('email'=>$email, 'password'=>$password));
$query = $this->db->get_where('users', array('email'=>$email));
if($query->num_rows() == 1 )
{
return $query->result();
}
wheni i run this i get an error:
Message: Undefined index: email
Filename: controllers/Students.php
Line Number: 145
please don't use $_POST parameter without validation. use input library
public function login(){
//load session library
$this->load->library('session');
$this->load->model('Model_students');
$post = $this->input->post(NULL, TRUE);
$email = isset($post['email']) ? $post['email'] : '';
$password = isset($post['password']) ? $post['password'] : '';
$data = $this->Model_students->login($email, $password);
if($data && !empty($data)){
$id = $data[0]->id;
$first_name = $data[0]->firstname;
$last_name = $data[0]->lastname;
$this->session->set_userdata('user_id', $id);
$this->session->set_userdata('lname', $last_name);
$this->session->set_userdata('user', $email);
$this->session->set_userdata('fname', $first_name);
$this->getImg();
redirect('Students/homepage');
}else{
$this->session->set_flashdata('error', 'Invalid login. User not found');
}
}

update data to database laragon using api

I want to update data to the database in ionic. how to make the data update. Here what I tried. I try using postmen to post the api and it appear success but the data does not change.
in api.php
public function update (Request $request)
{
$id = $request->id;
$medname = $request->medname;
$price = $request->price;
$stock = $request->stock;
$medno = $request->medno;
$ingredient = $request->ingredient;
$description = $request->description;
$addinfo = $request->addinfo;
AddMedicine:: where('medname',$medname)->update([
'id' =>$id,
'medname'=>$medname,
'price'=>$price,
'stock'=>$stock,
'medno'=>$medno,
'ingredient'=>$ingredient,
'description'=>$description,
'addinfo'=>$addinfo,
]);
$msg = "Data Updated";
$datamsg = response()->json([
'success' => $msg
]);
return $datamsg->content();
}
route
Route::put('/update','ApiController#update');
Are you sure use PUT request ? Because need to CSRF token please inspect
https://stackoverflow.com/questions/30756682/laravel-x-csrf-token-mismatch-with-postman

Not updating database and not throwing error in laravel

I have an issue while updating the user. When I try to update the user after clicking the save button then it redirect me to the same page and not throwing me any error but also its not updating anything in the database. Below is my code. I have no idea what's going on here. Help me :)
Controller
public function update(ReportRequest $request, $id)
{
$report = Report::findOrFail($id);
$input = $request->all();
if ($file = $request->file('photo_id')) {
$name = time() . $file->getClientOriginalName();
$file->move('images', $name);
$photo = Photo::create(['file' => $name]);
$input['photo_id'] = $photo->id;
}
$report->update($input);
return redirect()->back();
}
Route
Route::resource('admin/reports', 'ReportController', ['names'=>[
'index'=>'admin.reports.index',
'create'=>'admin.reports.create',
'edit'=>'admin.reports.edit',
]]);
Models
class Report extends Model
{
protected $fillable = [
'student_id',
'student_name',
'class_id',
'subject',
'teacher_name',
'report_categories_id',
'total_marks',
'obtained_marks',
'percentage',
'position',
'photo_id',
];
public function photo() {
return $this->belongsTo('App\Photo');
}
public function studentsClass() {
return $this->belongsTo('App\StudentsClass', 'class_id');
}
public function student() {
return $this->belongsToMany('App\Student');
}
}
Make sure you have your $fillable properties in your Photo and Report models, otherwise the create() and update() methods won't work as expected.
Check the $fillable fields in the Model as above. If the error persists check your laravel log on storage/logs/laravel.log.
In controller:
public function update(ReportRequest $request, $id){
$report = Report::findOrFail($id);
$input = $request->all();
try{
if ($request->photo_id != '') {
$path = 'images/';
$file = $request->photo_id;
$name = time() . $file->getClientOriginalName();
$file->move($path, $name);
$photo = Photo::create(['file' => $name]);
$report->update(['photo_id' => $photo->id]);
}
return redirect()->back();
}catch(\Exception $e){
return redirect()->back()->with('error_message', $e->getMessage());
}
}

laravel session route redirection error on multiple input fields

I have the following controller whenever i hit submit it redirects me to sales. Where as it should return admin.invoice.index page rather than sale.index. can any one please help?
$data = array();
for ($i=0; $i < count($request['product_id']); ++$i)
{
$sales= new Sale;
$sales->product_id = $request['product_id'][$i];
$sales->qty= $request['qty'][$i];
$sales->user_id = Auth::user()->id;
$sales->save();
$product = new Product;
$product->where('id', '=', $request['product_id'][$i])->decrement('stock', $request['qty'][$i]);
$data[]['sales'] = $sales;
$data[]['product'] = $product;
}
$data = [];
if ($request->session()->has('data')) {
$data = $request->session()->get('data');
}
Session::flash('success', 'Sale is successfully');
return view('admin.invoice.index')->with('data', $data);
}
Pass the data with with() through session.
{
$data = array();
for ($i=0; $i < count($request['product_id']); ++$i)
{
$sales= new Sale;
$sales->product_id = $request['product_id'][$i];
$sales->qty= $request['qty'][$i];
$sales->user_id = Auth::user()->id;
$sales->save();
$p = new Product;
$p->where('id', '=', $request['product_id'][$i])->decrement('stock', $request['qty'][$i]);
$product = Product::where('id', '=', $request['product_id'][$i])->first();
$data[$i]['sales'] = $sales;
$data[$i]['product'] = $product;
}
Session::flash('success', 'Sale is successfully');
return redirect('/invoice')->with('data', $data);
}
Then, Make a new route -
Route::get('/invoice', function(\Illuminate\Http\Request $request){
$data = [];
if ($request->session()->has('data')) {
$data = $request->session()->get('data');
}
return view('admin.invoice.index')->with('data', $data);
});
#Sohel0415
My Sales Controller is like this.
public function index()
{
$sales = Sale::orderBy('id', 'DESC')->get();
return view('admin.sales.index', compact('sales'));
}
public function create()
{
$products = Product::pluck('name', 'id', 'qty')->toArray();
return view('admin.sales.create', compact('products'));
}
public function store(Request $request)
{
$data = array();
for ($i=0; $i < count($request['product_id']); ++$i)
{
$sales= new Sale;
$sales->product_id = $request['product_id'][$i];
$sales->qty= $request['qty'][$i];
$sales->user_id = Auth::user()->id;
$sales->save();
$product = new Product;
$product->where('id', '=', $request['product_id'][$i])->decrement('stock', $request['qty'][$i]);
$data[]['sales'] = $sales;
$data[]['product'] = $product;
}
Session::flash('success', 'Sale is successfully');
return view('admin.invoice.index')->with('data', $data);
}
admin.invoice.index
#extends('layouts.master')
#section('content')
#foreach($data as $d)
{{$d['sales']}}
{{$d['product']}}
#endforeach
#endsection
My web.php or routes:
Route::resource('categories', 'CategoriesController');
Route::resource('products', 'ProductsController');
Route::resource('sales', 'SalesController');

laravel 5 Call to undefined method Illuminate\Database\Query\Builder::posts()

i want to store this information but that doesn't work i still have this error "Call to undefined method Illuminate\Database\Query\Builder::posts()
"
public function store(Request $request)
{
$this->validate($request,[
'title'=>'required',
'slug'=>'required',
'excerpt'=>'required',
'body'=>'required',
'created_at'=>'date_format:Y-m-d H:i:s',
'team_id'=>'required',
'image'=>'mimes:jpg,jpeg,png,bmp',
]);
$data = $this->handleRequest($request);
$request->user()->posts()->create($data);
return redirect('/backend/blog')->with('message', 'Your post was created successfully!');
}
private function handleRequest($request)
{
$data = $request->all();
if ($request->hasFile('image'))
{
$image = $request->file('image');
$fileName = $image->getClientOriginalName();
$destination = $this->uploadPath;
$image->move($destination, $fileName);
$data['image'] = $fileName;
}
return $data;
}

Resources