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
Related
I want to upload image using laravel 8, and I use following code:
$post = new Post;
$upload_image_name = time().'_'.$req->image_name->getClientOriginalName();
$req->image_name->move('uploads', $upload_image_name);
$post->title = $req->input('title');
$post->image_name = $upload_image_name;
$post->save();
It is working fine, but I got error Call to a member function getClientOriginalName() on null while image is empty.
It's because, suprise, image is empty while image is empty
Add condition to check uploaded image
$post = new Post;
$post->title = $req->input('title');
if($req->image_name)
$upload_image_name = time().'_'.$req->image_name->getClientOriginalName();
$req->image_name->move('uploads', $upload_image_name);
$post->image_name = $upload_image_name;
}
$post->save();
If image is required - add validation in action beginning - but right way for it - make validation in Form Request
$request->validate([
'image_name' => 'required|image',
'title' => 'required|string'
]);
If image is not required - change your migration and make posts.image_name nullable
I use following code but I don't think this is conventional way. Here is the code:
if($req->hasFile('image_name')){
$upload_image_name = time().'_'.$req->image_name->getClientOriginalName();
$req->image_name->move('uploads', $upload_image_name);
}
else
$upload_image_name = "Noimage";
$post = new Post;
$post->title = $req->input('title');
$post->image_name = $upload_image_name;
$post->save();
As I used my last laravel project.
$image = $request->file('thumbnail');
if (isset($image)) {
$imageName = date('y_m_d') . "_" . uniqid().".".$image->getClientOriginalExtension();
if (!Storage::disk('public')->exists('thumbnail')) {
Storage::disk('public')->makeDirectory('thumbnail');
}
$postImage = Image::make($image)->stream();
Storage::disk('public')->put('thumbnail/' . $imageName, $postImage);
} else {
$imageName = "";
}
$post = new Post;
$post->title = $request->title;
$post->image_name = $imageName;
$post->save();
image_name column should be nullable.
Actually I don't know how to retrieve data from db using function and query inside model .Please help me what should i add inside model function.
Here is controller
public function checkAdmin(Request $request){
$data = array();
$data['email'] = $request->email;
$data['password'] = $request->password;
$found = AdminModel::checkAdmin($data);
if($found == TRUE){
echo "found";
}
else{
echo "sorry";
}
}
Here is the Model Function
public static function checkAdmin($data){
$found = $this->where('email',$data['email'])->where('password',$data['password'])->first();
return $found;
}
What is the purpose of the checkAdmin function? Are you trying to validate a login? If yes, Laravel does this for you out of the box with the Auth::attempt method:
$credentials = $request->only('email', 'password');
if (Auth::attempt($credentials)) {
// Authentication passed...
return redirect()->intended('dashboard');
}
https://laravel.com/docs/5.7/authentication#authenticating-users
However to answer your question, you could do so like this:
$userFound = AdminModel::where(['email' => $request->email, 'password] => $request->password)->count();
if($userFound){
echo "found";
}
else{
echo "sorry";
}
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;
}
I implementing a payment gateway with flywire in laravel.
i have a method that update a data to a crm without problem, i need this data when is called to another method that make a redirect to a external url (is a wizard) the trouble is that after maked the redirection the value of sessions are lost.
How can I holding those values after redirecting to an external url?
use Session;
public function update(Request $request)
{
//code..
Session::put('value1',$request->val1);
Session::put('value2',$request->val2);
$url = 'https://www.urldemo.payment.com/payment/';
$data=[
'value1'= $request->val1,
'value2' = $request->val2,
];
$params = json_encode($data);
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,$url);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl,CURLOPT_POST,true);
curl_setopt($curl,CURLOPT_POSTFIELDS,$params);
try{
$response = curl_exec($curl);
$info = curl_getinfo($curl);
$err = curl_error($curl);
curl_close($curl);
return response()->json(['success'=>'the update was completed success','url'=> $url],200);
}catch(Exception $e){
return response()->json(['errors'=> $e->getMessage()],422);
}
}
/*This function retrieve the response data from the payment update*/
public function returnData(Request $request){
$obj = json_decode($request->getContent());
if(Session::exists('value1')){
$p1 = $obj->id;
}elseif(Session::exists('value2')){
$p2 = $obj->id;
}else{
$p1 = '';
$p2 = '';
}
$data2 = [
'value1' => $p1,
'value2' => $p2,
];
$values = json_encode($data2);
dd($values);
/*Here print the $p1 and $p2 empty thats after redirect to a external url the point is that here the session is lost*/
}
My question at this moment is how can i hold the sessions after redirect in Laravel 5.5 ?
I am making a blog. I want to concatenate the soon to be post id to the end of the post slug to make it unique but I can't find a way to access the auto increment value which was not sent through my form in request variable.
public function store(Request $request)
{
$this->validate($request, array(
'title' => 'required|max:255',
'body' => 'required'
));
$cleaned = preg_replace('/[^a-zA-Z0-9\s]/', '', $request -> title);
$cleaned = strtolower($cleaned);
$pieces = explode(" ",$cleaned);
$slug = implode("_", $pieces);
$slug = $slug."_". <------; //HERE IS THE PROBLEM
$post = new Post;
$post -> title = $request -> title;
$post -> body = $request -> body;
$post -> slug = $slug;
$post -> save();
Session::flash('success','Post Successful');
return redirect()->route('posts.show', $post->id);
}
You can't reliably get the AUTO_INCREMENT value before saving the $post.
You have to add it afterwards:
$slug = $slug."_";
$post = new Post;
[...]
$post->save();
$post->slug = $slug.$post->id;
$post->save();
The whole idea of including the id in the slug is not ideal. Maybe you should consider using a different approach.