How can I get the values of a BRED in another Controller

mikemastercorp

Aug 5th, 2022 01:43 PM

Hello again, guys. Sorry if I bombard you with lots of questions, however I got stuck and I would really appreciate some hints on this one.

So long story short, for my calendar events project I've created a table, named: occupation_types which contains just two fields:

$table->id();
$table->string('title');
$table->string('color');
$table->timestamps();

I've created a BREAD for it too and added a few entries.

From a modal submit form, I am pulling the occupation title as values of a select box so that the user can pick one of the desired occupational statuses. During submit via Ajax, I am sending a request to my controller where everything works fine, but I need to extract the color based on the occupation_id returned from the AJAX post as ID.

In my EventController I am pulling using a query to filter the object that matches the selected ID using:

$occupations = OccupationType::where('id', $request->occupation_id)->get();

This returns the whole object with all the values, however as far as I need to extract the $occupations->title and occupations->color, it returns an error:

[2022-08-05 20:13:21] local.ERROR: Property [color] does not exist on this collection instance. {"userId":1,"exception":"[object] (Exception(code: 0): Property [color] does not exist on this collection instance. at C:\\OpenServer\\domains\
ewwave.lar\\vendor\\laravel\\framework\\src\\Illuminate\\Collections\\Traits\\EnumeratesValues.php:968)
[stacktrace]

Would you help letting me know why it won't return the object value when in the java console, if i log response.color, it returns the value correctly?

How should I properly build the query, so that the title and color values can get extracted and saved to the DB?

mikemastercorp

Aug 5th, 2022 02:25 PM

Forgot to mention that if I return $occupations, where

$occupations = OccupationType::where('id', $request->occupation_id)->get();

I am able to view the proper object returning with the correct values, however they are not called by $object->title or color

OCCUPATION-TYPES-JS-CONSOLE.png

thinkverse

Aug 5th, 2022 02:58 PM

To make it easier for people to read code, please use codeblocks by surrounding your code with three backticks and an optional language. 👍

```php
// Your code.
```
Report
1
mikemastercorp

Aug 5th, 2022 03:01 PM

Hey Thinkverse, I wanted to do that and tried inserting code through the insert options here, but it was disappearing. So it seems the platform uses the well-known SLACK sort of coding, so will gladly use it in the future and will correct my earlier post to apply the cool styling.

mikemastercorp

Aug 8th, 2022 08:07 AM

I've found a way to go around that by pushing the color as the value of my dropdown select box and using the title as the text to show. This way, since the Ajax post was correct, I extracted the values of the selection value and text and pushed them to the controller.

I know that this is a workaround so I would still like to learn how can I obtain the values of an object in a controller because if I push the object to the view, I am fully able to extract all the values but not if I use it in a controller.

What am I missing this time? :)

cassiolacerda

Aug 9th, 2022 05:18 PM

Hello mikemastercorp!

To get the property of an object you can use $object['property'];

Try this:

$occupations = OccupationType::where('id', $request->occupation_id)->get(); 

$occupation_color = $occupations[0]['color'];

This way you will save the occupation color in the $occupation_color variable.

Report
1
mikemastercorp

Aug 11th, 2022 03:23 PM

Hello @cassiolacerda.

Thank you very much for your short and sweet answer. I will definitely test it upon next suitable case, as I've just completed my calendar full functionality of the calendar events and as I mentioned, I just used the controller to manipulate the data, but not to get and return through AJAX.

As I said, it is working fine now, but I would love to test your suggestion and make sure it works well as I am starting my next module (personal messages) and I would definitely need more interaction with controllers.