'Can someone explain these lines of Laravel code? [closed]

I am completely new to Laravel and I have been given a project I need to edit. So I have been tracing through the project trying to understand what it is doing. I have read a lot of the docs and I have run across something I don't fully understand.

Can someone help explain these lines of code:

$account = Account::find($account_id);
$settings = $account->accountSettings;

So specifically the: Account::find($account_id); and the $account->accountSettings;

Obviously, you aren't going to know what the variables contain and stuff, I am not asking for that. I am asking strictly for understanding WHAT is happening specifically.



Solution 1:[1]

$account = Account::find($account_id);

This line means SELECT * FROM account WHERE id = $account_id LIMIT 1 in mysql. This will return the whole row in a single object. Another example would be:

$account = Account::where('id', $account_id)->get();

This line will means SELECT * FROM account WHERE id = $account_id in mysql. This will return all rows with id = account_id in an array of objects.

$settings = $account->accountSettings;

This line means assigning the value of $account->accountSettings to $settings. So for example IF:

$account->accountSettings has a value of "foo"

$settings will have a value of "foo" too

the 'accountSettings' in $account->accountSettings could mean a column in the table of 'account', or it is simply a properties of 'account'.

Solution 2:[2]

The first line tells laravel to look into accounts table and get the line where the id is equal to $account_id then bind it to an object ($account), and the second line is to get accountSettings column value inside $settings variable

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2