vendredi 11 novembre 2016

how to join two models in laravel 5.2

I have two models as Project and Collaborator this is My tables

Project column

id
project_name
project_note
user_id

Collaborator Model

id
project_id
collaborator_id

I need join this models to get project_name instead project_id of Collaborator Model.

how can I do this. I read www.laravel.com documents but difficult to understand. help me in code...

project Model
<?php

namespace App;
use Auth;
use Illuminate\Database\Eloquent\Model;

class Project extends Model
{

     /*
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['project_name', 'project_notes', 'project_status', 'due_date'];

    public function scopePersonal($query)
{
     return $query->where('user_id', Auth::user()->id);

}



    //
}

collaborator Model
<?php

namespace App;
use Auth;
use Illuminate\Database\Eloquent\Model;



class Collaboration extends Model
{

    protected $table = 'project_collaborator';

    /*
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['project_id', 'collaborator_id'];



     /*
     * Get the user that is a collaborator on another project
     * @return collection
     */
    public function user()
    {
        return $this->belongsTo(User::class, 'collaborator_id');
    }


      /*
     * Query scope to return information about the current project
     * @param  $query
     * @param  int $id
     * @return query
     */
    public function scopeProject($query, $id)
    {
        return $query->where('project_id', $id);
    }

    public function scopeColabo($query)
{
     return $query->where('collaborator_id',Auth::user()->id);

}






}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire