Passing json from db to blade template - arrays

Let say I have ["gravy1", "gravy2"] in table's column "gravy". How can I display it so I can get this result:
This is my controller:
$grav = DB::table('items')
->select('gravy')
->get();
$gravies = json_decode($grav);
return view('welcome', [
'gravies' => $gravies,
]);

Related

How to get checkbox checked from database array using Laravel and Ajax

I'm trying to get data from database to checkboxes as checked. The checkbox data in database are as array that have been inserted with json and they are dynamic data based on insert function.
My tasks table:
id |employee_id | startDate | endDate | ...
---|------------|------------|-----------|--------
1 |["1","2"] | .......... | ..........| ....
My TasksController.php
function fetchdata(Request $request)
{
$id = $request->input('id');
$task = Task::find($id);
$output = array(
'employee_id' => $task->employee_id,
'name' => $task->name,
'description' => $task->description,
'startDate' => $task->startDate,
'endDate' => $task->endDate,
'percentage' => $task->percentage
);
echo json_encode($output);
}
public function getEmployeesList()
{
$employeesList = Employee::all();
return view('adminlte::tasks', ['employeesList' => $employeesList]);
}
My tasks.blade.php
#foreach($employeesList as $emplist)
<label class="checkbox-inline">
<input type="checkbox" id="employee_id" name="employee_id[]" class="chck" value="{{$emplist->id}}" >{{ $emplist->name }}</label>
#endforeach
My Ajax function inside blade:
$(document).on('click', '.edit', function(){
var id = $(this).attr("id");
$('#form_output').html('');
$.ajax({
url: "{{route('tasks.fetchdata')}}",
method: 'get',
data: {id:id},
dataType: 'json',
success:function(data)
{
$('#id').val(data.id);
$('#employee_id').val(data.employee_id);
$('#name').val(data.name);
.......................
..................
}
})
});
So, how can I retrieve data from database to checkboxes as checked, because for now I'm getting null "employee_id" value when I try to update a record.
Thank you in advance
Since you're encoding the data on the server side, you must decode it in the client side like :
...
success:function(data)
{
console.log( data );
data = JSON.parse(data);
$('#id').val(data.id);
$('#employee_id').val(data.employee_id);
$('#name').val(data.name);
...
}

How to sort ng-repeat key value pair getting from firebase

i new to angularJS i am getting key value pair from firebase how to sort by timeStamp to show latest at top.
please help me . thanks in advance, orderBy and reverse not working
var ref = new Firebase("https://firebaseio.com/notifications/");
var fb = $firebase(ref);
var syncArreglo = fb.$asObject();
syncArreglo.$loaded().then(function() {
angular.forEach(syncArreglo, function (value, key) {
if(syncArreglo.value != null){
syncArreglo.value.firebaseKey = syncArreglo.key;
}
});
});
syncArreglo.$bindTo($scope,'chats');
<ul class="menu" ng-repeat="(key , value) in chats">
Let say you have charts json as:
$scope.chats = [
{item: 'a', created : '2016-12-01', num :1},
{item: 'b', created : '2015-12-01' , num :4},
{item: 'c', created : '2018-12-01', num :5},
];
To sort item , you need to provide filter orderBy : sortItem : true as in
<ul ng-repeat="(key, value) in chats | orderBy : sortItem : true">
<li>{{value}} :: {{value.created}}</li>
</ul>
Your controller will have function sortitem which will tell the sorting item function
Conrtoller
$scope.sortItem = function(d) {
var dateing = new Date(d.created);
return dateing;
};
Play around jsfiddle : http://jsfiddle.net/agjqN/688/

How to save nested objects in my Firebase db

I have created a view for adding categories and a view for products. I want to add products to categories. This is working but it would probably be more efficient to have it structed like below instead of what I've got in the screenshot.
products
- KXbMe8zo14TyOYDJh4q
cat: {
id: "-KXXJY-i7Avnqw8IMqR", name: "Starters"
}
Would this structure be better and how should I implement it? I'm expecting the whole cat object to be passed in as I'm referening product.cat as the model.
If I set the value in the form element to be {{cat}} I get this in the DB:
"{\"name\":\"Kebabs\",\"$id\":\"-KXXJl0Ice17zDIYF_Xt\",\"$priority\":null}"
Does this look its correct?
<select ng-model="product.cat">
<option ng-repeat="cat in cats" value="{{cat.$id}}">{{cat.name}}</option>
</select>
I'm retrieving the categories like so:
var cats = $firebaseArray(firebase.database().ref('category'));
cats.$loaded().then(function(){
$scope.cats = cats;
});
I've ended up with the following:
var cats = $firebaseArray(firebase.database().ref('category'));
cats.$loaded().then(function(){
$scope.cats = cats;
});
$scope.addProduct = function() {
console.log($scope.product);
var category = {};
for(var i=0; i<$scope.cats.length; i++){
if($scope.cats[i].$id == $scope.product.catid){
category.id = $scope.cats[i].$id;
category.name = $scope.cats[i].name;
}
}
//var record = firebase.database().ref().child('products/category').push($scope.product.cat);
//var record = firebase.database().ref().child('products').push($scope.product);
var record = firebase.database().ref().child('products').push({
'category' : {id: category.id, name: category.name},
'name' : $scope.product.name,
'price' : $scope.product.price
});
if(record.key){
$scope.productSuccess = true;
} else {
$scope.productSuccess = false;
}
};
This gives me the following in my DB:

