简体   繁体   中英

How to prefill voyager BREAD form with currently logged in user?

[SOLVED] I have a simple listing in my laravel 5.7 project,and used voyager to manage its data. and i want to make a fully working created_by and last_edit_by BREAD table but still have no idea to get the currently logged in user.

i currently using custom views(resources/views/vendor/voyager) with copy-pasted original codes,but still using original VoyagerBreadController.php with some additional import.

?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Auth\AuthManager;
use Illuminate\Support\Facades\Auth;
use Auth\Illuminate\Foundation\Auth\AuthenticatesUsers;
use App\Wisata;
use App\User;

namespace App\Http\Controllers\Voyager;

use TCG\Voyager\Http\Controllers\VoyagerBreadController as BaseVoyagerBreadController;

class VoyagerBreadController extends BaseVoyagerBreadController
{
    //
}

and able to do this on my custom view

//get currently logged user
@php
  $currentUser = Auth::user()->nama;
@endphp

//printed output(for testing)
 {{$currentUser}}

and it works!,so i tried to set the BREAD option like this

{
    "default" : "{{$currentUser}}"
}

however,unlike my custom views it doesn't returned the $currentUser value,just plain {{$currentUser}} in my tables.

please help me to make a fully functional created_by and last_edit_by table.

notes: im open to use any packages.

EDIT : it's solved by duplicating the form just below the real dynamic form in my custom view(edit-add.blade.php),make a hidden type created_by & last_edited_by table & keep the BREAD option empty. and you can still adjust the visiblity without any problems.

//created_by form
<div class="form-group hidden  col-md-12 "><input type="hidden" class="form-control" name="created_by" placeholder="Created By" value="{{$currentUser}}"></div>
//last_edited_by form
<div class="form-group hidden  col-md-12 "><input type="hidden" class="form-control" name="last_edited_by" placeholder="Last Edited By" value="{{$currentUser}}"></div>

anyway , i still would be very thankful if someone give more efficient solution :)

You can add additional properties to the model without changing anything in the view file.

Just add the save() method in the corresponding Model class and assign additional properties.

Model

public function save(array $options = [])
{
    if (Auth::user()) {
        $this->created_by = Auth::user()->getKey();
        //you may use user's name or any other property
        $this->updated_by = Auth::user()->name;
    }

    return parent::save();
}

This will add created_by and updated_by whenever saving the new model record. You may add additional logic to the save() method as per your need.

Tested in Laravel 8 [and should be work in 5.7 too]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM