Hi i am trying to get the valid json response but i am not getting what i want my response is:
{
"status": "true",
"message": "Data Found!",
"data": {
"message_id": 25,
"sender_id": 15,
"body": "kaleem",
"subject": "Test",
"senderName": "Tester Techleadz",
"created_at": "2016-06-23 00:46:55"
}
}
we can see the message_id and sender_id are not in quotes as other values are in quotes i want to get the message_id and sender_id in quotes as well
My controller code is:
$id = $request->input('id');
$type = $request->input('type');
if($type == 'trash'){
$trash = MessagesRelationModel::leftjoin('messages','messages.id','=','messages_relation.message_id')
->leftjoin('users','users.id','=','messages.sender_id')
->leftjoin('contacts','users.contacts_id','=','contacts.id')
->where('messages.id' , $id)
->select('messages_relation.message_id','messages.sender_id','messages.body','messages.subject',DB::raw('CONCAT(firstname, " ", lastname) As senderName'),'messages.created_at')
->first();
if(count($trash) > 0)
{
$resultArray = ['status' => 'true', 'message' => 'Data Found!', 'data' => $trash];
return Response::json( $resultArray, 200);
}
}
else{
$resultArray = ['status' => 'false', 'message' => 'No Data Found'];
return Response::json($resultArray, 200);
}
}
how can i solve my problem in controller i used the select clause in controller any help will be highly appreciated!!!
This is a valid json, message_id and sender_id are integers, so that the are not in quoted, You can use them easily...
Related
I got a query that gets the data into a collection, problem is that it shows foreign id but i want it to display what i have given in the url parameters.
columnsGiven is the parameter from url. contains column names with child: "language.name". so column=active,title,language.name
For example i get this:
"name": "george",
"active": 1,
"language": 1,
and this is what i want:
"name": "george",
"active": 1,
"language": "Dutch",
this is my code:
public function index(Request $request){
$columnsGiven = explode(',', $request->columns);
$tableName = $request->table_name; //example: 'support_guide_translations'
$modelName = $request->model_name; //example: "App\Models\SupportGuideTranslation";
if($request->search){
$query = $modelName::search($request->search);
} else{
$query = $modelName::query();
if($request->sort){
$sort = explode('?', $request->sort);
$query->orderBy($sort[0], $sort[1]);
}
}
foreach ($request->query() as $key => $value) {
// dd($value);
if(!$value == ""){
if(Schema::hasColumn($tableName, $key)){
$query->where($key, $value);
}
if(in_array($key, $columnsGiven)){
dd('true');
}
// $searchWord = Str::contains('account123',$request->search);
}
}
$guides = $query->get();
return GuideResource::collection($guides);
}
}
this is GuideResource, it sends data to vue by making it json first. Not allowed to make changes here, it has to be done in the function index. :
public function toArray($request)
{
return [
'id' => $this->id,
'title' => $this->title,
'body' => $this->body,
'active' => $this->active,
'language' => $this->language,
'support_guide' => $this->support_guide,
'support_guide_group' => $this->support_guide_group,
];
}
"language": {
"id": 1,
"name": "Dutch",
"code": "NL",
"created_at": "2021-06-14T10:10:32.000000Z",
"updated_at": "2021-06-14T10:10:32.000000Z"
},
I believe "language" has a relationship with another model.
Isn't this what you are looking for?
return [
// your other attributes...
'language' => $this->language->relationshipName->name,
];
where relationshipName is the method name in the Language model.
For testing reasons, I want to make the following Post Request with the Laravel HTTP Client:
$test = Http::post(route('users.leads.store', ['user' => $user->id]), [
"company_name" => "TESTCOMPANY",
"zip" => "49685",
"city" => "BÜHREN",
"street" => "MÜHLENKAMP 3",
"contact_name" => "FABIANLUKASSEN",
"phone1" => "017691443785",
"email" => "FABIANLUKASSEN#TESTEN.DE",
"website" => "www.fabianlukassen.de",
"category" => "Hotel",
"closed_until" => now(),
"appointment_end" => now()->addDays(1),
"appointment_comment" => "HALLO ICH BIN FABIAN",
"additional_contacts" => "",
"phone2" => "",
"sub_category" => "",
"expert_status" => 0
]);
I know that the route is working just fine. However, with debugging in phpStorm, I can see that the $test variable contains a 419 error (unknown status). Does anyone know what's wrong?
(I'm using laravel 8)
I agree with #ElektaKode that the issue is likely due to lack of csrf token.
In order to disable CSRF middleware while testing,
switch off CSRF token for this route at /app/Http/Midddleware/VerifyCsrfToken.php, by updating:
protected $except = [ 'your-route-url' ];
Then you can use api authentication to follow it up.
The simplest way to use api authentication, follow this doc,
The other ways are either using Laravel passport or using jwt for api.(both will consume more time to set up, as you are using for testing using api authentication is your go to method.)
Usually in Laravel, 419 Page Expired error comes from CSRF middleware meaning there was a failure while validating CSRF token. Add your CSRF token to your test request or consider disabling CSRF middleware while testing.
Post Request with Laravels HTTP Client
$test = Http::post(route('users.leads.store', ['user' => $user->id]), [
"company_name" => "TESTCOMPANY",
"place_id" => null,
"street" => "MÜHLENKAMP 3",
"zip" => "49685",
"city" => "BÜHREN",
"title" => null,
"contact_name" => "FABIANLUKASSEN",
"additional_contacts" => null,
"phone1" => "+49 163 3006603",
"phone2" => null,
"email" => "FABIANLUKASSEN#TESTEN.DE",
"category" => "Hotel",
"sub_category" => null,
"website" => "www.fabianlukassen.de",
"status" => 1,
"expert_status" => 0,
"coordinates" => null,
"expert_id" => 1,
"agent_id" => null,
"blocked" => 0,
"important_note" => null,
]);
Route
Route::apiResource('users.leads', UserLeadController::class);
Store Method in the UserLeadController
public function store(User $user, CreateLeadRequest $request)
{
//TODO: Relocate validation to request class
if(!UserLeadController::isPhone("test", $request->phone1)) {
abort(400, "Keine gültige Telefonnummer!");
return;
}
if(!UserLeadController::isPhoneNumberUnique("test", $request->phone1)) {
abort(400, "Die Telefonnummer existiert bereits!");
return;
}
/**
* The logged in User
* #var User $agent
*/
$agent = Auth::user();
$phoneUtil = PhoneNumberUtil::getInstance();
$lead = new Lead();
$lead->fill($request->except(['appointment_end', 'appointment_comment']));
// Leads created by experts will be blocked
if ($user->id === $agent->id) {
$lead->blocked = true;
}
$numberProto = $phoneUtil->parse($lead->phone1, 'DE');
$lead->phone1 = $phoneUtil->format($numberProto, PhoneNumberFormat::INTERNATIONAL);
try {
$lead->save();
} catch (QueryException $e) {
//$message = 'Lead besteht bereits.';
//return Response::json(['errors' => $message], 422);
abort(422, "Lead besteht bereits!");
return;
}
if ($request->closed_until) {
$lead->closed_until = Carbon::create($request->closed_until);
$event_end = $request->appointment_end
? Carbon::parse($request->appointment_end)
: Carbon::parse($request->closed_until)->addMinutes(90);
$lead->calendarEvents()->save(new CalendarEvent([
'body' => $request->appointment_comment ?? "Wurde von {$this->roleDescriptor($agent->roles)}" . $agent->name . " angelegt.",
'type' => CalendarEventType::CALLCENTER_APPOINTMENT,
'event_begin' => $lead->closed_until,
'event_end' => $event_end,
]));
$lead->status = LeadState::APPOINTMENT;
$lead->expert_status = LeadExpertAcceptance::ACCEPTED;
} else {
$lead->status = LeadState::OPEN;
}
if (isset($request->agent)) {
$lead->agent_id = $request->agent;
}
try {
$user->leads()->save($lead);
$lead->comments()->save(new Comment([
'body' => "Wurde von {$this->roleDescriptor($agent->roles)}" . $agent->name . " angelegt.",
'user_id' => $agent->id,
'commentable_type' => 'lead',
'commentable_id' => $lead->id,
'reason' => 'CREATED',
'date' => now('Europe/Berlin'),
]));
if ($request->closed_until) {
$lead->comments()->save(new Comment([
'body' => "Termin wurde von {$this->roleDescriptor($agent->roles)}" . $agent->name . " vereinbart.",
'user_id' => $agent->id,
'commentable_type' => 'lead',
'commentable_id' => $lead->id,
'reason' => 'APPOINTMENT',
'date' => now('Europe/Berlin')->addMinute(),
]));
}
} catch (QueryException $e) {
//not sure if this works
$message = $e->getMessage();
abort(400, $message);
return;
}
if (empty($message)) {
return Response::json(['message' => 'Lead saved', 'lead' => new LeadSingleResource($lead)]);
} else {
return Response::json(compact('message'), 500);
}
}
//TODO: relocate function to rule object
protected static function isPhoneNumberUnique($attribute, $value) {
$withSpace = PhoneFormatter::formatInternational($value);
$withoutSpace = preg_replace('/ /', '', $withSpace);
$protos = [$withSpace, $withoutSpace]; // Necessary because legacy (25.06.2020).
$booleanTest = Company::query()->whereIn('phone', $protos)->doesntExist()
|| Lead::query()->whereIn('phone1', $protos)->orWhereIn('phone2', $protos)->doesntExist();
return $booleanTest;
}
//TODO: relocate function to rule object
protected static function isPhone($attribute, $value) {
if (!$value) {
return false;
}
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
$test = $phoneUtil->isValidNumber($phoneUtil->parse($value, 'DE'));
return $test;
}
fillable variable in the Lead Model
protected $fillable = [
'company_name',
'place_id',
'street',
'zip',
'city',
'title',
'contact_name',
'additional_contacts',
'phone1',
'phone2',
'email',
'category',
'sub_category',
'website',
'status',
'expert_status',
'coordinates',
'expert_id',
'agent_id',
'blocked',
'important_note'
];
As mentioned before, I receive a 200 OK status. Also, in a Vue.js component, I have done the following axios post request, which also just works fine.
axios
.post(`/api/users/${this.user_id}/leads`, {
"company_name": this.companyName,
"zip": this.zipCode,
"city": this.city,
"street": this.streetAndHouseNumber,
"contact_name": this.contactPartner,
"phone1": this.contactPartnerPhoneNumber,
"email": this.contactPartnerEmail,
"website": this.website,
"category": this.category,
"closed_until": this.appointmentStart,
"appointment_end": this.appointmentEnd,
"appointment_comment": this.comment,
//not used but needed (don't know the purpose)
"additional_contacts": "",
"phone2": "",
"sub_category": "",
"expert_status":this.expert_status,
}).then(() => {
window.location.href = this.routeSuccess;
}).catch((error) => {
this.showErrorAlert = true;
this.errorAlertMessage = error.response.data.message;
});
}
I'm integrating Apple Pay on our website using payfort.
Payfort ask to calculate signature which work fine if input values are string. But the problem is what if value is array.
As shown in following image from there documentation. They require apple_header and apple_paymentMethod field to be List data type. Now how should i calculate the signature in this case since it takes key and value as string. but in our case value is array. (i.e. apple_paymentMethod AND apple_paymentMethod)
I tried these 2 fields using json_encode function.
If i send these 2 fields as string then payfort api return invalid format for field: apple_header.
If i send these 2 fields as array then payfort api return Signature Mismatch.
I don't have any idea what am i missing.
Here is my code:
Controller Code:
$parameters = [
'digital_wallet' => 'APPLE_PAY',
'command' => 'PURCHASE',
'access_code' => config('payfort.APPLE_ACCESS_CODE'),
'merchant_identifier' => config('payfort.APPLE_MERCHANT_IDENTIFIER'),
'merchant_reference' => date( 'Y' ).str_pad($request->iOrderId, 6, 0, STR_PAD_LEFT) . '_' . time(),
'amount' => 7000,
'currency' => 'SAR',
'language' => 'en',
'customer_email' => 'test#gmail.com',
'apple_data' => $request->token['paymentData']['data'],
'apple_signature' => $request->token['paymentData']['signature'],
'apple_header' => json_encode([
'apple_transactionId'=> $request->token['paymentData']['header']['transactionId'],
'apple_ephemeralPublicKey'=> $request->token['paymentData']['header']['ephemeralPublicKey'],
'apple_publicKeyHash'=> $request->token['paymentData']['header']['publicKeyHash']
]),
'apple_paymentMethod' => json_encode([
'apple_displayName'=> $request->token['paymentMethod']['displayName'],
'apple_network'=> $request->token['paymentMethod']['network'],
'apple_type'=> $request->token['paymentMethod']['type']
]),
'customer_ip' => $request->ip(),
'customer_name' => 'ABC',
'merchant_extra' => $request->iOrderId,
'merchant_extra1' => $request->order_number,
'merchant_extra2' => $request->locale_info,
];
// canculate signature
$parameters['signature'] = $this->payfortCoreHelpers->calculateSignature($parameters, 'request', true);
// Add Array or List fields in back to parameters before send
$parameters['apple_header'] = [
'apple_transactionId'=> $request->token['paymentData']['header']['transactionId'],
'apple_ephemeralPublicKey'=> $request->token['paymentData']['header']['ephemeralPublicKey'],
'apple_publicKeyHash'=> $request->token['paymentData']['header']['publicKeyHash']
];
$parameters['apple_paymentMethod'] = [
'apple_displayName'=> $request->token['paymentMethod']['displayName'],
'apple_network'=> $request->token['paymentMethod']['network'],
'apple_type'=> $request->token['paymentMethod']['type']
];
calculateSignature function
public function calculateSignature($arrData, $signType = 'request', $isAppleRequest = false)
{
$shaString = '';
ksort($arrData);
foreach ($arrData as $k => $v) {
$shaString .= "$k=$v";
}
$shaString = config('payfort.SHA_REQUEST_PHRASE') . $shaString . config('payfort.SHA_REQUEST_PHRASE');
$signature = hash(config('payfort.SHA_TYPE'), $shaString);
return $signature;
}
Payfort Response
{
"amount": "7000",
"response_code": "00008",
"digital_wallet": "APPLE_PAY",
"signature": "****",
"merchant_identifier": "****",
"access_code": "****",
"customer_ip": "::1",
"language": "en",
"command": "PURCHASE",
"merchant_extra": "****",
"response_message": "Signature mismatch",
"merchant_reference": "20201599817035025",
"customer_email": "test#gmail.com",
"merchant_extra1": "1599817035025",
"merchant_extra2": "SAR",
"currency": "SAR",
"customer_name": "ABC",
"status": "00"
}
You might find this link helpful: https://github.com/devinweb/payment/blob/fa595fe60df4dff3c4f2189e37fd64fffdc8421b/src/Traits/Payfort/ApplePay.php#L51
This shows calculateSignature method that generates the signature for your request.
Particularly, line 67 shows that you need to have a comma followed by space between the items in the nested array.
ksort($arrData);
foreach ($arrData as $key => $value) {
if(is_array($value)){
$shaSubString = '{';
foreach ($value as $k => $v) {
$shaSubString .= "$k=$v, ";
}
$shaSubString = substr($shaSubString, 0, -2).'}';
$shaString .= "$key=$shaSubString";
}else{
$shaString .= "$key=$value";
}
}
I have a function that asks for a value per parameter from a table.When the request is made there are some values that are in the table that is not reading them, while others are.All of them are in the table. So it shouldn't give null.
function:
public function showMarketByGroup($idGroup)
{
$response = array('code' => 400, 'error_msg' => []);
try {
$karateka = Karateka::find($idGroup);
if (!empty($karateka)) {
$karatekasInMarket = $karateka->karatekasByGroupInMarket;
return $response = array('code' => 200, 'karatekas' => $karatekasInMarket, 'msg' => 'Get all karatekas by group in market');
} else {
return $response = array('code' => 401, 'error_msg' => 'Unautorized');
}
} catch (\Exception $exception) {
$response = array('code' => 500, 'error_msg' => $exception->getMessage());
}
return response($response, $response['code']);
}
This is the table where I am referencing:
When the column of the id_group has a value of 3 or 4, the answer is correct.But with the other values it enters an exception in Postman:
{
"code": 401,
"error_msg": "Unautorized"
}
I am making a web service in Laravel which is returning JSON.
I have created an Account model like so:
class Account extends Eloquent {
// The database table used by the model.
// (If not defined then lowercase and plural of class name is consider as a table name)
protected $table = "account";
// define which column can be mass assign
protected $fillable = array("user_id", "account_group_id", "generated_by", "image", "name",
"address", "zip", "area_id", "mobile", "email", "phone", "fax",
"website", "pan", "cst", "tin", "ecc", "iesc", "transport",
"other", "outstanding", "cform", "status", "mitp");
// To prevent column from mass assignment.
protected $guarded = array('id');
// Change Variable for CREATED_AT and UPDATED_AT
const CREATED_AT = 'itp';
const UPDATED_AT = 'utp';
}
I am fetching fields from Account using user_id and returning JSON via Response::json() in my controller
$accountData = Account::select('name', 'status', 'id', 'user_id', 'utp')->where('user_id', Auth::id())->first();
$return = array(
'result' => 'success',
'msg' => 'Login Successfully.',
'data' => $accountData
);
return Response::json($return);
In this, utp behaves as expected and returns a date as a string:
{
"result": "success",
"msg": "Login Successfully.",
"data": {
"name": "Demo",
"status": 0,
"id": 143,
"user_id": 207,
"utp": "2015-07-01 18:38:01"
}
}
However if I take each value separately from the account model like so:
$return = array(
'result' => 'success',
'msg' => 'Login Successfully.',
'data' => $accountData['user_id'],
'account_id' => $accountData['id'],
'utp' => $accountData['utp'],
'usertype' => 'account',
'status' => $accountData['status']
);
Then this gives some unexpected behavior from utp
{
"result": "success",
"msg": "Login Successfully.",
"data": 207,
"account_id": 143,
"utp": {
"date": "2015-07-01 18:38:01",
"timezone_type": 3,
"timezone": "Asia\\/Kolkata"
},
"usertype": "account",
"status": 0
}
Why does this happen with my timestamp field?
Because utp is a Carbon\Carbon instance. Model::toJson (actually Model::toArray, but both are used) handles that usually, and serializes a date to it's usual ISO3601-ish format
For expected behavior, you need to format the Carbon instance.
"utp" => $accountData['utp']->format("Y-m-d H:i:s"),
Alternatively, cast it to a string
"utp" => (string) $accountData['utp'],