laravel中从一个函数到另一个函数的变量

时间:2019-02-21 13:08:40

标签: php laravel

有2种功能,其中一种是压缩不存在的学校以查看和查看应将以下专业的系归还给新的学校功能。在插入DB时,我需要2个数组,一个来自输入,另一个来自上述函数。 但是数组未返回任何值,如上所示

protected $school_notFound = [];

public function insert(Request $request) {
    for ($i = 0; $i < sizeof($majors_array1); $i++) {
        if (School::where('major', '=', $majors_array1[$i])->exists()) {

        } else {
            array_push($this->school_notFound, $majors_array1[$i]);
            $this->school_notFound = array_unique($this->school_notFound);
        }
    }
    $school_notFound = $this->school_notFound;
    return view('enter-school-dept', compact('school_array', 'school_notFound'));
}

public function New_School(Request $request) {
    print_r($this->school_notFound);
    $New_school = $request->input('selected_school');
    print_r($New_school);
}

结果是

Array ( ) 
Array ( [0] => SOB [1] => SOB [2] => CMN )

1 个答案:

答案 0 :(得分:0)

我从您提供的代码中摘录,有人将填写表格以调用您的第二个函数New_School()。

由于我们无法将数组传递给表单,因此需要首先对数组进行序列化。您可以为此使用serialize()或json_encode(),在我的示例中,我使用json_encode()。比我们在表单中设置一个隐藏字段将携带序列化的jsonString。当用户提交表单时,我们可以从请求中检索jsonString并使用json_decode()将其转换回数组。如果使用serialize(),则可以使用unserialize()转换回数组。

PHP

class testController extends Controller
{
    //json_encode the array so we can put it in a hidden field in our form
    public function test(){
        $array = ['test', 'test2'];

        $json = json_encode($array);

        return view('test')->with('json', $json);
    }

    //json_decode the jsonString back to an array
    public function test2(Request $request){
        $array2 = json_decode($request->input('json'));

        print_r($array2);
    }
}

视图:

html

<form action="{{route('test2')}}" method="post">
    <input type="hidden" name="json" value="{{$json}}"><br>
    <input type="submit" value="Submit">
</form>

还有一个提示:在变量命名方式上要保持一致。请不要使用camelCase或snake_case来混合它们。


根据要求提供示例代码:

PHP

// This is not needed
//protected $school_notFound = [];

public function insert(Request $request) {
    $school_notFound = [];

    for ($i = 0; $i < sizeof($majors_array1); $i++) {
        if (School::where('major', '=', $majors_array1[$i])->exists()) {

        } else {
            array_push($school_notFound, $majors_array1[$i]);
            $school_notFound = array_unique($school_notFound);
        }
    }

    $school_not_found_json = json_encode($school_notFound);

    return view('enter-school-dept')
        ->with('school_notFound', $school_notFound)
        ->with('school_array', $school_array)
        ->with('school_not_found_json', $school_not_found_json);
}

public function New_School(Request $request) {
    $array2 = json_decode($request->input('school_not_found_json'));
    print_r($array2);
    $New_school = $request->input('selected_school');
    print_r($New_school);
}

html

<form action="" method="">
    <input type="hidden" name="school_not_found_json" value="{{$school_not_found_json}}">

    @foreach ($school_notFound as $notfound) 
    <option value='{{$notfound}}'>{{$notfound}}</option>
    <input list="browsers" name="selected_school[]" /></label>
    <datalist id="browsers" name="selected_school[]">
    @endforeach

    @foreach ($school_array as $school) 
    <option value='{{$school}}'>{{$school}}</option>
    @endforeach

    <input type="submit" value="Submit">
</form>