Symfony2. When using querybuilder on entity with foreign keys, how to return values of foreign keys as sequence of ids?

I have query in my repository, that returns array result:
$result = $qb
->select('partial u.{id, username, email, lastLogin}', 'partial groups.{id}')
->leftJoin('u.groups', 'groups')
->getQuery()
->getArrayResult();
When I requests this query on my AngularJS app, I getting next result:
[
{"id":1,
"username":"Sergio Ivanuzzo",
"email":"my_email#domain.com",
"lastLogin":null,
"groups":[{"id":1}]
}
]
Because of groups contains array of objects, I can't parse them in my editable directive (checkboxes not checked):
<span
data-pk="{{ user.id }}"
editable-checklist="user.groups"
e-ng-options="g.id as g.name for g in groups"
e-form="rowform">
{{ User.showGroups(user) }}
</span>
When groups attribute contains something like [1,2,3] directive is working (checkboxes are checked normally).
So, for fixing this I'm parsing each User object from response:
angular.forEach(response.data, function(user) {
var groups = [];
angular.forEach(user.groups, function(group) {
groups.push(group.id);
});
user.groups = groups;
});
$scope.users = response.data;
I feel, that I can avoid this workaround, If I could return groups for each user as sequence of numbers. How can I do this? Maybe better way exists?
If you need extra data, please, let me know! Thanks a lot for any help
You can use a filter to preserve your original data like
$scope.toArray = function(groupsAsObjects) {
var groups = [];
angular.forEach(groupsAsObjects, function(group) {
groups.push(group.id);
});
return groups;
};
And
<span
data-pk="{{ user.id }}"
editable-checklist="user.groups | toArray"
e-ng-options="g.id as g.name for g in groups"
e-form="rowform">
{{ User.showGroups(user) }}
</span>
Or remap your full array of objects as arrays like this :
$scope.toArray = function(obj, addKey) {
if (!obj)
return obj;
if (addKey === false) {
return Object.keys(obj).map(function(key) {
return obj[key];
});
} else {
return Object.keys(obj).map(function(key) {
return Object.defineProperty(obj[key], '$key', {
enumerable: false,
configurable: true,
value: key
});
});
}
};

ui-sref not working when generate dynamic content for datatable columns

I'd to link a column of my data-table to a dynamic Angularjs view.
Table 1 :
ID | Name | Action
1 | Jack | Edit
Edit should be a link to #/clients/1/edit
/clients/:id/edit (app.client_edit) is already created and it's working.
I'm trying the code below:
$scope.dataTableOpt = {
"columnDefs": [
{
"targets": 0,
"render": function ( data ) {
return "<a ui-sref=\"app.client_view({id: $row[0]})\">Edit</a>";
}
}
],
'ajax': 'http://localhost/admin/clients'
};
Given result:
Link1 = < a ui-sref="app.client_view({'id': '1'})">edit</ a>
Expected result:
Link2 = < a ui-sref="app.client_view({id: '1'})" class="ng-scope" href="#!/client/2">edit</ a>
When I put < a ui-sref="app.client_view({'id': '1'})">test< / a> on the page statically it's working but not sure why it doesn't work when it's dynamically generated.
Please advise.
my way:
DataTable set options:
"columnDefs": [
{
"targets": [0, 1],
"render": function ( data, type, row ) {
var href = $compile('<a ui-sref="stateName({id: ' + $stateParams.id + '})"></a>')($scope)[0].href;
return '' + data + '';
}
}
]
#Abbas Moazzemi
If Use .withOption('createdRow' as below, you dont need to use $compile:
.withOption('createdRow', function(row) {
// Recompiling so we can bind Angular directive to the DT
$compile(angular.element(row).contents())($scope);
})

Resources