app/Http/Controllers/MessagesController.php
/**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
Store a newly created resource in storage.
Store a newly created resource in storage .
public function store(Request $request)
    {
        $request->validate([
            'message' => 'required|max:500',
        ]);
        $groupId = $request->input('group_id');
        $senderId = auth()->user()->id;
        Message::create([
            'content' => $request->input('message'),
            'sender_id' => $senderId,
            'group_id' => $groupId
        ]);
        if(\App\Group::find($groupId)->users->where('id', $senderId)->isEmpty()){
            \App\Group::find($groupId)->users()->attach($senderId);
        }
        return Redirect::back();
    }
public function store ( Request $ request ) { $ request -> validate ( [ 'message' => 'required|max:500' , ] ) ; $ groupId = $ request -> input ( 'group_id' ) ; $ senderId = auth ( ) -> user ( ) -> id ; Message :: create ( [ 'content' => $ request -> input ( 'message' ) , 'sender_id' => $ senderId , 'group_id' => $ groupId ] ) ; if ( \ App \ Group :: find ( $ groupId ) -> users -> where ( 'id' , $ senderId ) -> isEmpty ( ) ) { \ App \ Group :: find ( $ groupId ) -> users ( ) -> attach ( $ senderId ) ; } return Redirect :: back ( ) ; }
Store a newly created resource in storage.
Store a newly created resource in storage .
//message can be max 500 character and cannot be null //checks which group the message is going to //takes senderid to display who sent the message