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

henryjevans26521
henryjevans26521

Hey,

So i'm rather stuck on something conceptually here and I've tried so many things but I just can't grasp it. What is the best way for a component to "know" what to query? I keep getting stuck with tying to do queries, because I don't know how the component knows the ID of the Record i'm using to perform the query (for matching things.)

At the moment, for example - I have a Parent > child model via a hasmany relationship, so I can say "on the parents page grab the slug from the URL and then query the record that matches that to bring up the children"

Is there a better way of doing this? One way i've thought to try but can't figure out is to pass the variable through my components properties (like the builderdetails plugin does) But I'm not sure how you would make that pass anything beyond a string/interger, is it just using that column name to perform a query?

In this case, what i'm actually wanting to do is, is on childs page (called in with recorddetails component) is get the Other children associated with the child in focusses parent, but I don't understand how I can do this with a custom component, is it able to access the variables that the recorddetails component has?

Basically, i'm really struggling to connect the front and backend up outside of using the recorddetails and recordslist plugin, without getting the Slug from the URL, which i'm not sure if it's a really good way of doing things.

Thanks.

witul
witul

I am not sure what you are trying to do exactly, but you can call component with other component's properties.

Here is main record for which you want to show details. You cant get it's id from slug.

{% component 'recorddetails' %}

Now your component is available via recorddetails variable in twig. variable name is the same as alias of component. Then you can call list component like this:

{% component 'recordlists' records=recorddetails.getRelated() %}

getRelated is some method of main record component for which you want to get related items.

Now you can use $this->property('records') inside your recordlists onRender method.

henryjevans26521
henryjevans26521

So essentially, what i'm wanting to do now is:

I want it so when somebody goes onto a page whilst logged in, My components can grab the ID and then perform a query, so as to bring up the data related to that record. e.g. User with ID 6 logs in > User goes onto the page and it shows them records from a another model which are joined via a hasMany/hasOne relationship.

I know how to do the latter and perform the query to get the records...it's just making it such by the correct (currently logged in users) Id that i'm confused with, as my component obviously needs to know the parameter it is querying by.

With this:

{% component 'recordlists' records=recorddetails.getRelated() %}

Could one theoretically pass the users model (with the array of logged in users attributes) onto my component which performs the query, then use the ID value from the users.id attribute to perform said query so as to call on the relevant records?

BG78
BG78

Can't you use 'Auth::getUser()->id' in your controller to get the currently logged in user?

henryjevans26521
henryjevans26521

"Can't you use 'Auth::getUser()->id' in your controller to get the currently logged in user?"

see I knew something like this had to exist...but I just didn't know what it was called. Yeah, that's literally the exact thing I was after.

1-5 of 5

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