I have a JSON field which I am experimenting with but I am having a bit of trouble with it.
I have added the following to my Customer model:
protected $casts = [
'billingConfig' => 'array'
];
And I updated a test field using the following in my controller:
$customer->billingConfig = ['attachableType' => $request->attachmentsConfig];
$customer->save();
After this, the following appears in my database:
{"attachableType": "combined"}
Now, when I go to grab this specific value through my blade:
{{$customer->billingConfig->attachableType}}
I get "Trying to get property 'attachableType' of non-object"
But when I use the below:
{{$customer->billingConfig['attachableType']}}
I get the "combined" value I was looking for.
I was using this guide: https://www.qcode.in/use-mysql-json-field-in-laravel/, and I guess I wanted to make sure I was doing everything right and their method was wrong or I had goofed up somewhere.
JSON data fields are being read as an array, which you can not call a property for, that's what I believe the reason for the error you got when called
{{$customer->billingConfig->attachableType}}
because it's an array, not a data object (like the case in javascript).
Thanks for sharing.
Related
I have an array of post codes coming from an input:
$postCodes = collect(["BK13TV", "BK14TV", "BK15TV", "BK16TV"]);
In my database I already have two of the post codes - "BK13TV", "BK16TV".
So I would like to run something like this:
$postCodeModels = PostCode::findManyOrCreate($postCodes->map($postCode) {
return ['code' => $postCode]
})
My initial approach was to load all the post codes, then diff them against the postCodes from the input like so:
PostCode::createMany($postCodes->diff(PostCode::all()->pluck('code')))
However, here it means that I am loading the entire content of post_codes table, which just seems wrong.
In the ideal case, this would return all post code models matching the passed post codes as well as would create entries for post codes that did not exist in the database.
First I need to retrieve existing postcodes:
$existingPostCodes = PostCode::whereIn('code', $postCodes)->get();
The find all the post codes in the input, that are not stored yet in database:
$newPostCodes = $postCodes->diff($existingPostCode->pluck('code'));
Finally retrieve all the new post codes as models:
$postCodeModels = PostCode::whereIn('code', $postCodes)->get();
Admittedly, this still takes three queries, but does eliminate the crazy stuff of loading an entire table worth of data.
I was wondering if there is a way to cast a field that comes in through the JsonLoader(). This is basically what I want but it doesn't work.
Person = LOAD 'people' USING JsonLoader() AS (name:chararray)
I haven't tried with JsonLoader, but i have faced a similar situation with HCatLoader.
There i did the casting in the second line.
Person_tmp = LOAD 'people' USING JsonLoader();
Person = FOREACH Person_tmp GENERATE name:chararray;
Just try it out. It might work out.
Well, this scenario may be quite familiar but I couldn't find the solution.
Scenario:
I am making a REST API call through tFileFetch and I get a json out of it. I parse it to get paging token and more result through tflowtoiterate. Now if more result is equals true, I have to call the same tFileFetch component to get the new set of json using new pagination result.
I have to loop through tFileFetch until the 'more result' is false.
My approach:
Access token-pagination-tFileFetch_1->JSON->tflowtoIterate->more result=true->IF [moreresult=true]->tFileFetch_2->JSON->tFlowtoIterate->more result=true->tLOOP [moreresult.equals{true)->tFileFetch_2->
After tFileFetch2 I have used tSetGlobalVar to put pagination as common var to pass to tFileFetch2
I am not sure whether this approach is appreciable or not, please suggest any improvements if any?
I have actually covered it by myself. 'code' Add SubJob Ok from tLoop to tFileFetch and add condition in tLoop which say until your condition matches 'more result' is false. Finally added delimited output with append mode
I am designing a Web Application using Django Framework. I have written the model code, urls.py and view code which can be seen Here.
I have added some data into the database table. But when I try to access the object using the code below, it just shows bookInfo objects five times. I don't think I am successful enough in pulling the data from the database. Kindly help.
View
def showbooks(request):
booklist = bookInfo.objects.order_by('Name')[:10]
output = ','.join([str(id) for id in booklist])
return HttpResponse(output)
You are iterating through the object list, you just need to reference the column/attribute you want:
output = ','.join([obj.id for obj in booklist])
Alternatively you can more more finely craft you original db call, then the iterable you use will work. In this case we'll pull out a list of the 'id' attribute.
booklist = bookInfo.objects.order_by('Name').values_list('id', flat=True)[:10]
output = ','.join([id for id in booklist])
I think you are successful in pulling the data. It is just that booklist contains objects, not numeric ids. You can add __unicode__ method to you class BookInfo that is supposed to return a string representation of the object (probably book name in this case). This method is going to be invoked when str() is applied. You can find more info about __unicode__ here.
I am working on a Dynamic data.
after creating a dynamic model and registering in global.asax, like
DefaultModel.RegisterContext(typeof(masterEntities1),new ContextConfiguration() { ScaffoldAllTables = true });
when i run an application, it shows a list of tables but when i click any of the table it throws an exception:
The method 'Skip' is only supported for sorted input in LINQ to Entities. The method 'OrderBy' must be called before the method 'Skip'.
but i haven't declare any query into my application.
You must call .OrderBy' on your query if you use the .Skip method. For example if you were using something similar to the following:
results = results.Skip(pageNumber * size).Take(size);
In the case above you would have previously had to use the .OrderBy to order the query if you are planning on using paging methods or something of the like. If you have an Id field, adding this onto your original query expression should eliminate the error:
.OrderBy(x => x.Id);