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!
Related
i'm have a order able in DB that has related to product and service and other models, between these tables and models there is a many to many polymorph
i want to show client all his order
in future, maybe, the model change in name or amount
is there anyway to use namespace of model that is stored middle table instead specific method?
for exammple i dont want use
public function products()
{
return $this->morphedByMany(Product::class, 'orderable');
}
public function services()
{
return $this->morphedByMany(Service::class, 'orderable');
}
and etc
instead:
public function morhToAll(){
?????
}
and then for example i can:
$orderItems=auth()->user()->orders()->morhToAll
foreach(orderItems as $item){
.....
}
the usr can see all his orders and their details.
i want to show client his/her orders details
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
Problem
I have two classes, Users & Posts. A user "hasMany" posts and a post "belongTo" a user. But when I call "User::all()" it doesn't automatically pull the users posts for obvious reasons, because if my user had relations to 100 different tables pulling all users would start to become pretty chunky.
Question
Is there a way to pull all users and all user->posts in one or few lines of code without going through a foreach loop?
I know i can use a mutator but the problem I have is my field is called user_id and i have tested it with this code:
public function getUserIdAttribute($id)
{
return User::find($id);
}
But it will replace "user_id" field value with a user object, Id rather have it set to its own "temporary user" field within the result. I'm trying to find best practice!
Thank you in advance.
What you're looking for is called Eager Loading
Inside your post model :
class Post extends Model
{
protected $table='posts';
public $primaryKey='id';
public function user(){
return $this->belongsTo('App\User','user_id');
}
}
now you want to get post with user use below code :
$posts=Post::with('user')->get();
inside your user model :
class User extends Model
{
public function posts(){
return $this->hasMany('App\Model\Post');
}
}
now you want to get a user with all posts :
$user=User::where('id',$id)->first();
$user_posts=$user->posts;
I am trying to create relation between two tables, users and messages in Laravel models, as the user can send a message to another user so that I have two foreign-keys (fromUser_id and toUser_id) as shown in the image below.
For the first relation it is straightforward that I will create a function with the name messages
public function messages(){
return $this->hasMany('App\Models\Message', 'fromUser_id');
}
However I do not know how to name the second relation as far as I know it should be messages too, according to the standard naming of Laravel, which will obviously issue an error as we have the first function with the same name.
public function messages(){
return $this->hasMany('App\Models\Message', 'toUser_id');
}
Would you please let me know what should I name it and how this will affect the models.
Well, you should not use simple messages as relationship but rather use receivedMessages and sentMessages like this:
public function sentMessages()
{
return $this->hasMany('App\Models\Message', 'fromUser_id');
}
public function receivedMessages()
{
return $this->hasMany('App\Models\Message', 'toUser_id');
}
hello guys I have this set of database
clients(#id,raison_sociale,adresse,)<br>
delivery(#id,client_id(fk),date_del) <br>
details(delivery_id(fk),product_id(fk),qt)
product(id,code,desig,price)
and here are my eloquent relationships
class Client extends Model {
public function livraison (){
return $this->hasMany('App\Models\Livraison'); } }
class Detail extends Model
{
public function delivery (){
return $this->belongsTo('App\Models\Delivery');}}
class Livraison extends Model{
public function client (){
return $this->belongsTo('App\Models\Client');
}
public function detail (){
return $this->hasMany('App\Models\detail'); }}
Do I have something wrong with these relationships ?
I have a page where I show all of my delivery but I can't find the correct way to fetch this row:
raison_soc(from client) & date_del(from delivery); and details table (for each client of course )
use App\Models\Client;
Client::with('delivery.detail')->get();
I can't test your relationships but assuming you are following the naming conventions your relations should be fine. The above will grab all details that are associated to the delivery by the client.
You could improve your relationship with a hasManyThrough method Has Many Through
You would do something like this in your Client class:
public function details(){
return $this->hasManyThrough('App\Models`Details', 'App\Models\Livarasion');
}
If you explain your 2nd point more clearly I can help you adjust this. Please let me know if you have any issues