将数据从控制器传递到 Laravel 中查看
Passing data from controller to view in Laravel
我是 Laravel 的新手,我一直在尝试将 table 'student' 的所有记录存储到一个变量,然后将该变量传递给一个视图,以便我可以显示他们。
我有一个控制器 - ProfileController,里面有一个函数:
public function showstudents() {
$students = DB::table('student')->get();
return View::make("user/regprofile")->with('students',$students);
}
在我看来,我有这段代码:
<html>
<head>
//---HTML Head Part
</head>
<body>
Hi {{ Auth::user()->fullname }}
@foreach ($students as $student)
{{ $student->name }}
@endforeach
@stop
</body>
</html>
我收到此错误:Undefined variable: students (View:regprofile.blade.php)
你能试试这个吗,
return View::make("user/regprofile", compact('students')); OR
return View::make("user/regprofile")->with(array('students'=>$students));
同时,您可以像这样设置多个变量,
$instructors="";
$instituitions="";
$compactData=array('students', 'instructors', 'instituitions');
$data=array('students'=>$students, 'instructors'=>$instructors, 'instituitions'=>$instituitions);
return View::make("user/regprofile", compact($compactData));
return View::make("user/regprofile")->with($data);
尝试使用此代码:
return View::make('user/regprofile', array
(
'students' => $students
)
);
或者如果您想将更多变量传递到视图中:
return View::make('user/regprofile', array
(
'students' => $students,
'variable_1' => $variable_1,
'variable_2' => $variable_2
)
);
用于传递单个变量以查看。
在您的控制器中创建一个方法,例如:
function sleep()
{
return view('welcome')->with('title','My App');
}
在你的路线中
Route::get('/sleep', 'TestController@sleep');
在您看来Welcome.blade.php
。您可以像 {{ $title }}
一样回显您的变量
对于数组(多个值)更改,睡眠方法为:
function sleep()
{
$data = array(
'title'=>'My App',
'Description'=>'This is New Application',
'author'=>'foo'
);
return view('welcome')->with($data);
}
您可以像 {{ $author }}
一样访问您的变量。
在Laravel 5.6中:
$variable = model_name::find($id);
return view('view')->with ('variable',$variable);
你也可以试试这个:
public function showstudents(){
$students = DB::table('student')->get();
return view("user/regprofile", ['students'=>$students]);
}
此外,在您的 view.blade
文件中使用此变量来获取学生姓名和其他列:
{{$students['name']}}
将单个或多个变量传递给控制器查看的最好和最简单的方法是使用compact()方法。
用于将单个变量传递给视图,
return view("user/regprofile",compact('students'));
用于将多个变量传递给视图,
return view("user/regprofile",compact('students','teachers','others'));
并且在视图中,您可以轻松地遍历变量,
@foreach($students as $student)
{{$student}}
@endforeach
public function showstudents() {
$students = DB::table('student')->get();
return (View::make("user/regprofile", compact('student')));
}
尝试使用此代码:
Controller:
-----------------------------
$fromdate=date('Y-m-d',strtotime(Input::get('fromdate')));
$todate=date('Y-m-d',strtotime(Input::get('todate')));
$datas=array('fromdate'=>"From Date :".date('d-m-Y',strtotime($fromdate)), 'todate'=>"To
return view('inventoryreport/inventoryreportview', compact('datas'));
View Page :
@foreach($datas as $student)
{{$student}}
@endforeach
[Link here]
$books[] = [
'title' => 'Mytitle',
'author' => 'MyAuthor,
];
//pass data to other view
return view('myView.blade.php')->with('books');
or
return view('myView.blade.php','books');
or
return view('myView.blade.php',compact('books'));
----------------------------------------------------
//to use this on myView.blade.php
<script>
myVariable = {!! json_encode($books) !!};
console.log(myVariable);
</script>
在laravel 8及以上版本中,您可以通过这种方式进行路由绑定。
public function showstudents() {
$students = DB::table('student')->get();
return view("user/regprofile",['students'=>$students]);
}
在视图文件中,您可以像下面这样访问它。
@foreach($students as $student)
{{$student->name}}
@endforeach
我是 Laravel 的新手,我一直在尝试将 table 'student' 的所有记录存储到一个变量,然后将该变量传递给一个视图,以便我可以显示他们。
我有一个控制器 - ProfileController,里面有一个函数:
public function showstudents() {
$students = DB::table('student')->get();
return View::make("user/regprofile")->with('students',$students);
}
在我看来,我有这段代码:
<html>
<head>
//---HTML Head Part
</head>
<body>
Hi {{ Auth::user()->fullname }}
@foreach ($students as $student)
{{ $student->name }}
@endforeach
@stop
</body>
</html>
我收到此错误:Undefined variable: students (View:regprofile.blade.php)
你能试试这个吗,
return View::make("user/regprofile", compact('students')); OR
return View::make("user/regprofile")->with(array('students'=>$students));
同时,您可以像这样设置多个变量,
$instructors="";
$instituitions="";
$compactData=array('students', 'instructors', 'instituitions');
$data=array('students'=>$students, 'instructors'=>$instructors, 'instituitions'=>$instituitions);
return View::make("user/regprofile", compact($compactData));
return View::make("user/regprofile")->with($data);
尝试使用此代码:
return View::make('user/regprofile', array
(
'students' => $students
)
);
或者如果您想将更多变量传递到视图中:
return View::make('user/regprofile', array
(
'students' => $students,
'variable_1' => $variable_1,
'variable_2' => $variable_2
)
);
用于传递单个变量以查看。
在您的控制器中创建一个方法,例如:
function sleep()
{
return view('welcome')->with('title','My App');
}
在你的路线中
Route::get('/sleep', 'TestController@sleep');
在您看来Welcome.blade.php
。您可以像 {{ $title }}
对于数组(多个值)更改,睡眠方法为:
function sleep()
{
$data = array(
'title'=>'My App',
'Description'=>'This is New Application',
'author'=>'foo'
);
return view('welcome')->with($data);
}
您可以像 {{ $author }}
一样访问您的变量。
在Laravel 5.6中:
$variable = model_name::find($id);
return view('view')->with ('variable',$variable);
你也可以试试这个:
public function showstudents(){
$students = DB::table('student')->get();
return view("user/regprofile", ['students'=>$students]);
}
此外,在您的 view.blade
文件中使用此变量来获取学生姓名和其他列:
{{$students['name']}}
将单个或多个变量传递给控制器查看的最好和最简单的方法是使用compact()方法。
用于将单个变量传递给视图,
return view("user/regprofile",compact('students'));
用于将多个变量传递给视图,
return view("user/regprofile",compact('students','teachers','others'));
并且在视图中,您可以轻松地遍历变量,
@foreach($students as $student)
{{$student}}
@endforeach
public function showstudents() {
$students = DB::table('student')->get();
return (View::make("user/regprofile", compact('student')));
}
尝试使用此代码:
Controller:
-----------------------------
$fromdate=date('Y-m-d',strtotime(Input::get('fromdate')));
$todate=date('Y-m-d',strtotime(Input::get('todate')));
$datas=array('fromdate'=>"From Date :".date('d-m-Y',strtotime($fromdate)), 'todate'=>"To
return view('inventoryreport/inventoryreportview', compact('datas'));
View Page :
@foreach($datas as $student)
{{$student}}
@endforeach
[Link here]
$books[] = [
'title' => 'Mytitle',
'author' => 'MyAuthor,
];
//pass data to other view
return view('myView.blade.php')->with('books');
or
return view('myView.blade.php','books');
or
return view('myView.blade.php',compact('books'));
----------------------------------------------------
//to use this on myView.blade.php
<script>
myVariable = {!! json_encode($books) !!};
console.log(myVariable);
</script>
在laravel 8及以上版本中,您可以通过这种方式进行路由绑定。
public function showstudents() {
$students = DB::table('student')->get();
return view("user/regprofile",['students'=>$students]);
}
在视图文件中,您可以像下面这样访问它。
@foreach($students as $student)
{{$student->name}}
@endforeach