@artem_atlas

Trying to get property 'id' of non-object. Как исправить?

При создании связки создается ui, но не возвращает значение id.
Model test:
class test extends Model
{
    use HasFactory;

    protected $guarded = [];

    public function ui_name() {
        return $this->belongsTo('App\Models\Uis', 'name_id', 'id');
    }

    public function ui_desc() {
        return $this->belongsTo('App\Models\Uis', 'desc_id', 'id');
    }
}

UI Model:
class Uis extends Model
{
    use HasFactory;

    protected $guarded = [];

    public function category() {
        return $this->hasOne('App\Models\Categories\Category', 'ui_id', 'id');
    }

    public function tag() {
        return $this->hasOne('App\Models\Tags\Tag', 'ui_id', 'id');
    }

    public function test_name() {
        return $this->hasOne('App\Models\Tests\test', 'name_id', 'id');
    }

    public function test_desc() {
        return $this->hasOne('App\Models\Tests\test', 'desc_id', 'id');
    }
}

Controller:
$test = new test();
        $test->ui_name()->create([
            'text' => "TEXT",
            'desc'=> "TEXT",
            'module_id' =>1, 
            'isHTML' => false
        ]);

tests table:
Schema::create('tests', function (Blueprint $table) {
            $table->id();

            $table->bigInteger('name_id');
            $table->bigInteger('desc_id')->nullable();
            $table->integer('max_ball')->default(20);
            $table->string('password')->nullable();
            $table->string('special_link')->unique()->nullable();
            $table->timestamps();
        });

uis table:
$table->id();
            $table->string('text');
            $table->text('desc');
            $table->bigInteger('module_id')->nullable();
            $table->boolean('isHTML')->default(false);
            $table->timestamps();

Warning:

ErrorException
Trying to get property 'id' of non-object
  • Вопрос задан
  • 528 просмотров
Пригласить эксперта
Ответы на вопрос 1
@Kostik_1993
Web Developer
$test = new test();
Вы создали объект, но не сохранили его в базу. А так как вы этого не сделали, то и свойства id у вас нет
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы