我有两张桌子(学生和电话),一对一的关系。问题是StudentController抛出试图获取非对象的属性
这是我的代码
Phone.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Phone extends Model
{
//
protected $table = 'phone';
protected $primaryKey = 'student_id';
protected $fillable = [
'student_id',
'phone_number',
];
public function student()
{
return $this->belongsTo('App\Student', 'id_student');
}
}
Student.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Student extends Model
{
protected $table = 'student';
protected $fillable = [
'nisn',
'student_name',
'date_birth',
'sex',
];
protected $dates = ['date_birth'];
public function getStudentNameAttribute($student_name)
{
return ucwords($student_name);
}
public function setStudentNameAttribute($student_name)
{
$this->attributes['student_name'] = strtolower($student_name);
//return strtolower($student_name);
}
public function phone() {
return $this->hasOne('App\Phone', 'student_id');
}
}
电话迁移
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTablePhone extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('phone', function (Blueprint $table)
{
$table->integer('student_id')->unsigned()->primary('student_id');
$table->string('phone_number')->unique();
$table->timestamps();
$table->foreign('student_id')
->references('id')->on('student')
->onDelete('cascade')
->onUpdate('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('phone');
}
}
StudentController
public function store(Request $request)
{
$input = $request->all();
$validator = Validator::make($input, [
'nisn' => 'required|string|size:4|unique:student,nisn',
'student_name' => 'required|string|max:30',
'date_birth' => 'required|date',
'sex' => 'required|in:M,W',
'phone_number' => 'required|numeric|digits_between:10,15|unique:phone,phone_number',
]);
if ($validator->fails()) {
return redirect('student/create')
->withInput()
->withErrors($validator);
}
Student::create($input);
$phone = new Phone;
$phone->phone_number = $request->input('phone_number');
$student->phone()->save($phone);
return redirect('student');
}
public function edit($id)
{
$student = Student::findOrFail($id);
$student->phone_number = $student->phone->phone_number;
return view('student.edit', compact('student'));
}
当我访问&#34; student / edit&#34;时出现错误说有错误异常(E_NOTICE)$student->phone_number = $student->phone->phone_number;
当我在&#34; student / create&#34;
"Undefined variable: student"
$ student-&gt; phone() - &gt; save($ phone);`
任何帮助表示感谢。
答案 0 :(得分:1)
在store
方法中,您从未定义过名为student
的变量。没有$student = ...
或变量传递给名为student
的方法。在你的范围内没有神奇的东西。
您正在尝试使用不作为对象$student->...
存在的变量,您从未定义过$student
。
使用edit
方法:
$student = Student::findOrFail($id);
$student->phone_number = $student->phone->phone_number;
$student->phone
可能是null
。您必须在此处检查null
。单个Realtionship类型将返回Model实例或null
。
(null)->anything
&#34;试图获取非对象的属性&#34;
<强>更新强>
我不确定你想要的例子但是:
$student = Student::create(....);
现在你已经定义了一个名为student
的变量,它将是你期望的对象,基于代码的上下文。
if ($student->phone) {
$student->phone_number = $student->phone->phone_number;
}
检查$student->phone
是否有&#34; truthy&#34; value(对象在转换为true
时返回bool
,null
将为false
),如果我们有关系,我们将从中提取phone_number
。
我不确定您为什么要以这种方式添加该电话号码,但您可以选择创建一个“访问者”#39;在该属性的模型上:
public function getPhoneNumberAttribute()
{
return $this->phone ? $this->phone->phone_number : null;
// php 7+
return $this->phone->phone_number ?? null;
}
那种类型的东西,现在$student->phone_number
将从你的关系中得到那个值......这个方法可以调整,但基本上就是这样。