douou9786 2016-02-09 19:05
浏览 43

如何保护删除以前的class元素值

I have controller class in laravel in which i have a function create() and a variable attachment i am calling function by ajax my class code is.

class AttachmentController extends Controller
{
    public  $_attachments;

    public function create()
    {

            $this->_attachments[]=  'test';

            var_dump($this->_attachments);



    }

problem is every time when i call it by ajax it return me "test" at 0 index of attachment array . but i want if i call create function 1st time it give me test on 0 index but when next time when i call it . it give me "test" on both 0 and 1 index and so on .. how it is possible please help me

  • 写回答

3条回答 默认 最新

  • douzhenyu6533 2016-02-09 19:24
    关注

    To preserve the values between requests you need to store them somewhere, an alternative is to use sessions, as the example below, for more information see https://laravel.com/docs/session

    <?php
    
    namespace App\Http\Controllers;
    
    use Illuminate\Http\Request;
    
    use App\Http\Requests;
    use App\Http\Controllers\Controller;
    
    class AttachmentController extends Controller
    {
        public function create(Request $request)
        {   
            $request->session()->push('attachments', 'test');
            var_dump($request->session()->get('attachments'));
        }   
    }
    
    评论

报告相同问题?