如何从两个模型连接这两个表:
Student.php:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $table = 'students';
}
StudentDetails.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class ProductDetails extends Model
{
protected $table = 'student_details';
}
然后在一个控制器中使用这两个模型:
use App\Student;
use App\StudentDetails;
public function index()
{
return Product::latest()
->join('StudentDetails','StudentDetails.product_id','Product.product_id')
->get();
//I dont know what the right approach is but i was thinking of
doing something like this
}
我搞不懂StudentDetails模型的用途是什么。通过以下方式我才让它运行起来:
return Student::latest()
->join('student_details','student_details.student_id','student.student_id')
->get();