cmgmyr / laravel-messenger

Simple user messaging package for Laravel
MIT License
2.46k stars 517 forks source link

Before using question #237

Closed lgt closed 7 years ago

lgt commented 7 years ago

Can I use this package for example like a project tracking system. Let me explain:

I have projects and projects having Tickets assigned to users(Project Managers and Responsables), now each Ticket should own some comments where given users can add new comments or reply to existing ones.

Do I have to make some relation between package messages Model and Tickets Model?

Is there any example for this case?

Thanks forward for support

antonkomarev commented 7 years ago

@lgt extend Thread model and set your extended model in config file.

lgt commented 7 years ago

thanks for feedback can you give some example please

antonkomarev commented 7 years ago

https://github.com/cmgmyr/laravel-messenger/issues/92

antonkomarev commented 7 years ago

https://github.com/cmgmyr/laravel-messenger/issues/214

lgt commented 7 years ago

So one I have my model which relates other models and if I extend

Cmgmyr\Messenger\Models\Thread

than all my dependencies are gone. Here is what I tried

<?php

namespace App\MyApp;

//use Illuminate\Database\Eloquent\Model;
use Cmgmyr\Messenger\Models\Thread as MessengerThread;

class Ticket extends MessengerThread
{
    /**
     * @var array
     */
    protected $fillable = [
        'ticket_id',
        'project_id',
        'subject',
        'subject_long',
        'user_id',
        'attachments',
        'project_link',
        'ticket_priority',
        'estimated_time',
        'status',
    ];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function project()
    {
        return $this->belongsTo('App\MyApp\Project');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo('App\User');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
     */
    public function documents()
    {
        return $this->morphMany('App\MyApp\Document', 'document');
    }

}
antonkomarev commented 7 years ago

Now overwrite Thread model with Ticket in the config/messenger.php file. https://github.com/cmgmyr/laravel-messenger/blob/master/src/config/config.php#L11

lgt commented 7 years ago

so I added

'thread_model' => App\MyApp\Ticket::class,

but still the same, no related data returned from other models

antonkomarev commented 7 years ago

@lgt which models? What exactly are you trying to get?

lgt commented 7 years ago

My Ticket has a relation to Projects, Users in controller I would fetch a ticket like

/**
 * @param $id
 *
 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
 */
public function getById($id)
{
    $ticket = Ticket::find($id);

    return view('tickets/detail', array('ticket' => $ticket));
}
antonkomarev commented 7 years ago

@lgt why are you still using Thread model? You can use Ticket model since it's just an extension of Thread.

This should work:

$ticket = Ticket::find($id);

$project = $ticket->project;
dd($project);
lgt commented 7 years ago

I do not understand the relation, if I make that would drop error

Trying to get property of non-object

The point would be get the ticket with project, user and messages

something like

public function threads()
{
    return $this->hasMany('Cmgmyr\Messenger\Models\Thread');
}
cmgmyr commented 7 years ago

I think you're on the right track. In my mind, a Ticket would have one Thread. Since you'd want to add a ticket relation to the thread model, you'd want to extend thread into your own model, update the config, then add the opposite relation to your ticket.

app/ThreadTicket.php

class ThreadTicket extends \Cmgmyr\Messenger\Models\Thread
{
   public function ticket()
   {
        return $this->belongsTo(Ticket::class);
   }
}

config/messenger.php

'thread_model' => App\MyApp\ThreadTicket::class,

app/Ticket.php

class Ticket extends Model
{
   public function thread()
   {
        return $this->hasOne(ThreadTicket::class);
   }
}

So in your controller, you could do something like (or similar)

$ticket = Ticket::findOrFail($id);
$threadSubject = $ticket->thread->subject;

I'm going to close this for now, but if you have any questions please feel free to update.