我已经定义了变量这是我的代码
class MainController extends Controller
{
public function index()
{
$country = Country::all();
return view ('index',compact($country));
}
public function getStates($id)
{
$states = State::where('country_id', $id)->pluck("name", "id");
return json_encode($states);
}
}
您可以执行以下任何操作
1。
public function index()
{
$country = Country::all();
return view('index',compact('country'));
}
public function index()
{
$country = Country::all();
return view('index',['country' => $country]);
}
当使用压缩写入数据时,不带$
并将其放入''
:
public function index()
{
$country = Country::all();
return view ('index',compact('country'));
}