简体   繁体   中英

Laravel eloquent error with “hasmany” relationship

I have this annoying problem when trying to execute a hasMany method:

I have a , of which I want to retrieve all the states ,我想检索所有状态

class Task extends Eloquent {
protected $guarded = array();

public static $rules = array();


public function states(){
    return $this->hasMany('TaskState','task_id');
}

...from this (db has columns: task_id, user_id, status) (数据库具有列:task_id,user_id,状态)

class TaskState extends Eloquent {
public $table= "taskstates";
protected $guarded = array();

public static $rules = array();


public function task(){
    return $this->belongsToMany('Task','task_id');
}

However when I try to execute this,

$task = Task::find($id);
    return $task->states;

it keeps giving this error:

Call to undefined method TaskState::newQuery()

I'm stuck on this for like two days. Would anyone know what the problem might be here? Thanks in advance!

如果模型之间存在一对多关系,则应在TaskState中使用“ belongsTo”而不是“ belongsToMany”;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM