2015-06-25 17 views
8

Vì vậy, tôi nhận được lỗi sau từ Laravel:SQLSTATE [42000]: Cú pháp lỗi hoặc vi phạm truy cập: 1066 bảng Không độc đáo/bí danh trên mối quan hệ

SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'participants' (SQL: select `participants`.*, `participants`.`message_id` as `pivot_message_id`, `participants`.`user_id` as `pivot_user_id`, `participants`.`created_at` as `pivot_created_at`, `participants`.`updated_at` as `pivot_updated_at` from `participants` inner join `participants` on `participants`.`id` = `participants`.`user_id` where `participants`.`deleted_at` is null and `participants`.`message_id` in (2)) 

nhắn/người tham gia relatioship vẻ của tôi như thế này:

public function participants() 
    { 
     return $this->belongsToMany('Namespace\Modules\Email\Models\Participant', 'participants', 'message_id', 'user_id')->withTimestamps(); 
    } 

và tôi đang cố gắng để gọi nó là như thế này:

public function getAllMessages() 
{ 
    return Message::with('user')->with('participants')->get(); 
} 

Tại sao tôi nhận được lỗi này? Chuyện gì vậy?

Chỉnh sửa: Đã bao gồm mô hình đầy đủ

nhắn lớp nhắn kéo dài hùng biện { sử dụng PublishedTrait; sử dụng SoftDeletingTrait;

/** 
    * The database table used by the model. 
    * 
    * @var string 
    */ 
    protected $table = 'messages'; 

    /** 
    * The attributes that can be set with Mass Assignment. 
    * 
    * @var array 
    */ 
    protected $fillable = ['subject', 'user_id', 'body', 'status']; 

    /** 
    * The attributes that should be mutated to dates. 
    * 
    * @var array 
    */ 
    protected $dates = ['created_at', 'updated_at', 'deleted_at']; 

    /** 
    * Validation rules. 
    * 
    * @var array 
    */ 
    protected $rules = [ 
     'subject' => 'required|max:255', 
     'body' => 'required', 
    ]; 

    /** 
    * User relationship 
    * 
    * @return \Illuminate\Database\Eloquent\Relations\BelongsTo 
    */ 
    public function user() 
    { 
     return $this->belongsTo(Config::get('email.user_model')); 
    } 

    public function assets() 
    { 
     return $this->belongsToMany('Namespace\Modules\Assets\Models\Asset', 'message_assets'); 
    } 

    /** 
    * Participants relationship 
    * 
    * @return \Illuminate\Database\Eloquent\Relations\HasMany 
    */ 
    public function participants() 
    { 
     return $this->belongsToMany('Namespace\Modules\Email\Models\Participant', 'participants', 'message_id', 'user_id')->withTimestamps(); 
    } 

    /** 
    * Recipients of this message 
    * 
    * @return \Illuminate\Database\Eloquent\Relations\HasMany 
    */ 
    public function recipients() 
    { 
     return $this->participants()->where('user_id', '!=', $this->user_id); 
    } 

    /** 
    * Returns the latest message from a thread 
    * 
    * @return Namespace\Modules\Email\Models\Message 
    */ 
    public function getLatestMessageAttribute() 
    { 
     return $this->messages()->latest()->first(); 
    } 

    /** 
    * Returns threads that the user is associated with 
    * @param $query 
    * @param $userId 
    * @return mixed 
    */ 
    public function scopeForUser($query, $userId) 
    { 
     return $query->join('participants', 'messages.id', '=', 'participants.message_id') 
      ->where('participants.user_id', $userId) 
      ->where('participants.deleted_at', null) 
      ->select('messages.*'); 
    } 

    /** 
    * Returns threads that the user is associated with 
    * @param $query 
    * @param $userId 
    * @return mixed 
    */ 
    public function scopeForUserWithDeleted($query, $userId) 
    { 
     return $query->join('participants', 'messages.id', '=', 'participants.message_id') 
      ->where('participants.user_id', $userId) 
      ->select('messages.*'); 
    } 

    /** 
    * Returns messages that the user has sent 
    * @param $query 
    * @param $userId 
    * @return mixed 
    */ 
    public function scopeByUser($query, $userId) 
    { 
     return $query->where('user_id', $userId); 
    } 

    /** 
    * Returns threads with new messages that the user is associated with 
    * @param $query 
    * @param $userId 
    * @return mixed 
    */ 
    public function scopeForUserWithNewMessages($query, $userId) 
    { 
     return $query->join('participants', 'messages.id', '=', 'participants.message_id') 
      ->where('participants.user_id', $userId) 
      ->whereNull('participants.deleted_at') 
      ->where(function ($query) { 
       $query->where('messages.updated_at', '>', $this->getConnection()->raw($this->getConnection()->getTablePrefix() . 'participants.last_read')) 
        ->orWhereNull('participants.last_read'); 
      }) 
      ->select('messages.*'); 
    } 

} 

tham gia

class Participant extends Eloquent 
{ 
    use SoftDeletingTrait; 

    /** 
    * The database table used by the model. 
    * 
    * @var string 
    */ 
    protected $table = 'participants'; 

    /** 
    * The attributes that can be set with Mass Assignment. 
    * 
    * @var array 
    */ 
    protected $fillable = ['message_id', 'user_id', 'last_read']; 

    /** 
    * The attributes that should be mutated to dates. 
    * 
    * @var array 
    */ 
    protected $dates = ['created_at', 'updated_at', 'deleted_at', 'last_read']; 

    /** 
    * Thread relationship 
    * 
    * @return \Illuminate\Database\Eloquent\Relations\BelongsTo 
    */ 
    public function message() 
    { 
     return $this->hasMany('Namespace\Modules\Email\Models\Message'); 
    } 

    /** 
    * User relationship 
    * 
    * @return \Illuminate\Database\Eloquent\Relations\BelongsTo 
    */ 
    public function user() 
    { 
     return $this->belongsTo(Config::get('email.user_model')); 
    } 
} 

Trả lời

6

trả lời qua Slack chính thức Larachat:

Mối quan hệ thiếu pivot table để làm việc này. Đối số thứ hai trong phương pháp participants là pivot table để sử dụng:

public function participants() 
{ 
    return $this->belongsToMany('Namespace\Modules\Email\Models\Participant', 'PIVOT', 'message_id', 'user_id')->withTimestamps(); 
} 

Vì vậy, bạn không thể sử dụng người tham gia như trục bởi vì nó là một trong các bảng trong mối quan hệ, bạn cần một bảng message_participant trục.

+1

** Lưu ý ** Tham số đầu tiên là tên của mô hình bạn muốn, không phải tên của bảng/mô hình tổng hợp. Trộn lẫn sẽ ném lên một lỗi tương tự. –

2

lỗi của bạn là

...from `participants` inner join `participants` ... 

Bạn cần cung cấp các bí danh cho mỗi tài liệu tham khảo, như trong

...from `participants` p1 inner join `participants` p2 ... 

và sau đó sử dụng p1p2 ở các vị trí chính xác, ví dụ

...on p1.`id` = p2.`user_id` ... 

(Tôi đoán mà là p1 và đó là p2; bạn phải đưa ra quyết định đó)

+0

yea, tôi hiểu phần đó, vấn đề là tôi đang sử dụng mối quan hệ hùng biện chuẩn, và tôi nghĩ rằng chỉ cần làm việc:/ – ChrisBratherton

+2

cách xác định bí danh này bằng cú pháp hùng hồn? – Tom

+3

Đây là câu hỏi về các mối quan hệ hùng hồn trong laravel, không phải cách thực hiện các phép nối sql và bí danh. –

Các vấn đề liên quan