M
M
miniven2016-04-22 10:50:10
HTML
miniven, 2016-04-22 10:50:10

Laravel: why can't I access the model?

The page has a form.
To record the entered data in the database, I need to find a specific row from the table:
This happens as follows:

$alerts_info = $request->get('alert_info');
        foreach ($alerts_info as $id => $value) {
            $alert = AlertsModel::find($id + 1);
            dd($alert);
            $alert->alert_info = $value;
            $alert->save();
        }

This code works. Through dd() I check what was written to the $alert variable.
I get this:
e78fc4b2dd134c228145f042b070ffa7.png
And everything is fine here, but there is absolutely identical code that for some reason does not want to work:
$visibility_vals = $request->get('block_visibility');
        foreach ($visibility_vals as $id => $value) {
            $visibility = VisibilityModel::find($id + 1);
            if (is_null($value)) {
                $value = 0;
            };
            //dd($visibility);
            //$visibility->visible = $value;
            //$visibility->save();
        }

Here $visibility is set to null. And even if you write find (1), for example, it will still be null.
Moreover, if you do this:
$visibility_vals = $request->get('block_visibility');
        foreach ($visibility_vals as $id => $value) {
            $visibility = VisibilityModel::All();
            if (is_null($value)) {
                $value = 0;
            };
            dd($visibility);
            $visibility->visible = $value;
            $visibility->save();
        }

That all works well.
2f01f25d4bb3420da71c4e3757028528.png
And then I can access any line through $visibility[0];
What am I doing wrong? How is it that I can access the entire table but not a particular row?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
D', 2016-04-22
@miniven

The fact that Model::all() returns data only means that there is data in the database.
If find returns null, it means that it did not find an entry with that ID.
Are there records in the database with the ID that you are trying to get?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question