Eloquent 关系控制器抛出试图获取非对象的 属性
Eloquent Relationship Controller throw to Trying to get property of non-object
我有两个具有一对一关系的 table(学生和 phone)。问题是 StudentController 抛出 Trying to get 属性 of non-object
这是我的代码
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');
}
}
Phone迁移
<?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');
}
}
学生控制器
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'));
}
当我访问 "student/edit" 时出现错误说有 ErrorException (E_NOTICE) $student->phone_number = $student->phone->phone_number;
当我在 "student/create"
"Undefined variable: student"
$student->phone()->save($phone);`[=18 上创建商店数据表单时=]
感谢任何帮助。
在您的 store
方法中,您从未定义名为 student
的变量。没有 $student = ...
或传递给名为 student
的方法的变量。在范围内不会为您神奇地创造任何东西。
您正在尝试使用一个不作为对象存在的变量 $student->...
,您从未定义 $student
。
在 edit
方法中你有:
$student = Student::findOrFail($id);
$student->phone_number = $student->phone->phone_number;
$student->phone
可能是 null
。您必须在此处检查 null
。单一关系类型将 return 模型实例或 null
.
(null)->anything
"Trying to get a property of a non-object"
更新:
我不确定你想要什么例子但是:
$student = Student::create(....);
现在您定义了一个名为 student
的变量,根据您的代码上下文,它将是您期望的对象。
if ($student->phone) {
$student->phone_number = $student->phone->phone_number;
}
检查 $student->phone
是否具有 "truthy" 值(对象 return true
转换为 bool
时,null
将是 false
),如果我们有关系,我们将从中提取 phone_number
。
我不确定您为什么要以这种方式添加 phone 数字,但您可以选择在模型上为该属性创建一个 'accessor':
public function getPhoneNumberAttribute()
{
return $this->phone ? $this->phone->phone_number : null;
// php 7+
return $this->phone->phone_number ?? null;
}
那种类型的东西,现在 $student->phone_number
将为您从关系中获得价值……可以调整该方法,但基本上就是这样。
我有两个具有一对一关系的 table(学生和 phone)。问题是 StudentController 抛出 Trying to get 属性 of non-object
这是我的代码
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');
}
}
Phone迁移
<?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');
}
}
学生控制器
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'));
}
当我访问 "student/edit" 时出现错误说有 ErrorException (E_NOTICE) $student->phone_number = $student->phone->phone_number;
当我在 "student/create"
"Undefined variable: student"
$student->phone()->save($phone);`[=18 上创建商店数据表单时=]
感谢任何帮助。
在您的 store
方法中,您从未定义名为 student
的变量。没有 $student = ...
或传递给名为 student
的方法的变量。在范围内不会为您神奇地创造任何东西。
您正在尝试使用一个不作为对象存在的变量 $student->...
,您从未定义 $student
。
在 edit
方法中你有:
$student = Student::findOrFail($id);
$student->phone_number = $student->phone->phone_number;
$student->phone
可能是 null
。您必须在此处检查 null
。单一关系类型将 return 模型实例或 null
.
(null)->anything
"Trying to get a property of a non-object"
更新:
我不确定你想要什么例子但是:
$student = Student::create(....);
现在您定义了一个名为 student
的变量,根据您的代码上下文,它将是您期望的对象。
if ($student->phone) {
$student->phone_number = $student->phone->phone_number;
}
检查 $student->phone
是否具有 "truthy" 值(对象 return true
转换为 bool
时,null
将是 false
),如果我们有关系,我们将从中提取 phone_number
。
我不确定您为什么要以这种方式添加 phone 数字,但您可以选择在模型上为该属性创建一个 'accessor':
public function getPhoneNumberAttribute()
{
return $this->phone ? $this->phone->phone_number : null;
// php 7+
return $this->phone->phone_number ?? null;
}
那种类型的东西,现在 $student->phone_number
将为您从关系中获得价值……可以调整该方法,但基本上就是这样。