Jump to content

Capsule and retrieving data from db


Walther

Recommended Posts

About db managing using capsule I googled around quite deeply, but was able to found just trivial examples...  so I have an issue I can't find a way to fix.

 So, i.e., I understand that with this:

$domain5 = Capsule::table('tbldomains')->where('status', '=', 'Active')->select('id', 'domain')->first();

I can retrieve from db the first active domain, and with  $domain5-> id I can retrieve its id , and so on...

And with:

$domain5 = Capsule::table('tbldomains')->where('status', '=', 'Active')->select('id', 'domain')->get();

I should retrieve all the active domains but... how to retrieve the data of every single domain?

I tried (unsuccessfully) to var_export($domain5) and var_dump($domain5) to analyze the structure and understand how to retrieve single fields of every record, but it doesn't work... 

So, what are the right methods to do it?! 🤔

 

Edited by Walther
Link to comment
Share on other sites

Hi Walther,

You are only selecting the id and domain for each row where the domain is active, if you want all data you can use

$domain5 = Capsule::table('tbldomains')->where('status', 'Active')->get();

You can also use the Domain model to achieve this

$domain5 = Domain::where('status', 'Active')->get();

Ensure to import at the top of the file

use WHMCS\Domain\Domain;

 

Link to comment
Share on other sites

For docs on the Domain model, check: https://classdocs.whmcs.com/8.6/WHMCS/Domain/Domain.html .  Change version to version in use for that specific one.  Using capsule directly will not produce the model and just the direct database data for the domains table.   You will get essentially the same info but in some cases the Model will have different variable names then the database column names.   So using one over the other depends on a few factors. 

Link to comment
Share on other sites

49 minutes ago, steven99 said:

For docs on the Domain model, check: https://classdocs.whmcs.com/8.6/WHMCS/Domain/Domain.html .  Change version to version in use for that specific one.  Using capsule directly will not produce the model and just the direct database data for the domains table.   You will get essentially the same info but in some cases the Model will have different variable names then the database column names.   So using one over the other depends on a few factors. 

Yes, I offered the Model as an alternative just in case he wants to further expand it and grab client details, order details, etc... without the hassle of using joins.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated