Wis*_*dis 1 php view object laravel laravel-5
我想将学生模型的对象传递给 Laravel 中的视图。我尝试使用
return view('student/main')->with($student);
Run Code Online (Sandbox Code Playgroud)
$student 是学生模型的一个实例(代码如下)
但它给出了一个错误“非法偏移类型”
我知道数据可以作为数组传递给视图。但如果可能的话,我真的想把它作为一个对象传递。然后我可以通过从 get 方法获取数据来显示数据如下。
<h4 class="text-left"><strong>{{$student->getName()}}</strong> </h4>
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种可以使用保留对象而不是数组来完成的解决方案。(如果可能)
Student 模型代码如下。它由简单的 setter 和 getter 组成。
class Student extends Model{
//attributes
private $student_id;
private $first_name;
private $last_name;
private $batch_id;
// set attributes
public function setID($student_id)
{
$this->student_id = $student_id;
}
public function setFirstName($first_name)
{
$this->first_name = $first_name;
}
public function setLastName($last_name)
{
$this->last_name = $last_name;
}
public function setBatchID($batch_id)
{
$this->batch_id = $batch_id;
}
// get attributes
public function getName()
{
return $this->first_name." ".$this->last_name;
}
public function getID()
{
return $this->student_id;
}
public function getBatchID()
{
return $this->batch_id;
}
Run Code Online (Sandbox Code Playgroud)
小智 5
你有很多选择来做到这一点
return view('student/main', ['student'=> $student]);
return view('student/main', compact('student'));
return view('student/main')->with('student', $student);
return view('student/main')->withStudent($student);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1856 次 |
| 最近记录: |