Laravel 5.2 - Output to a view - output

I have a simple method that passes info to a view:
Route::get('/', function () {
$thissite = DB::table('this_site')->where('id',1)->get();
$slider1 = DB::table('front_sliders')->where('active',1)->take(10)->get();
return view('index')->with('slider1',$slider1)->with('site', $thissite );
});
All the data is passed OK and the $thissite is just one record, with one of the fields being called headline.
My problem is outputting this single variable along the lines of:
<h1><strong>{{ headline }}</strong></h1>
I have tried many variations on this but I am not getting anywhere!

So headline is a column in this_site? If so then the this should work:
<h1><strong>{{ $site->headline }}</strong></h1>

This worked:
<h1><strong>{{ $site[0]->headline }}</strong></h1>

Related

Updating Data within a unique randomly generated ID/KEY in firebase using HTML

function updateFirebase(){
const fb=firebase.database().ref()
//get field values
author = document.getElementById('uname').value
user_email = document.getElementById('umail').value
data = {author, user_email}
//update database
fb.child('Article/').update(data);
}
</script>
I have problem with my code. I want to update the data inside a table named "Article". Article has generated items with a unique key/id and each key has its own content. Lets say I want to be able to edit the "author" or change the "title", the problem is they each have a randomly generated key/id that I cant access. for example that "-LS39kReBHrKGqNj7h_". I can only save the data inside the "Article" tree but I cant change the "author" or the "title". How do i get a workaround this so I can change those properties?
Here is how my firebase looks like
It depends whether you have the record reference on the frontend before update or not (whether you have fetched it before you are trying to update it).
But generally, you have two options
You can store the key reference as an "id" field on the object.
To achieve that, you need two step process when creating the record at the first place
// Creates a new record in DB and returns it to you. Now you can get the "key"
const newRecord = firebase.database().ref('TABLE_NAME_REF').push();
newRecord.set({
id: newRecord.key
...
});
This is great if you fetch the list of records on the frontend and then you want to update one of them. Then you can just build the ref path like this
fb.child('Article/' + record.id ).update(data); // where record is the prefetched thing
You need to find the element based on its fields first. And once you have it, you can update it right away.
To achieve this, you can simply do something like:
firebase.database()
.ref('TABLE_NAME_REF') // let's say 'Article'
.orderByChild('RECORD_KEY') // Let's say 'author'
.equalTo('KEY_VALUE') // let's say 'zoranm'
.limitToFirst(1)
.once("value")
.then(res => {
// You need to loop, it always returns an array
res.forEach(record => {
console.log(record.key); // Here you get access to the "key"
fb.child('Article/' + record.key ).update(data); // This is your code pasted here
})
})

Pushing Multiple values into array gives error using angular6

I am working in angular 6 here i want to push multiple values into array which gives me the following error
here is my code
this._model.NomineeList.push(
{
'FirstName': this._nomineemodel.FirstName,
'CNIC': this._nomineemodel.CNIC,
'MiddleName': this._nomineemodel.MiddleName,
'LandlineNumber': this._nomineemodel.LandlineNumber,
'LastName': this._nomineemodel.LastName,
'MobileNumber': this._nomineemodel.MobileNumber,
'PermanentAddress': this._nomineemodel.PermanentAddress,
'PresentAddress': this._nomineemodel.PresentAddress,
'RelationId': this._nomineemodel.RelationId,
'RelationName': this._nomineemodel.RelationName,
'UPermanentAddress': '',
'UPresentAddress': ''
});
How to push into array using angular 6.
The error is self explanatory, the kind on model you are inserting in your array they are not identical. there are two possible reasons.
1) Either you haven't initiated the array with empty values. or
2) The model you are inserting is missing mandatory properties or the new model having few extra properties or spell mistake in properly name.
Check this stackblitz example.

Vuejs changes order of json_encoded array, when decodes it back from props in vuejs component

