如何在Laravel中传递数据

时间:2017-11-06 10:07:49

标签: php laravel

我在控制器中写了一个函数

class GPromoCode extends Controller {
    public function generatePromoCode() {
        $length = 8;
        $characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $string = '';    
        $prefix ='KM';
        for ($p = 0; $p < $length ; $p++) {
            $string .= $characters[mt_rand(0, strlen($characters)- 1)];
        }
        return $prefix.$string;
    }
}

在视图刀片模板中,如何通过$ coupon将字符串传递到文本框 例如:{{$ coupon}}

提前致谢!

3 个答案:

答案 0 :(得分:0)

你应该试试这个:

public function generatePromoCode()
        {
        $length = 8;
        $characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $string = '';    
        $prefix ='KM';
            for ($p = 0; $p < $length ; $p++) {
                $string .= $characters[mt_rand(0, strlen($characters)- 1)];
                }
        $coupon = $prefix.''.$string;

        return View('coupon',compact('coupon')); 
        }


<input type="text" value = "{{$coupon}}">

答案 1 :(得分:0)

class GPromoCode extends Controller
{
    //generate promote code
    public function generatePromoCode()
    {
    $length = 8;
    $characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $string = '';    
    $prefix ='KM';
        for ($p = 0; $p < $length ; $p++) {
            $string .= $characters[mt_rand(0, strlen($characters)- 1)];
            }
          $data = $prefix.$string;
         return View('your_view_page_name',compact('data')); 
    }

}

<input type="text" value = "{{$data}}">

答案 2 :(得分:0)

您可以像这样从控制器传递视图文件中的数据:

public function generatePromoCode() {
        $length = 8;
        $characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $string = '';    
        $prefix ='KM';
        for ($p = 0; $p < $length ; $p++) {
            $string .= $characters[mt_rand(0, strlen($characters)- 1)];
        }
        return view('view_file_name',['string'=>$string,'prefix'=>$prefix]);
    }

只是你可以在视图文件中高于这两个变量:

{{$prefix}}
{{$string}}