J
J
jazzus2019-01-05 17:30:01
PHP
jazzus, 2019-01-05 17:30:01

Where are the fields when creating a new object?

I want to do like this:

$company = new Company;
               $company['country_id'] = (new Country)->getId('russia');

               return response()->json($company);

And you have to do this:
$company = new Company;
               $company['name'] = null;
               $company['website'] = null;
               $company['country_id'] = (new Country)->getId('russia');
               $company['city_id'] = null;
               $company['other_country'] = null;
               $company['other_code'] = null;
               $company['other_city'] = null;
               $company['address'] = null;
               $company['phone'] = null;

               return response()->json($company);

Because in the first case it passes an empty object, and I need fields for the date in json. It is not clear then what is the point of new Сompany, then I can simply do $company = {};
Maybe something needs to be added to new Сompany so that Laravel automatically creates fields from the table in the object?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
neol, 2016-03-30
@AstonMartin

$handle = fopen($dir . $minus, 'r');
while (($res = fgets($handle)) != FALSE) {
    if ($res != 0) {
        $array_minus[trim($res)] = true; // <- this
    } else {
        break;
    }
}
fclose($handle);
// сравнение
foreach ($array_source as $value) {
    if (!isset($array_minus[$value])) { // <- this
        $array_result[] = $value;
    }
}

file_put_contents("result.txt", implode(PHP_EOL, $array_result));

Two important lines are marked with comments.
On two files of 150k lines, it worked for me in 0.333s
PS array_diff, proposed by Denis Akimkin , is much better in readability with comparable performance, so it's better to use it.

D
DarkMatter, 2016-03-30
@darkmatter

No way. This is php you need either something like HHVM or php7 or another programming language

A
Antonio Solo, 2019-01-05
@solotony

the fields of the object "name" and others are implemented through magic, and in fact the object does not have them.
why don't you use
$company->toJson();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question