GithubHelp home page GithubHelp logo

laravel-messenger's Introduction

#Laravel Messenger Basic Messaging System for Laravel4

Introduction

The main purpose of this package is providing an suited Foundation for extended communication platforms, based on laravel 4 (or compatible frameworks).

##Overview You have many users and they want to chat? Couldn't be simplier!

  • Participants have many conversations
  • A conversation belongs to many participants
  • A message belongs to a single conversation
  • A conversation consists of many Messages
  • Participants latest read timestamp is stored

##Installation

  • Add "andheiberg/messenger": "dev-master" to your composer.json
  • Run composer update
  • Add 'Andheiberg\Messenger\MessengerServiceProvider', to app/config/app.php under providers
  • Run php artisan messenger:setup
  • Add use \Andheiberg\Messenger\Traits\UserCanMessage; inside your User model class

Usage

Congratulations! Now you can use the laravel4 messenger.

php artisan messenger:setup will publish a Conversation and a Message model to app/models. If you prefer to make them your self, just extend Pichkrement\Messenger\Models\Conversation and Pichkrement\Messenger\Models\Message.

Examples

Now you can use it like a pro.

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        $user = Auth::user();
        $conversations = Conversation::forUser()->get();

        if ($conversations->isEmpty())
        {
            return Redirect::route('conversations.create');
        }

        return Redirect::route('conversations.show', [$conversations->last()->id]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        $user = Auth::user();
        $conversations = Conversation::forUser()->orderBy('updated_at', 'desc')->get();

        return View::make('conversations.create', compact('conversations'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store()
    {
        $input = Input::all();

        $conversation = Conversation::create([
            'subject' => $input['subject'],
        ]);

        $message = Message::create([
            'conversation_id' => $conversation->id,
            'user_id' => Auth::user()->id,
            'body' => $input['message'],
        ]);

        $sender = Participant::create([
            'conversation_id' => $conversation->id,
            'user_id' => Auth::user()->id
        ]);

        if ($this->input->has('recipient'))
        {
            $recipient = User::where('email', $input['recipient'])->first();
            Participant::create([
                'conversation_id' => $conversation->id,
                'user_id' => $recipient->id,
            ]);
        }

        return Redirect::route('conversations.index');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        $user = Auth::user();
        $conversations = Conversation::forUser()->orderBy('updated_at', 'desc')->get();
        $conversation  = Conversation::find($id);

        // $me = Participant::me()->where('conversation_id', $conversation->id)->first();
        // $me->last_read = new DateTime;
        // $me->save();

        return View::make('conversations.show', compact('conversations', 'conversation'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function createMessage($conversation)
    {
        return View::make('conversations.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function storeMessage($conversation)
    {
        $message = Message::create([
            'conversation_id' => $conversation,
            'user_id' => $this->auth->user()->id,
            'body' => $this->input->input('message'),
        ]);

        return Redirect::route('conversations.show', $conversation);
    }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.