我正在尝试将变量从控制器传递到视图,但是它显示以下消息:
未定义的变量。
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class QuizController extends Controller
{
public function getUrl($url = null) {
$bandeira = '1';
$bandeira2 = '2';
if ($url == '1') {
return view('quiz')->with($bandeira);
} elseif ($url == '2') {
return view('quiz')->with($bandeira2);
} else {
return view('home');
}
}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Copa do Mundo 2018</title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
</head>
<body>
<div id="page-wrap">
<h1>Quiz Copa do Mundo 2018</h1>
<form action="grade.php" method="post" id="quiz">
<ol>
<li>
<h3> ?</h3>
@if($bandeira == '1')
<img src="{{ asset('img/espanha.jpg') }}" alt=""/>
@elseif($bandeira == '2')
<img src="{{ asset('img/argentina.jpg') }}" alt=""/>
@endif
答案 0 :(得分:1)
以这种方式传递信息时,数据应为具有键/值对的数组。然后,您可以在视图内部使用其对应的键(例如
<?php echo $key; ?>
)访问每个值。
更正您的代码:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class QuizController extends Controller
{
public function getUrl($url = null) {
$data_to_view['bandeira'] = url;
if ($url != null) {
return view('quiz')->with($data_to_view);
} else {
return view('home');
}
}
}
您认为:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Copa do Mundo 2018</title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
</head>
<body>
<div id="page-wrap">
<h1>Quiz Copa do Mundo 2018</h1>
<form action="grade.php" method="post" id="quiz">
<ol>
<li>
<h3> ?</h3>
@if($bandeira == '1')
<img src="{{ asset('img/espanha.jpg') }}" alt=""/>
@elseif($bandeira == '2')
<img src="{{ asset('img/argentina.jpg') }}" alt=""/>
@endif
答案 1 :(得分:0)
这样写变量
$bandeira['bandeira'] = 1;
return view('quiz')->with($bandeira);
答案 2 :(得分:0)
我认为您应该像这样编写getUrl方法
return view('home', ['bandeira' => $url]);
,不需要if语句
答案 3 :(得分:-1)
更改您的查看页面
const target = 1234; // count the number of digits
function numDigits(num, count=0){
let div10 = count;
div10 += 1;
if(Math.floor(num / 10) < 1){
console.log(`Entered the end and div10 is ${div10}`);
return div10; // returning undefined
}
let curr = Math.floor(num / 10);
console.log(`Bottom of call and dividing ${curr}`);
return numDigits(curr, div10); // HERE
}
console.log(numDigits(target));
我认为这对您有帮助