Laravel Voyager 覆盖 BREAD 控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44576648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:09:03  来源:igfitidea点击:

Laravel Voyager overwrite BREAD controller

phplaravelvoyager

提问by Pedro

I've started to use Voyager and I've problems with a controller.

我已经开始使用 Voyager,但我在使用控制器时遇到了问题。

I've create a new table in database called Progress, voyager by default create a BREAD form to browse read delete and add items .

我在数据库中创建了一个名为 Progress 的新表,默认情况下 voyager 创建一个 BREAD 表单来浏览读取删除和添加项目。

I like to put a default value into a field when the user go to add a new item. The default value that I like to put is authentication user_id

我喜欢在用户添加新项目时将默认值放入字段中。我喜欢放置的默认值是 authentication user_id

How can I do that?

我怎样才能做到这一点?

Thanks.

谢谢。

回答by Theson

You can do that completely outside of Voyager.

您可以完全在 Voyager 之外执行此操作。

First exclude the authentication_user_idfrom the Add form (in Voyager's database interface). If the field doesn't take a null value you can set some temporary default, or modify your migrations - whichever is most convenient.

首先authentication_user_id从添加表单(在 Voyager 的数据库界面中)中排除。如果该字段不采用空值,您可以设置一些临时默认值,或修改您的迁移 - 以最方便的方式为准。

Next create a model observerand then utilise the created()function. For example:

接下来创建一个模型观察者,然后利用该created()功能。例如:

<?php
    namespace App\Observers;

    use App\Models\Project;

    class ProgressObserver
    {
        /**
         * Listen to the Progress created event.
         *
         * @param  Progress  $progress
         * @return void
         */
         public function created(Progress $progress)
         {
            $progress->authentication_user_id = WHATEVER_YOU_DO_HERE;
            $progress->save();
         }

    }

回答by Abhinav Verma

You can do that by creating a model for your bread, as shown in image enter image description here

您可以通过为面包创建模型来做到这一点,如图所示 在此处输入图片说明

after you have done creating a model for your bread you can create a function named save

为面包创建模型后,您可以创建一个名为的函数 save

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use TCG\Voyager\Traits\Translatable;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;
use Auth;

class MyViewModel extends Model
{
    //
    protected $table = "my_view";

    public function save(array $options = [])
    {
        $this->user_id = \Auth::user()->id;
        parent::save();
    }
}

now whenever you save any record in administration of voyager, you will see current login user id is getting saved in your database table.

现在,每当您在 voyager 管理中保存任何记录时,您都会看到当前登录用户 ID 被保存在您的数据库表中。

回答by Apit John Ismail

I think you can create a migration to write a default value for that field

我认为您可以创建迁移来为该字段写入默认值

回答by Niket Joshi

You have to add following code into the modal like this,

您必须像这样将以下代码添加到模态中,

//assign created_by loggedin user
 public function __construct(array $attributes = [])
{
    $this->creating([$this, 'onCreating']);
    parent::__construct($attributes);
}

public function onCreating($row)
{
    // Placeholder for catching any exceptions
    if (!\Auth::user()->id) {
        return false;
    }

    $row->setAttribute('created_by', \Auth::user()->id);
    $row->setAttribute('assign_to', \Auth::user()->id);
}

i have adding this because of my project need this. you can also add your field in onCreating() function.

我添加了这个是因为我的项目需要这个。您还可以在 onCreating() 函数中添加您的字段。