[laravel] Creating a messenger that can send to multiple users

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 1420 Views - Last Post: 04 January 2015 - 04:30 AM Rate Topic: -----

#1 typhlosion   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 121
  • Joined: 09-December 13

[laravel] Creating a messenger that can send to multiple users

Posted 02 January 2015 - 09:23 AM

Hi people of DIC,

I have a bit of a issue, well more the fact I don't know how to do something with in a framework (Laravel) and I can't seem to find the help online, mostly because I am not sure what to search for on Google.

I would like a messaging system on the app I am developing but I am very new to the Laravel framework and MVC of any form ....

Here is the part of migration that I am building at the moment for "call_logs".

Basically the idea of "call logs" as you can imagine is a call logging part of the program, the user that takes the call will take information and then send that info to the appropriate people.


Schema::table('call_logs', function(Blueprint $table)
    {
        $table->integer('id')->primary();

        $table->integer('taken_by')->unsigned();
        $table->foreign('taken_by')->references('id')->on('users');

        $table->integer('to')->unsigned();
        $table->foreign('to')->references('id')->on('users');

        $table->string('title');
        $table->string('subject');
        $table->text('content', 2000);


        $table->softDeletes();
        $table->timestamps();
    });



So I need 'to' to be some sort of array of users to send to, how is this achieved, also I don't want to have to come across the n+1 issue with saving the same message for multiple users.....how do I get around this.

Is This A Good Question/Topic? 0
  • +

Replies To: [laravel] Creating a messenger that can send to multiple users

#2 typhlosion   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 121
  • Joined: 09-December 13

Re: [laravel] Creating a messenger that can send to multiple users

Posted 02 January 2015 - 10:34 AM

I have redone this since as I think I may have found the answer, answers and comments will still be appreciated however.

		Schema::table('messages', function(Blueprint $table)
		{
			$table->integer('id')->primary();

			$table->integer('taken_by')->unsigned();
			$table->foreign('taken_by')->references('id')->on('users');

			$table->integer('to')->unsigned();
			$table->foreign('to')->references('id')->on('users');

			$table->integer('content_id')->unsigned();
			$table->foreign('content_id')->references('id')->on('message_contents');

			$table->string('title');
			$table->string('subject');
			
			$table->softDeletes();
			$table->timestamps();
		});
		Schema::table('message_contents', function(Blueprint $table)
		{
			$table->integer('id')->primary();

			$table->text('content', 2000);
		});

Was This Post Helpful? 0
  • +
  • -

#3 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4241
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: [laravel] Creating a messenger that can send to multiple users

Posted 02 January 2015 - 04:09 PM

If you wanted to create a single call_log, and share that among multiple users, you are still a bit off target.

Generally, when you want to save arrays of things in a relational database, what you need to do is create a second table and put the items in there, linked back to the original row. - A One-To-Many (1:N) relationship.

In your case, it might look something like this:
call_log
+----+---------+
| id | message |
+----+---------+
|  1 | ...     |
|  2 | ...     |
+----+---------+

call_log_recipients
+-------------+---------+
| call_log_id | user_id |
+-------------+---------+
|           1 |    1234 |
|           1 |    1467 |
|           2 |    1327 |
|           1 |    1378 |
+-------------+---------+


There the call_log_recipients list references the same message (1) three times, each time linked to a separate user. This way you can link individual messages to as many users you want without having to duplicate anything.

As far as I can see, the code you posted already has all the elements required to put that schema together. (Though I'm not experienced with Laravel specifically.)
Was This Post Helpful? 0
  • +
  • -

#4 typhlosion   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 121
  • Joined: 09-December 13

Re: [laravel] Creating a messenger that can send to multiple users

Posted 03 January 2015 - 03:58 AM

That does make a whole lot of sense =]

But how about speed, being a messaging system I don't want there to be much of a lag this is my main concern, the way you have showed me which I am guessing is a "pivot" table should be the fastest way for this ?

I can't see any other way of making it faster.

could I use a JSON object as 'send_to' ? or something like this ?

as of now I think I will stick to what you have recommended as it seems the best solution ;)
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,994
  • Joined: 08-August 08

Re: [laravel] Creating a messenger that can send to multiple users

Posted 03 January 2015 - 06:13 AM

You could do it in straight PHP. Remember that a framework needs to load in its own files before opening yours. There's lots of stuff going on and it all takes time. For each user it's not a lot of time, but when you have many users it can add up. You've got two choices:

  • Speed up development time with a framework.
  • Speed up your site with lean PHP code.

Pick one.

This post has been edited by CTphpnwb: 03 January 2015 - 06:33 AM

Was This Post Helpful? 0
  • +
  • -

#6 typhlosion   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 121
  • Joined: 09-December 13

Re: [laravel] Creating a messenger that can send to multiple users

Posted 03 January 2015 - 06:30 AM

To be honest I think learning a framework like Laravel is just teaching me how to code lean PHP, I am new to the concept of MVC so Laravel helps alot with that =]

Guess I'll have to stick with frameworks for now and then once I am more competent with PHP and well...programming as a whole, I will worry more about such problems.
Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,994
  • Joined: 08-August 08

Re: [laravel] Creating a messenger that can send to multiple users

Posted 03 January 2015 - 06:35 AM

IMHO, Laravel tries to do so many things for you that I think it makes it harder. Not only do you need to learn PHP, but you also have to learn how Laravel does the things it does!
Was This Post Helpful? 0
  • +
  • -

#8 typhlosion   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 121
  • Joined: 09-December 13

