Private message with laravel - laravel

I am building a simple private chat app with laravel and vue js.
I have a messages table with a column : id/sender_id/receiver_id and body
my main problem is fetching the users from the messages table. so if sender_id 1 has sent the message to receiver_id 2 then In the sender dashboard, I want to display the NAME of the receiver with receiver id 2 and on clicking the name I want to fetch conversation between these two users.
similarly, in the receiver dashboard, I want to show the list of all users who have sent a message.
so in short, as a logged-in user, I should see the name of users to whom I have sent a message and from whom I got the messages, and when clicking those names I will able to see all the conversations.
Message.php
class Message extends Model
{
use HasFactory;
public function user(){
return $this->belongsTo(User::class);
}
}
currently, I am doing like this
Route::get('/messages','MessageController#index');
public function index()
{
$messages = Message::with('user')
->groupBy('sender_id')
->where('receiver_id',auth()->user()->id)
->orWhere('sender_id',auth()->user()->id)
->get();
}
and my vue is like this
<li v-for="(person,index) in users" :key="index">
{{person.user.name}}{{person.user_id}}
</li>
export default{
data(){
return{
users:[],
messages:[],
}
},
mounted(){
axios.get('/api/users').then((response)=>{
this.users = response.data
})
}
//....
}
my problem is: if I (let's say john )sent a message to David(receiver), I see John name on David dashboard which is correct so on clicking John name I can fetch all the conversation between John and David However, On john's dashboard(message sender to David) I see John's name, but I need here David's name.
so on the sender dashboard, I need the receiver name and on the receiver dashboard, I need sender name.
I think this probably related to the query. please help me. Thanks

Related

Simple chat application using Laravel

I am trying to make simple chat application. I have already made relationship b/w user and chat_message using model. Just I want to know that when I am sending message to any user message is not sending in private chat. How can I do this?
Database
User: https://i.stack.imgur.com/eEaR2.png
Chat Mesasge: https://i.stack.imgur.com/rI2Zs.png
User Model
class User extends Authenticatable{
public function Chats() {
return $this->hasMany('App\Chat_message','id','to_user_id');
}
}
Chat_message
class Chat_message extends Model{
public function user(){
return $this->belongsTo('App\User');
}
}
Controller
public function chat($id){
$single_chat = User::with('Chats')->where('id',$id)->first();
return $single_chat;
}
Route
Route::get('/chat/{id}','HomeController#chat');
result
{"id":1,"name":"zubairMukhtar","email":"zubairmukhtar1992#gmail.com","email_verified_at":null,"job":null,"phone_number":null,"created_at":"2020-02-12 06:49:12","updated_at":"2020-02-12 06:49:12","chats":[]}
You should to fetch chat_messages data group by from_user_id, so that you will get all chat_messages data separated by user who sent a message, and based on you need to display those message on separate window.
Currently as per your current query you will get all messages on same window, so it will display all messages on same screen!
Hope this help!

Problem in showing additional field of pivot table in Larvel Nova

I am using Laravel Nova for my admin side.
I have this problem that in my pivot table, I added additional field.
I have user table and event table. User can have many event, and event can have many users. So I created a pivot table for the 2 models. But I want to the status of the event of user. So I added additional field for status in pivot table.
My problem now is in the Laravel Nova.
In my event resource, I have this:
BelongsToMany::make('Pending Users', 'pendingUsers', 'App\Nova\User'),
It shows another table for pending clients. I can already get the list of users in specific event but I can't show to the table the status of the event of user. If the user is approved or still pending.
What strategy is best in my case? I am thinking to make a model for Event and User.
I don't know how to show the additional field of pivot table in BelongsToMany in Laravel Nova. It just shows what I added in User Resource of Nova. Like ID and name. But I don't know where the status of user's event will show. I want to show it in the table of BelongsToMany of events to users.
You need to make sure that you're defining the relationship in BOTH models, and then letting Nova know which fields you want to show like this:
In your event resource:
BelongsToMany::make('Users')
->fields(function() {
return [
Text::make('status')
];
}),
In your event model:
public function users()
{
return $this->belongsToMany(User::class)->withPivot('status');
}
In your user model:
public function events()
{
return $this->belongsToMany(Event::class)->withPivot('status');
}
I got this working by defining ->withPivot('filed name)' on the relationship it drove me insane for a long time.
BelongsToMany::make('your_relationship')
->fields(function () {
return [
Text::make('your_text'),
];
})
Your relation will be here something just like this what i said previous
public function users()
{
return $this->belongsToMany(User::class)->withPivot('filed name');
}

Laravel - Multiple Arrays for Multiple Recipients (Mailable/Notifications)

I'm looking for advice for a project and an interesting predicament I've put myself in.
I have multiple fields in my shipment model, they are:
shipto_id
shipfrom_id
billto_id
They all link (through different relationships) to my customer model:
public function billtoAccount()
{
return $this->belongsTo('App\Customer', 'bill_to');
}
public function shiptoAccount()
{
return $this->belongsTo('App\Customer', 'ship_to');
}
public function shipfromAccount()
{
return $this->belongsTo('App\Customer', 'ship_from');
}
and these customers (in reality would likely be better described as customer accounts (these are NOT USER ACCOUNTS, they're more just like a profile for each company that business is done with)) can have a multitude of users associated with them.
public function users()
{
return $this->belongsToMany(User::class);
}
Now, while I know how to send off mailables and notifications, I was curious to know how I would go about sending off those to multiple user's emails. So let me describe the following: Something is created and the following customers (and in turn, their users) are referenced.
(billto_id) - Customer Account 1 - User 1 (email1#example.com)
(shipto_id) - Customer Account 2 - User 2 (email2#example.com) & User 3 (email3#example.com)
(shipfrom_id) - Customer Account 37 - User 6 (email4#example.com)
Now, how would I go about moving the emails of the users over to an array of emails to have a notification or mailable sent to them?
So it should pop out: email1#example.com, email2#example.com, email3#example.com, email4#examples.com
Elaborating on #Devon 's comment:
This is business logic. You could have a method on your Shipment model that returns the customer instances to be notified as an array, e.g. getNotifiables() : array
Then in your Customer model you may use the Notifiable trait
use Illuminate\Notifications\Notifiable;
And looping over your Notifiables, i.e. Customers
$notification = new ShipmentWasCreated()
foreach ($shipment->getNotifiables() as $notifiable) {
$notifiable->notify($notification);
}
Better alternative:
Notification::send($shipment->getNotifiables(), new ShipmentWasCreated());

Laravel get related data from 3 tables

I have three tables: users, emails,attachments. User table is connected with emails by user_id. Emails table is connected with attachments by email_id.
My question is: How should I make it look eloquent in laravel to get all users their emails and their attachments? (I know how get all user and they emails but I don't know how to add attachments.)
Depending on your database relationship,you may declare a relationship method in your Email model, for example:
// One to One (If Email has only one attachment)
public function attachment()
{
return $this->hasOne(Attachment::class);
}
Otherwise:
// One to Many (If Email contains more than one attachment)
public function attachments()
{
return $this->hasMany(Attachment::class);
}
To retrieve the related attachment(s) from Email model when reading a user using id, you may try something like this:
$user = User::with('email.attachment')->find(1); // For One-to-One
$user = User::with('email.attachments')->find(1); // For One-to-Many
Hope you've already declared the relationship method in the User model for Email model using the method name email.
Note: make sure, you have used right namespace for models. It may work if you've done everything right and followed the Laravel convention, otherwise do some research. Also check the documentation.

Pulling a value from related table in a controller

I'm using Yii2 framework for the first time, trying to implement it in a project.
I've got a dropdown list of customers where i'd like to also show customer's company next to customer's name.
Customer and Company tables are related. Here's what it looks like in a Customer model:
public function getCompany()
{
return $this->hasOne(Company::className(), ['id' => 'company_id']);
}
So now i'm forming a dropdown list containing customer's name, email, phone and company name. Name, email, and phone belong to one table, so there's no problem with pulling them together. Here's what it looks like in Customer model:
public function getfullInfo()
{
return $this->name.' '.$this->phone.' '.$this->email;
}
I don't really understand this framework's logic. How do I pull in Company's name in above code?
Thank you guys.
Here's the correct code:
public function getfullInfo()
{
return $this->name.' '.$this->phone.' '.$this->email.' '.$this->company['name'];
}
This was so easy and I've wasted so much time on this.

Resources