z-song / laravel-admin

Build a full-featured administrative interface in ten minutes
https://laravel-admin.org
MIT License
11.12k stars 2.81k forks source link

hasMany表单值为空 #5527

Open 354651432 opened 2 years ago

354651432 commented 2 years ago

Description:

hasMany表单提交以后的url ,desc 两个字段都为空

{
    "_id": 5,
    "text": "fds",
    "images": [
        {
            "id": 4,
            "article_id": 5,
            "created_at": "2022-02-22T09:56:21.000000Z",
            "updated_at": "2022-02-22T09:56:21.000000Z",
            "url": null,
            "desc": null,
        }
    ],
},

Steps To Reproduce:

form代码

class ArticleController extends AdminController
{
    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Article());

        $form->textarea('text', __('Text'))->required();
//        $form->multipleImage('imgs')->sortable();
        $form->hasMany("images", function (Form\NestedForm $form1) {
            $form1->image("url");
            $form1->text("desc");
        });

        return $form;
    }
}

models 代码

class Article extends Model
{
    use HasFactory;
    public $fillable = ["*"];

    public function images()
    {
        return $this->hasMany(Image::class, "article_id", "id");
    }

    public function toArray()
    {
        return [
            "_id" => $this->id,
            "text" => $this->text,
            "images" => $this->images,
        ];
    }
}

class Image extends Model
{
    use HasFactory;

    protected $fillable = ["*"];
}
joe94113 commented 2 years ago

加上belongsTo關聯會有效嗎 DOC

class Image extends Model
{
    use HasFactory;

    protected $fillable = ["*"];

    public function article()
    {
        return $this->belongsTo(Article::class, 'article_id');
    }
}