This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

Paul
Paul

Hi, i'm trying to get my code to work, im sure im close but am missing one or 2 things...

I have a model 'area' with a jsonable field called 'spots'. On the backend i have the spots working using a repeater to fill the database. Now i want visitors to be able to add a new spot to the database using a component. The data from the form is putted to the database using the code below.

  • If there was no 'spot' yet i get the information but the [ and ] around the text are missing.
  • If there was a spot it is overwriting the existing text.

Can you advice what should be done different?
The most important code used to generate the json string and write it to the database:

//get area  
$areadetails = Areas::where('slug', '=', $slug)->first();  
// copy the spots to variable   
$original = $areadetails->spots;
// generate the array like json   
$array = array("PublishedSpot"=>0, "spot_description"=>$data['description'], "spot_name"=>$data['spotName']);
// replace original value with the new values (there is problem 2, i need to add spot to existing ones)   
$original = $array;   
// copy back and save to database.
$areadetails->spots = $original;
$areadetails->save();

What is saved using above code:
{"PublishedSpot":0,"spot_description":"<p>Introduction of the spot<\/p>\r\n","spot_name":"wddadw"}
This is not showing in the repeater untill i manualy add [] around it.

Thanks a lot!

mjauvin
mjauvin

Did you add the spots field name to the model's $jsonable array?

Paul
Paul

I'm not completely sure what you mean with the 'spots field name'? Should there be a sort of identifier? This shows a working jsonable array from the database with more then 1 record:

[{"PublishedSpot":"1","spot_description":"Parking, best to use for sector Brumleytal and ","spot_name":"Parking Dorenther Klippen"},
{"PublishedSpot":"1","spot_description":"Plisseetal, \r\nCoordinate is not checked yet, please contact us to make it better.","spot_name":"Plisseetal"},
{"PublishedSpot":"1","spot_description":"Dreikaiserstuhl","spot_name":"Dreikaiserstuhl"}]
mjauvin
mjauvin

You'd have to share your model, field definition and actual ajax handler so I have a better idea of what's going on

Paul
Paul

Hi, i put the files on the web, its quite a lot to copy in this post. https://atacamaweb.nl/october/Areas.php.txt https://atacamaweb.nl/october/columns.yaml.txt https://atacamaweb.nl/october/fields.yaml.txt If usefull i can also share the whole plugin..

Thanks a lot already for trying to understand me ;-)

mjauvin
mjauvin

Where is $data coming from in your first post? Can you share the whole function?

Also, did you try to json_encode your spots data before assigning it to the model?

Paul
Paul

The whole plugin can be found here: https://atacamaweb.nl/october/areafinder.zip ( i will remove this file end of today) The strings showed in the posts before (where did i mention $data?) are copied from the database. The working example (with [ ]) is generated by the repeater in the backend. I have been playing with the json_encode, at that moment it returned a lot of quotes mainly. I will have a try with that in 2 moments.

mjauvin
mjauvin

I would personally create a spots model and add a $hasMany relation to the Area model. That would be a lot easier to manage.

Paul
Paul

The json_encode() does not help, it is only adding a lot of quotes ;-) When i started with october i started also with a model for the area's. But i did not find a solution at that moment to add the spot from the area backend. But that is another subject.

Im surprised it is so hard to do what i want... sad, maybe i should first start old-fasioned with a manual copy from e-mail and find a better solution after that. @Mjauvin, thanks for the help. @others, if you know the answer please tell me ;-)

mjauvin
mjauvin

Oh, I'm sure it's just a small detail to solve, that should work... I just didn't have time to look at your plugin (I'm browsing your code on my phone at the moment) I'll take a better look when I'm back home.

mjauvin
mjauvin

Ok, after looking at your SpotAdd component, I understand what the problem is... you're trying to assign an array to the Model's spots field... but it should be an array of arrays

mjauvin
mjauvin

so use this:

$areadetails->spots = array($originial);

But since you probably want to add new spots, try this:

$spot->spot_description = Input::get('description');
$spot->spot_name = Input::get('spotName');
$spot->spot_coordinate = Input::get('spotCoordinate');
$spot->spot_type = Input::get('spotType');
$spot->spot_coordinatedd = Input::get('spotCoordinatedd');

$areadetails->spots = array_merge($areadetails->spots, $spot);
Paul
Paul

I have it working! Thanks a lot of helping me to the right direction.

For the people with simular problems, now i got it working with code above. Only creating an array i do this way:
'$spot = [ ['PublishedSpot' => '0', 'spot_description' => Input::get('description')]];'

And for saving i do a check on the $areadetails->spots to see it is filled or not using an if(is_array()). If not you cannot merge it ofcource. This works, thanks!

1-13 of 13

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.