你好,我是laravel的新手,不太懂编程所以我在控制器中有这段代码我想从数据库记录中获取结果集然后将其用作视图下拉菜单的选择这是控制器中的代码
$rcrdID = DB::table('dbo_studentrecord')
->join('dbo_students','dbo_studentrecord.StudentID' ,'=' ,'dbo_students.StudentID')
->lists(DB::raw('CONCAT("ID ", dbo_studentrecord.StudentID, " | " ,dbo_students.FirstName, " ", dbo_students.LastName)'),'StudentRecordID');
我试图使下拉菜单看起来像,例如:
ID | 1 | Billy | Joe | 12345
是否有更好的方法来做到这一点?由于多
您应该在Student
和StudentRecord
模型之间建立关系。
class Student extends Model {
protected $table = 'students';
public function StudentRecord() {
return $this->hasOne('App'StudentRecord');
}
}
学生记录模型(App'StudentRecord.php):
class StudentRecord extends Model {
protected $table = 'student_records';
public function Student() {
return $this->belongsTo('App'Student');
}
}
如果您创建了关系(一对一),您可以在控制器中使用以下查询:
namespace App'Http'Controllers;
use Student;
use StudentRecord;
class StudentController extends Controller {
public function getStudent($id) {
return Student::find($id)->load('StudentRecord');
}
}
你可以阅读load()函数