Re: [laravel] Creating a messenger that can send to multiple users

Posted 03 January 2015 - 06:40 AM

well it works pretty much how any other MVC structure framework would work, I think it was heavily inspired by Ruby on Rails the Ruby MVC framework which is IMHO a lot better then any PHP framework but that's just my taste.

But yes plain PHP in the right hands is better then any framework out there, it's just for me...I don't have PHP hands yet XD I am relatively new to programming TBH
Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,994
  • Joined: 08-August 08

Re: [laravel] Creating a messenger that can send to multiple users

Posted 03 January 2015 - 07:05 AM

Check the attached code out. It's just a simple example showing one way to organize code. You can use it to roll your own MVC without adding all the overhead and 1complications of a framework.

1Look at Laravel's templating system. It's not terribly complex, but it's got its own set of rules that you need to learn and remember, but only when using Laravel. Use another framework and they're useless. That's another problem with frameworks like these!

Edit: Added the code!

Attached File(s)


This post has been edited by CTphpnwb: 03 January 2015 - 07:14 AM

Was This Post Helpful? 1
  • +
  • -

#10 typhlosion   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 121
  • Joined: 09-December 13

Re: [laravel] Creating a messenger that can send to multiple users

Posted 03 January 2015 - 07:18 AM

I hear the same thing from a lot of experienced PHP programmers, my boss being one of them XD, and yes I see what you mean and knew this from the start TBH. =[

The problem I have at the moment is that the project I am undertaking is pretty big and I lack in advanced PHP knowledge and programming over all, so I see frameworks like Laravel like having stabilizers on your bike XD

I am also learning Ruby and more specifically Rails although PHP is my native language so to speak XD

What do you think of Ruby on Rails ? I go to a coding club and all the guys there are hardcore Ruby Developers and they seem to love the Rails framework or are you just a hard core PHP man yourself? =p

This post has been edited by typhlosion: 03 January 2015 - 07:21 AM

Was This Post Helpful? 0
  • +
  • -

#11 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,994
  • Joined: 08-August 08

Re: [laravel] Creating a messenger that can send to multiple users

Posted 03 January 2015 - 07:25 AM

I don't use Ruby. I spend most of my time in C/C++, PHP, Javascript, some Obj-C, and now Swift. I see frameworks as necessary evils. For example, the purpose of Swift is to write code for IOS and for the Mac, so it would be pointless to use it without Apple's frameworks. Still, it's the frameworks that are most of the work! Sure, they have huge advantages and they help make for a consistent user interface, but that doesn't mean they're ideal!
Was This Post Helpful? 0
  • +
  • -

#12 typhlosion   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 121
  • Joined: 09-December 13

Re: [laravel] Creating a messenger that can send to multiple users

Posted 03 January 2015 - 07:38 AM

I think maybe it is best then to develop your own "framework" surely the more you develop in a language the more structure your naturally start to evolve.

Is MVC the best way to organize a project however, when you work on a large project do you find yourself doing alot of "framework" type code before hand, or do you just feel out the project as you go?

I see why ready made frameworks like Laravel can be a pain in the ass, but ATM I feel exploring different frameworks can benefit a novice developer at least like myself :D/> just seeing different ways experienced PHP developers have tried to tame PHP XD

But yes I think it best not to rely too much on frameworks.

But like I say do you build a "framework" of code for each project, each project surely has it's own organised structure so that other developers can understand it quicker.

This post has been edited by typhlosion: 03 January 2015 - 07:39 AM

Was This Post Helpful? 0
  • +
  • -

#13 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,994
  • Joined: 08-August 08

Re: [laravel] Creating a messenger that can send to multiple users

Posted 03 January 2015 - 07:45 AM

I think that since web sites are generally broken up into pages you don't have to think of them as large projects. You simply develop one page at a time, using a consistent, modular approach. For example, often I'll have a "user" class that keeps track of the user's status: logged in or not, level of capabilities, etc. The class can be included and inherited from in the various 1pages and this can help keep things consistent.


1Remember that PHP does not have pages, only files. You need to write your code to create HTML pages!
Was This Post Helpful? 0
  • +
  • -

#14 typhlosion   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 121
  • Joined: 09-December 13

Re: [laravel] Creating a messenger that can send to multiple users

Posted 03 January 2015 - 08:46 AM

Yes things like users will be universal to any project but yet still different project will need different attr for users.

I am currently building a in browser app for the company I work for which is a computer repair company, so we will need a job list, call logger....and things of that nature just a full logging system for the company, and again "users" are actually "technicians" in the app and they hold their own special attr =]

I just feel "safer" using Laravel to build this app at the moment, even if it means having to re build it later on, and to be honest I really wouldn't mind that, by time that day comes, and I am sure it will like you say, I should be more prepared for it ;)/> as long as I keep up with learning actual PHP in mean time =]

This post has been edited by typhlosion: 03 January 2015 - 08:49 AM

Was This Post Helpful? 0
  • +
  • -

#15 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,994
  • Joined: 08-August 08

Re: [laravel] Creating a messenger that can send to multiple users

Posted 03 January 2015 - 09:33 AM

View Posttyphlosion, on 03 January 2015 - 10:46 AM, said:

Yes things like users will be universal to any project but yet still different project will need different attr for users.

The ability to create multiple types of users (Admin, moderator, standard user, guest, etc.) is built into OOP. You might start with a guest and inherit their capabilities, adding more for standard users, etc. Frameworks put their own spin on this and present it as if it were something they invented!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2