Php:
$json_string = "{
"26":{"blabla":123,"group_id":1,"from":"27.08.2018","to":"02.09.2018"},
"25":{"blabla":124,"group_id":1,"from":"20.08.2018","to":"26.08.2018"},
"24":{"blabla":125,"group_id":1,"from":"20.08.2018","to":"26.08.2018"}
}"
my.blade.php template:
<my-component :records={{ $json_string }}></my-component>
MyComponent.vue:
export default {
props: ['records'],
...
mounted: function () {
console.log(this.records);
}
Output is:
{__ob__: Observer}
24:(...)
25:(...)
26:(...)
And when I use v-for, records in my table in wrong order (like in console.log output).
What I am doing wrong?
EDIT:
I figured out 1 thing:
When I do json_encode on collection where indexes are from 0 till x, than json string is: [{some data}, {some data}]
But if I do ->get()->keyBy('id') (laravel) and than json_encode, json string is:
{ "26":{some data}, "25":{some data}, "24":{some data} }
Then how I understood, issue is in different outer brackets.
In Javascript keys of objects have no order. If you need a specific order then use arrays.
Here is documentation for keyBy Laravel method: https://laravel.com/docs/5.6/collections#method-keyby
I wanted to have ids for rows data to fast and easy access without iterating over all rows and check if there is somewhere key Id which is equals with my particular Id.
Solution: not to use keyBy method in Laravel and pass json string to Vue component like following [{some data}, {some data}] (as I described in my Question Edit section) - this will remain array order as it used to be.
I found this short and elegant way how to do this, instead of writing js function by myself:
Its find() method: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
Example:
let row = records.find( record => record.id === idToFind );

Laravel: Searching in MySQL database with items from Form::textarea

I'm trying to query a MySQL database for items coming from a textarea. My controller's store() function has the following code:
// [...] validation is done here and assumed ok
foreach(explode("\n", Input::get('itemlist')) as $line) {
$item = preg_split('/(?<=\d) (?=[a-z])|(?<=[a-z])(?=\d)/i', $line);
if (isset($item[1])) {
echo 'Looking for ' . $item[1];
$itemObj = DB::table('items')->select('name', 'id')->where('name', '=', trim($item[1], "\r"))->first();
var_dump($itemObj);
}
}
The data received from Input::get('itemlist') is a list of items and amount, something like:
5 apples
2 oranges
1 banana
The Looking for ... part displays the item names properly. Also, the var_dump will actually show the proper result for the last thing searched for, everything else is just 'null'.
Is there something about DB:table that stops from doing queries in a loop like this? Should I do this some other way? Thanks!
EDIT Turns out there were trailing '\r' after each line except the last one so the query was for "Some Item\r" and not "Some Item". Fixed it above!

Filtering and rearranging model/content in Ember Controllers

Let's say I have a JSON array of data, something like:
[ {"name":"parijat","age":28},
{"name":"paul","age":28},
{"name":"steven","age"27},
...
]
that is part of my model, and this model is setup like this:
App.UserRoute = Ember.Route.extend({
model:function(){
return App.User.FIXTURES ; // defined above
}
});
I want to get the unique ages from this JSON array and display them in my template, so I reference the computed properties article and read up a little on how to enumerate Ember Enumerables, to eventually get this:
App.UserController = Ember.ArrayController.extend({
ages:function(){
var data = this.get('content');
var ages = data.filter(function(item){
return item.age;
});
}.property('content');
});
Now the above piece of code for controller is not correct but the problem is that it doesn't even go into data.filter() method if I add a console.log statements inside. IMO, it should typically be console logging as many times as there exist a App.Users.FIXTURE. I tried using property('content.#each') which did not work either. Nor did changing this.get('content') to this.get('content.#each') or this.get('content.#each').toArray() {spat an error}.
Not sure what to do here or what I am completely missing.
Filter is used for reducing the number of items, not for mapping.
You can use data.mapBy('age') to get an array of your ages:
ages:function(){
return this.get('content').mapBy('age');
}.property('content.#each')
or in your handlebars function you can just use the each helper:
{{#each item in controller}}
{{item.age}}
{{/each}}