php - How to get last inserted value for each sender? -
i know can last inserted id using $insertedid = $user->id;
i'm trying last inserted value in view each sender sends data. inbox shown last message sent sender. how can show in view?
this post messaging happens.
public function postmessage(request $request) { $this->validate($request, [ 'recipient' => 'required', 'message' => 'required|max: 2000', ]); $message = new message(); $message->message_content = $request->message; $message->save(); $user = auth::user(); foreach($request->recipient $recipientid) { $message->users()->sync([ $recipientid => ['sender_id' => $user->id]],false ); } return redirect()->back(); }
retrieving values. i'm done here far retrieve user's message. still don't have idea how last inserted. how can achieve this?
public function getmessage() { $recipientlists = db::table('users')->where('id', '!=', auth::id())->get(); $messageresult = db::table('message_user')->select('messages.message_content', 'users.username', 'messages.id') ->join('users', 'users.id', '=', 'message_user.sender_id') ->join('messages', 'messages.id', '=', 'message_user.message_id') ->where('user_id', '=', auth::id()) ->where('sender_id', '=')->get(); return view ('message.create')->with('recipientlists', $recipientlists)->with('messageresult', $messageresult); }
migration (pivot table)
public function up() { schema::create('message_user', function (blueprint $table) { $table->increments('id'); $table->integer('message_id')->unsigned(); $table->foreign('message_id')->references('id')->on('messages')->ondelete('cascade'); $table->integer('user_id')->unsigned(); $table->foreign('user_id')->references('id')->on('users')->ondelete('cascade'); $table->unsignedinteger('sender_id')->nullable(); $table->foreign('sender_id')->references('id')->on('users')->ondelete('cascade'); $table->datetime('datereceived')->default(db::raw('current_timestamp')); $table->timestamp('datemodified')->default(db::raw('current_timestamp on update current_timestamp')); }); }
Comments
Post a Comment