php Laravel创建模型已经与必要的关系

klsxnrf1  于 2023-06-20  发布在  PHP
关注(0)|答案(1)|浏览(134)

我试图在数据库中保存一个模型,它需要定义一些关系,我想知道是否有其他方法可以做到这一点。
我试图保存的模式:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Download extends Model
{
    use HasFactory;
    protected $table = "tec_downloads";
    protected $primaryKey = "tdo_id";
    protected $dateFormat = "Y-m-d H:i:s";
    const CREATED_AT = "tdo_criado_em";
    const UPDATED_AT = "tdo_atualizado_em";
    protected $with = [
        "type",
        "status",
        "builder",
        "enterprise",
        "apartment",
        "house",
        "ticket",
        "requester"
    ];

    public function getIdAttribute()
    {
        return $this->tdo_id;
    }

    public function getCreatedAtAttribute()
    {
        return $this->tdo_criado_em;
    }

    public function getUpdatedAtAttribute()
    {
        return $this->tdo_atualizado_em;
    }

    public function type()
    {
        return $this->hasOne(DownloadType::class, "tdt_id", "tdt_id");
    }

    public function status()
    {
        return $this->hasOne(DownloadStatus::class, "tds_id", "tds_id");
    }

    public function builder()
    {
        return $this->hasOne(Builder::class, "con_id", "con_id");
    }

    public function enterprise()
    {
        return $this->hasOne(Enterprise::class, "emp_id", "emp_id");
    }

    public function apartment()
    {
        return $this->hasOne(Apartment::class, "uni_id", "uni_id");
    }

    public function house()
    {
        return $this->hasOne(House::class, "cas_id", "cas_id");
    }

    public function ticket()
    {
        return $this->hasOne(Ticket::class, "cha_id", "cha_id");
    }

    public function requester()
    {
        return $this->hasOne(User::class, "usr_id", "usr_id");
    }

    public function toArray()
    {
        return [
            "id" => $this->id,
            "createdAt" => $this->createdAt,
            "updatedAt" => $this->updatedAt,
            "type" => $this->type,
            "status" => $this->status,
            "builder" => $this->builder,
            "enterprise" => $this->enterprise,
            "apartment" => $this->apartment,
            "house" => $this->house,
            "ticket" => $this->ticket,
            "requester" => $this->requester,
        ];
    }
}

首先,我尝试在使用保存方法之前使用setRelation方法绑定不可空的字段,但它不起作用:

$download = new Download();
$download->setRelation("type", DownloadType::findOrFail(DownloadType::TICKET));
$download->setRelation("status", DownloadStatus::findOrFail(DownloadStatus::REQUESTED));
$download->setRelation("builder", $ticket->enterprise->builder);
$download->setRelation("enterprise", $ticket->enterprise);
$download->setRelation("ticket", $ticket);
$download->setRelation("requester", User::findOrFail($requester));
$download->save();

这将返回以下错误:
“Illuminate\Database\QueryException:SQLSTATE[23000]:违反完整性约束:1048列'tdt_id'不能为null(SQL:update tec_downloads_tipos set tdt_id =?其中tdt_id = 3)”
如果我这样做,它会起作用,但我想知道这是否是正确的方法:

$download = new Download();
$download->tdt_id = DownloadType::TICKET;
$download->tds_id = DownloadStatus::REQUESTED;
$download->con_id = $ticket->enterprise->builder->id;
$download->emp_id = $ticket->enterprise->id;
$download->cha_id = $ticket->id;
$download->usr_id = User::findOrFail($requester)->id;
$download->save();
jyztefdp

jyztefdp1#

当您保存$download模型时,框架不会考虑您设置的关系。这就是为什么你的第一个方法失败了。
你的第二种方法完全没问题。我个人更喜欢使用create方法。
如果你是框架新手,我建议你阅读Illuminate\Database\Eloquent\Model类。

相关问题