2017-06-15 133 views
0

我已經開始使用Voyager,並且遇到了控制器問題。Laravel Voyager覆蓋BREAD控制器

我已經在數據庫中創建了一個名爲Progress的新表,默認情況下是voyager創建一個BREAD表單來瀏覽讀取刪除和添加項目。

我喜歡在用戶添加新項目時將默認值放入字段中。我想要的默認值是身份驗證user_id

我該怎麼做?

謝謝。

+0

你能覆蓋包視圖嗎?我從未使用過旅行者,因此我只是在黑暗中拍攝一張照片,但如果您可以發佈供應商文件並對其進行修改,則可以通過此方式設置默認值。 – btl

回答

0

我想你可以創建一個遷移寫一個默認值該字段

0

你可以通過創建你的麪包的模型,如圖圖像 enter image description here

你已經創建完成後,您的麪包模型,你可以創建現名爲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(); 
    } 
} 

每次保存任何記錄在航海的管理功能,您將看到當前登錄的用戶ID是getti ng保存在數據庫表中。

0

您可以完全在Voyager之外做到這一點。

首先從Add窗體(在Voyager的數據庫接口中)中排除authentication_user_id。如果該字段沒有空值,您可以設置一些臨時默認值,或者修改您的遷移 - 以最方便的方式。

接下來創建一個model observer,然後利用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(); 
     } 

    } 
0

您必須添加以下代碼到這樣的模式,

//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); 
} 

我已經加入這一點,因爲我的項目需要這個。你也可以在onCreating()函數中添加你的字段。