I am new to laravel framework and working on one-to-one relationship. I have created a table of student and phone. I am trying to get a student who has a phone number. but i get an error.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Student extends Model
{
use HasFactory;
// protected $table = 'stdnt';
//protected $primaryKey = 'student_id';
//public $timestamps = false;
// One to one relationship
public function rPhone()
{
return $this->hasOne(Phone::class);
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Phone extends Model
{
use HasFactory;
}
@foreach ($all_students as $item)
Name: {{ $item->student_name }} <br>
Email: {{ $item->student_email }} <br>
Phone: {{ $item->rPhone->phone }} <br><br>
@endforeach




