如何在雄辩的关系中建立关系Laravel

如何在雄辩的关系中建立关系Laravel

问题描述:

I have Two Table

1- projects

User with id 5 has created 3 project - which means he will have 3 unique conversation with our different agent

----------------------------------------
|id | user_id |  title    | time       |
|1  |    5    |  Example  | 2018-06-30 |
|2  |    5    |  Example  | 2018-06-30 |
|3  |    5    |  Example  | 2018-06-30 |
----------------------------------------

2- conversation

-------------------------------------------
|id | project_id |  user_one | user_two   |
|1  |    1       |     5     |     3      |
|2  |    2       |     5     |     7      |
|3  |    3       |     5     |     10     |
-------------------------------------------

Whenever a project is created, a conversation is created with that project id, Now in Laravel i want to get that project detail using Eloquent Relationships.

User_one is the project creator and user_two is our agent assigned to that product.

This is What I've tried

class Chat extends Model {
 public function project()
    {
        return $this->belongsTo('App\ProjectModel');
    }
 }

class ProjectModel extends Model
{
   public $table = 'projects';
}

Here is the controller function

public function Progress($id){ // Id passed here is 2, so it should show detail of project number 2

    return \App\Chat::find($id)->project()->get();
}

After all this I'm getting an error - Call to a member function project() on null

我有两张表 p>

1-项目 p>

id为5的用户创建了3个项目 - 这意味着他将与我们的不同代理进行3次独特的对话 p>

  ----------  ------------------------------ 
 | id |  user_id | 标题| 时间| 
 | 1 |  5 | 示例|  2018-06-30 | 
 | 2 |  5 | 示例|  2018-06-30 | 
 | 3 |  5 | 示例|  2018-06-30 | 
 ---------------------------------------- 
   code>  pre> 
 
 

2-对话 p>

  ------------------  ------------------------- 
 | id |  project_id |  user_one |  user_two | 
 | 1 |  1 |  5 |  3 | 
 | 2 |  2 |  5 |  7 | 
 | 3 |  3 |  5 |  10 | 
 ------------------------------------------- 
   pre> 
 
 

无论何时创建项目,都会使用该项目ID创建对话。现在在Laravel中我想使用Eloquent Relationships获取该项目详细信息。 p> \ n

User_one是项目创建者,user_two是我们分配给该产品的代理。 p>

这是我尝试过的 p>

  class Chat扩展Model {
 public function project()
 {
返回$ this-> belongsTo('App \ ProjectModel'); 
} 
} 
 
class ProjectModel扩展Model 
 
 \  n public $ table ='projects'; 
} 
  code>  pre> 
 
 

这是控制器功能 p>

 公共功能 进度($ id){//此处传递的ID为2,因此它应显示项目编号2的详细信息
 
返回\ App \ Chat :: find($ id) - > project() - > get(  ); 
} 
  code>  pre> 
 
 

在所有这些之后我收到一个错误 - 在null上调用成员函数project() p> DIV>

You can try like this with creating 2 Models Project and Conversation

table

conversations(id, project_id, creator, agent)

Project Model

public function conversations(){
    return $this->hasMany('App\Conversation');
} 

Conversation Model

public function project(){
   return $this->belongsTo('App\Project', 'project_id');
} 


public function creatorUser(){
   return $this->belongsTo('App\User', 'creator');
} 

public function agentUser(){
   return $this->belongsTo('App\User', 'agent');
} 

Fetch Data

public function Progress($id){ // Id passed here is 2, so it should show detail of project number 2
     $chats = Conversation::with('project', 'creatorUser', 'agentUser')->where('project_id', 2)->get();
     foreach($chats as $chat){
        dd($chat->project); //it will always print same project because we have filter the conversation by project_id
        dd($chat->creatorUser);
        dd($chat->agentUser);

     }

}

you can create a relation Along with specifying foreign Key and owner key like:

class Chat extends Model {
 public function project()
    {
        return $this->belongsTo(App\ProjectModel::class, 'project_id', 'id');
    }
 }

and then you will be able to get:

\App\Chat::where('id', $id)->first()->project;