NgAdmin list all laravel - php

I am using NgAdmin with Laravel to create a restAPI with an Admin structure using Angular.
Everything was okay with all the methods in the Controllers but then I needed to get all items in some specif table (with the results not paginated);
public function index()
{
try{
$statusCode = 200;
$builder = \ApiHandler::parseMultiple($this->place, array('latitude,longitude'), $this->passParams('places'));
$places = $builder->getResult();
$response = [];
foreach($places as $place){
$response[] = [
'id' => $place->id,
'latitude' => $place->latitude,
'longitude' => $place->longitude,
'updated_at' => $place->updated_at
];
}
return $this->makeResponse($response,$statusCode, $builder);
}catch (\Exception $e){
$statusCode = 500;
$message = $e->getMessage();
return \Response::json($message, $statusCode);
}
}
The thing is that my $builder always give me a limit of 315 items and paginates the rest.
Have you guys faced the same problem?
All I want is to get all items.

Related

Retrieve data from external API and pass multiple data to my view

In my controller i have this function
public function GetStatusDetails()
{
$response = Http::get('https://exemple.exemple.com/fr/api/<token>/availability/<Id>/getStatusDetails?format=json');
$StatusDetails = json_decode($response->body(), true);
//dd($data);
return view('ControlmAPI.netvigie', [
'StatusDetails' => $StatusDetails
]);
}
public function GetStatus()
{
$response = Http::get('https://exemple.exemple.com/fr/api/<token>/availability/<Id>/getStatus?format=json');
$Status = json_decode($response->body(), true);
//dd($data);
return view('ControlmAPI.netvigie', [
'Status' => $Status
]);
}
Is not the same call api but when i want to use StatusDetails in my blade i can't but Status i can so my question is how to pass multiple data to my blade and use it separately.
the dd of them is DD so in my blade i do {{$Status[0]['status']}} it work but when i want to do for "StatusDetails" it doesn't but if i do only for "StatusDetails" it works but not for both someone have the solution please ?
You can simply pass them as an array
public function GetStatus()
{
$statusResponse = Http::get('https://exemple.exemple.com/fr/api/<token>/availability/<Id>/getStatus?format=json');
$statusDetailsResponse = Http::get('https://exemple.exemple.com/fr/api/<token>/availability/<Id>/getStatusDetails?format=json');
$Status = json_decode($statusResponse->body(), true);
$StatusDetails = json_decode($statusDetailsResponse->body(), true);
return view('ControlmAPI.netvigie', [
'Status' => $Status,
'StatusDetails' => $StatusDetails,
]);
}

How to add and update in one api in laravel

I am trying to add and update using the same API, currently, I can add but I am not clear about how to update using the same API.
I am adding folders against id and my body response looks like this:
{
"id": "2",
"folder_detail": [1,3,4]
}
I can add folders with id 1,3 and 4 against id 2 but next time when I hit the same API with folder[1,3,5] it should update folders details not should add again, I can do that by making separate API but I want to do that in one API.
My Controller code:
try {
$folder = Doctor::where('id', $request->get('id'))->first();
$folder->doctor()->attach($request->get('folder_detail', []));
DB::commit();
return response([
'status' => true,
'message' => 'Folder detail added',
], 200);
} catch (\Exception $ex) {
DB::rollback();
return response([
'status' => false,
'message' => __('messages.validation_errors'),
'errors' => $ex->getMessage(),
], 500);
}
}
public function doctor()
{
return $this->belongsToMany('App\Folder', 'folder_details');
}
Your help will be highly appreciated?
Okay so after our back and forth in the comments I think you are looking for something like this:
$folders = $request->get('folder_detail', []);
foreach($folders as $folder) {
$record = Folder::firstOrNew(['id', $folder]);
$record->doctor_id = $request->id;
// You can add your own records here, or use the 'fill' function
}
So, this way, you loop through all your folders. Check if the folder with the specific ID already exists, if it does not, it creates a new one. The doctor is linked through the doctor_id on your record.
Find record if exist then update otherwise create
$post = $request->all();
$doctor = Doctor::find($post['id']);
if($doctor){
$doctor->update([
//your data for update doctor model
]);
//remove old folders which is related with this doctor
Folder::where('doctor_id', $doctor->id)->delete();
//add current data of folder
if(!empty($post['folder_detail'])){
foreach($post['folder_detail'] as $folder){
Folder::create([
'doctor_id' => $doctor->id,
'folder_detail' => $folder
]);
}
}
//return your response here
} else {
$doctor = Doctor::create([
//data for create doctore
]);
//add current data of folder
if(!empty($post['folder_detail'])){
foreach($post['folder_detail'] as $folder){
Folder::create([
'doctor_id' => $doctor->id,
'folder_detail' => $folder
]);
}
}
//return your response here
}

How to set multi select value from array object in yii2 while updating

I have table which have multiple reference to ohter tables like
user
id name email
categories
id title
user_categories
user_id category_id
Here a user will have multiple category associated with him/her
I am able to save these successfully with new records like following
View File:
echo $form->field($package_categories, 'category_id')->dropDownList( ArrayHelper::map(
StudyMaterialCategories::find()->all(), 'id', 'title'),
['multiple' => true]
);
Save New record:
$model = new Packages();
$package_categories = new PackageCategories();
$request = Yii::$app->request;
if ($request->isPost) {
$transaction = Yii::$app->db->beginTransaction();
try {
$post = $request->post();
$model->load($post);
$model->save();
foreach ($post['PackageCategories']['category_id'] as $key => $value) {
$package_categories = new PackageCategories();
$package_categories->category_id = $value;
$package_categories->package_id = $model->id;
$package_categories->save();
}
$transaction->commit();
return $this->redirect(['view', 'id' => $model->id]);
} catch (Exception $ex) {
$transaction->rolback();
Yii::$app->session->setFlash("error", $ex->getMessage());
}
}
Till now It's running successfully.
But I'm stuck when going to update the table. The problem part is dropdown list. How to set multiple selected option as per database if I'm coming with array of object.
Have a look on the following code
$package_categories = PackageCategories::find()
->where('package_id=:package_id', ['package_id' => $id])->all();
if (count($package_categories) < 1) {
$package_categories = new PackageCategories();
}
$request = Yii::$app->request;
if ($request->isPost) {
$transaction = Yii::$app->db->beginTransaction();
try {
$post = $request->post();
$model->load($post);
$model->save();
$package_categories = new PackageCategories();
$package_categories->deleteAll(
"package_id=:package_id",
[':package_id' => $model->id]
);
foreach ($post['PackageCategories']['category_id'] as $key => $value) {
$package_categories = new PackageCategories();
$package_categories->category_id = $value;
$package_categories->package_id = $model->id;
$package_categories->save();
}
$transaction->commit();
return $this->redirect(['view', 'id' => $model->id]);
} catch (Exception $ex) {
$transaction->rolback();
Yii::$app->session->setFlash("error", $ex->getMessage());
}
}
if I try to get first object of the array $package_categories of only able to set selected one option
This is an example code of a model class Permit which has a many to many relationship with Activity through PermitActivity (pivot table model).
Model Class Activity
public class Permit extends \yii\db\ActiveRecord {
public $activities_ids;
...
public function rules() {
return [
...
[['activities_ids'], 'safe'],
...
];
}
...
// Method called after record is saved, be it insert or update.
public function afterSave($insert, $changedAttributes) {
// If this is not a new record, unlink all records related through relationship 'activities'
if(!$this->isNewRecord) {
// We unlink all related records from the 'activities' relationship.
$this->unlinkAll('activities', true);
// NOTE: because this is a many to many relationship, we send 'true' as second parameter
// so the records in the pivot table are deleted. However on a one to many relationship
// if we send true, this method will delete the records on the related table. Because of this,
// send false on one to many relationships if you don't want the related records deleted.
}
foreach($this->activities_ids as $activity_id) {
// Find and link every model from the array of ids we got from the user.
$activity = Activity::findOne($activity_id);
$this->link('activities', $activity);
}
parent::afterSave($insert, $changedAttributes);
}
...
// Declare relationship with Activity through the pivot table permitActivity
public function getActivities(){
return $this->hasMany(Activitiy::className(), ['id' => 'activity_id'])
->viaTable('permitActivity',['permit_id' => 'id']);
}
...
public function afterFind(){
parent::afterFind();
$this->activities_id = ArrayHelper::getColumn($this->activities, 'id');
}
}
This way the model class is the one responsible for creating and updating the relationship using the pivot table.
The most important thing is to have the relationship method declared correctly.
Edit
This is an example of the view using kartikv\widgets\Select2. I don't really know if dropDownList supports multiple select, however Select2 has so many useful features i usually use it over other options.
echo $form->field($model, 'activities')->widget(Select2::classname(), [
'data' => $data,
'options' => [
'placeholder' => '...'
],
'pluginOptions' => [
'allowClear' => true,
'multiple' => true,
],
]);

Joomla assign data to view

I am working on Joomla 3.6 and I am very new in joomla. I am fetching the data from an api and I want to pass that data to a view. How can I do that.
Controler: user.php
public function profile() {
$wskey = sdafsda;
$companycode = 'sdafsd';
$client = 1;
$cardno = 'sdafsd';
$pin = 'sdaf';
$wsdl = 'http://example/service.asmx?wsdl';
$getdata = array(
'WSKey' => $wskey,
'CompanyCode' => $companycode,
'CardNo' => $this->EncryptData($cardno),
'Client' => $client,
'PIN' => $this->EncryptData($pin),
);
$soapClient = new SoapClient($wsdl);
try {
$result = $soapClient->GetProfile($getdata);
} catch (Exception $e) {
return $e;
}
}
And view is created in com_users->views->cprofile.
I want to show this data in default.php of cprofile views and Want to know how can I call a view with data.
Sorry might not be clear.

Does Laravel5 transaction work or not if I create method instead of using eloquent or query builder in my controller method

I'm so concern about my operation to used Mysql statement like update, delete, insert and within Laravel Eloquent or Query builder because I've create more Mysql statement with more conditional inside of my method in Controller so if one of that conditional or something occurs or meet any problem during I'm processing my method in controller Method I will lost my information or data my data which I want to insert or update to my database.
As below function I used setNotification() which I create in Notification Model and I have method inside that Model which I want to call it to post_data is a method in Controller so if I do so does DB::beginTransaction() will work or not because I prefer to keep all Mysql statement inside all method in Model.
now I currently using Laravel Transaction with Try catch
public function post_data() {
if (Request::ajax() && $this->CheckPermId_from_session(90)) {
$res = null;
$data = Request::except(['_token']);
$rules = [//rules varray];
$data = [//Input data];
$data['tranx_time'] = date("y-m-d H:m:s", time());
$val = Validator::make($data, $rules);
if ($val->fails()) {
$res = ['res' => false, 'form' => false, 'data', $data];
} else {
DB::beginTransaction();
try {
//$update = Teller::where('id', '=', Request::input('teller_till_id'))->update(array('balance' => Request::input('tell_balance')));
$updateTeller = Teller::where('id', '=', Request::input('chief_till_id'))->update(array('balance' => Request::input('last_chief_balance')));
$insertId = DB::table('till_transaction')->insertGetId($data);
if ($insertId && $updateTeller) {
$this->notification->setNotification([$data['to_account'], json_encode($this->group_code), $insertId, Request::input('chief_till_id'), $data['type'], date("Y-m-d H:m:s", time()), $data['type']]);
$res = ['res' => true, 'data', $data];
} else {
$res = ['res' => false, 'data', $data];
}
DB::commit();
} catch (Exception $e) {
DB::rollBack();
throw $e;
}
}
return $res;
}
}

Categories