Laravel is there a way to add values to a request array

前端 未结 13 2001
闹比i
闹比i 2020-12-07 15:24

I come across a situation in Laravel while calling a store() or update() method with Request parameter to add some additional value to the request before calling Eloquent fu

相关标签:
13条回答
  • 2020-12-07 15:42

    I tried $request->merge($array) function in Laravel 5.2 and it is working perfectly.

    Example:

    $request->merge(["key"=>"value"]);
    
    0 讨论(0)
  • 2020-12-07 15:45

    Based on my observations:

    $request->request->add(['variable' => 'value']); will (mostly) work in POST, PUT & DELETE methods, because there is value(s) passed, one of those is _token. Like example below.

    <form action="{{ route('process', $id) }}" method="POST">
        @csrf
    </form>
    
    public function process(Request $request, $id){
        $request->request->add(['id' => $id]);
    }
    

    But [below code] won't work because there is no value(s) passed, it doesn't really add.

    <a href='{{ route('process', $id) }}'>PROCESS</a>
    
    public function process(Request $request, $id){
        $request->request->add(['id' => $id]);
    }
    


    When using GET method you can either declare Request and assign value(s) on it directly. Like below:

    public function process($id){
        $request = new Request(['id' => $id]);
    }
    

    Or you can use merge. This is better actually than $request->request->add(['variable' => 'value']); because can initialize, and add request values that will work for all methods (GET, POST, PUT, DELETE)

    public function process(Request $request, $id){
        $request->merge(['id' => $id]);
    }
    

    Tag: laravel5.8.11

    0 讨论(0)
  • 2020-12-07 15:45

    You can add parameters to the request from a middleware by doing:

    public function handle($request, Closure $next)
    {
        $request->route()->setParameter('foo', 'bar');
        return $next($request);
    }
    
    0 讨论(0)
  • 2020-12-07 15:47

    Usually, you do not want to add anything to a Request object, it's better to use collection and put() helper:

    function store(Request $request) 
    {
        // some additional logic or checking
        User::create(array_merge($request->all(), ['index' => 'value']));
    }
    

    Or you could union arrays:

    User::create($request->all() + ['index' => 'value']);
    

    But, if you really want to add something to a Request object, do this:

    $request->request->add(['variable' => 'value']); //add request
    
    0 讨论(0)
  • 2020-12-07 15:48

    The best one I have used and researched on it is $request->merge([]) (Check My Piece of Code):

        public function index(Request $request) {
            not_permissions_redirect(have_premission(2));
            $filters = (!empty($request->all())) ? true : false;
            $request->merge(['type' => 'admin']);
            $users = $this->service->getAllUsers($request->all());
            $roles = $this->roles->getAllAdminRoles();
            return view('users.list', compact(['users', 'roles', 'filters']));
        }
    

    Check line # 3 inside the index function.

    0 讨论(0)
  • 2020-12-07 15:51

    You can also use below code

    $request->request->set(key, value).

    Fits better for me.

    0 讨论(0)
提交回复
热议问题