laravel:总是会运行到其它model的toArray()
Can u save me?!!!!!
以下是代码,麻烦了解的解答一下,跪谢!
需要什么我都可以贴上来,在线等!
这是我controller->index()的代码
Blockquote
public function index(Request $request)
{
$keyword = $request->get('search');
$attendance_info = attendance::with('student','lesson')->orderBy('its_record_attendance.attendance_time','desc');
if($keyword){
$attendance_info->where('its_record_attendance.student_id', 'like', $keyword . '%')
->orwhere('its_record_attendance.student_name', 'like', '%'.$keyword . '%')
->orwhere('its_record_attendance.attendance_lesson', 'like', '%'.$keyword . '%');
}
$attendance_info = $attendance_info->paginate();
$ret = $attendance_info->toArray();
if ($ret) {
$data = ['status' => 200, 'message' => 'success', 'data' => $ret];
} else {
$data = ['status' => 200, 'message' => "error", 'data' => null];
}
return Response($data);
}
以下App\Models\attendance.php
Blockquote
namespace App\Models;
use Sofa\Eloquence\Eloquence;
use Sofa\Eloquence\Mappable;
class attendance extends BasicModel
{
use Eloquence, Mappable;
protected $table = 'its_record_attendance';
public $timestamps = false;
protected $guarded = [];
public function student()
{
return $this->belongsTo('App\Models\student', 'student_id')
->select('id','name','class','home','phone','birthday','gender');
}
public function lesson()
{
return $this->belongsTo('App\Models\lesson', 'attendance_lesson')
->select('id','semester','teacher','lesson_name');
}
function toArray()
{
$arr = parent::toArray(); // TODO: Change the autogenerated stub
$arr['student_info'] = $arr['student_id'].'-'.$arr['student_name'];
$arr['lesson_info'] = $arr['lesson']['id'].'-'. $arr['lesson']['lesson_name'].'-'.$arr['lesson']['teacher'];
return $arr;
}
我的理解是不会运行到 App\Models\lesson.php,但是事实相反,
而且报错:ErrorException in lesson.php line 53:Illegal string offset 'name',
很明显他是运行到lesson.php然后识别不出$arr['semester']['name']。
附上App\Models\lesson.php的代码
Blockquote
namespace App\Models;
use Sofa\Eloquence\Eloquence;
use Sofa\Eloquence\Mappable;
class lesson extends BasicModel
{
use Eloquence, Mappable;
protected $table = 'its_teacher_lesson';
public $timestamps = false;
protected $guarded = [];
public function semester()
{
return $this->belongsTo('App\Models\semester', 'semester')
->select('id','name');
}
public function classtime()
{
return $this->belongsTo('App\Models\classtime', 'time')
->select('id','name');
}
public function classroom()
{
return $this->belongsTo('App\Models\classroom', 'class_room')
->select('id','name');
}
public function classinfo()
{
return $this->belongsTo('App\Models\classinfo', 'class_id')
->select('id','name');
}
function toArray()
{
$arr = parent::toArray(); // TODO: Change the autogenerated stub
$arr['semester'] = $arr['semester']['name'];
$arr['time'] = $arr['classtime']['name'];
$arr['class_room'] = $arr['classroom']['name'];
$arr['class_id'] = $arr['classinfo']['name'];
return $arr;
}
}Blockquote
No Comments