When to use request in Laravel? - laravel

I am reading an article casting created_at to a string as it will return as an object by default. When to make request exactly? I know that the request can be used to transform one format to another. But when exactly? Any rules like when you create a data column you must use it? Things like that?

Using created_at as a string is not a best practice. Laravel Eloquent will automatically extract them from DB and cast them to Carbon instance, so you can transform it with a string (with ->format()) whenever you want.
Plus, having a Carbon object, you have access to lots of methods to handle your date, like comparison, extraction of single data (like day, month, hours...), so it doesn't make sense to cast it to a string, imho.

Related

Is it possible to overwrite scalar types in GraphQL?

I want an ID scalar to serialize data not as a string but rather as a Mongo Object id. The default behaviour is serializing data as a string. I don't want to create a custom scalar with another name as it may create some difficulties down the road.
The ID type is required to serialize as a string, but absolutely nothing is specified about its format and it's intended to be opaque. You could use a Mongo object ID hex string as a GraphQL ID and (at a GraphQL level) there wouldn't be any problems with this.

Why does Laravel array cast sort the elements, and can it be avoided?

I have a JSON field in a database which is populated using the array cast on an Eloquent model.
Before saving the field, Laravel sorts the elements by their key. Presumably this happens during serialisation.
Why does it do this? And is there a way to prevent it?
If you do not need to implement search by field, you can use the text type instead of the json type. Just before saving, you will need to execute json_encode, either yourself or implement a mutator.
I've never encountered this behavior before, but perhaps you can try using an accessor and a mutator to accomplish what you want. Let's say your database column is meta. You can use Laravel's special setAttribute and getAttribute methods in your model.
A mutator
For example, to json_encode data as it's about to be saved to your database it would be:
public function setMetaAttribute($value)
{
$this->attributes['meta'] = json_encode($value);
}
An accessor
To json_decode data as it's being retrieved it would be:
public function getMetaAttribute($value)
{
return json_decode($value, true);
}
The rule of thumb is take get or set and append your column name with first letters uppercased and removing any underscores, and appending Attribute to the method name. E.g. active_users column becomes getActiveUsersAttribute and so on.
Try that and see if it does any funky sorting.
Sometimes MySQL changes the order of keys when a JSON field is used.
The basic solution is to use a TEXT field to store json.
See Mysql 5.7 native json support - control keys order in json_insert function

Getting started with Bleve using BoltDB

I am trying to wrap my head around Bleve and I understand everything that is going on in the tutorials, videos and documentation. I however get very confused when I am using it on BoltDB and don't know how to start.
Say I have an existing BoltDB database called data.db populated with values of struct type Person
type Person struct {
ID int `json:"id"`
Name string `json:"name"`
Age int `json:"age"`
Sex string `json:"sex"`
}
How do I index this data so that I can do a search? How do I handle the indexing of data that will be stored in the database in the future?
Any help will be highly appreciated.
Bleve uses BoltDB as one of several backend stores and is separate from where you store your application data. To index your data in Bleve, simply add your Index:
index.Index(person.ID, person)
That index exists separately from your application data (whether it's in Bolt, Postgres, etc).
To retrieve your data, you'll need to construct a search request using bleve.NewSearchRequest(), then call Index.Search(). This will return a SearchResult which includes a Hits field where you can retrieve the ID for your object. You can use this to look up the object in your application data store.
Disclaimer: I am the author of BoltDB.
How you index your data depends on how you want to query for it.
If you want to query by any arbitrary fields, like {Age:15, Name:"Bob"} then BoltDB isn't an awesome fit for your problem.
BoltDB is simply a key value store with fast access to sequential keys and efficient prefix seeking. It's not really a replacement for general use databases.
You likely want something more like a document store (ie: MongoDB) or RDBMS (ie: PostgreSQL).
If you just wanted something that uses simple files and is embedded, you could also use SQlite with the Go module
If you want to search by only a single field, like ID or Name, then use that as the key.
If lookup speed doesn't matter at all, I guess you can use Bolt to just iterate over the entire db, parse the json and check the fields. But that's probably the worst approach you could take.

Storing a time stamp(Calendar object) with objectify

When i try to save an entity that contains with a Calendar object i get the following error.
'class java.util.GregorianCalendar' is not a registered #Subclass"
I want to be able to store a time stamp with my entity. Storing the time as a string works but id like to keep it as an object if possible.
Is there something specific i need to do to be able to store a Calendar object with objectify?
If i can not store a Calendar object, what is the best alternative for storing a timestamp with objectify(hopefully using standard java classes)?
java.util.Date is a good choice.

Convert Session Object to IOrderedQueryable<T>

I need to convert a Session object to an IOrderedQueryable and came up blank. I've thought of creating a wrapper, but its not working properly. Basically, I am pulling a Linq query and would like to store it so that I don't have to pull it each time I visit. There are up to 7-10 parameters per user so it's not something that's great for caching.
I can simply cast my Session object as an IOrderedQueryable like:
(IOrderedQueryable<T>)Session["myObject"];
It seems you want to store the data returned by the linq query, if that's the case you need to make it grab the data i.e. by using .ToList() and storing that.

Resources