0

I know you can get the last inserted id using this $insertedId = $user->id; But I'm trying to get the last inserted value in my view for each sender who sends the data. Just like a inbox it always shown the last message that sent by the sender. How can I show this in my view?

This is my POST where 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 as $recipientId)
    {
        $message->users()->sync([ $recipientId => ['sender_id' => $user->id]],false );
    }

    return redirect()->back();
}

Retrieving the values. What I'm done here so far I just retrieve all the user's message. Still don't have an idea how to get the last inserted. Any help how can I 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'));
    });
}
Francisunoxx
  • 1,440
  • 3
  • 22
  • 45
  • Do you have `->timestamps()` on your tables in your migration? – Ohgodwhy Aug 08 '16 at 13:56
  • @Ohgodwhy Yes I have. Please see my updated post :) – Francisunoxx Aug 08 '16 at 13:57
  • Not a good solution, but a quick solution. Store your last insert id into SESSIONS. Then you would be able to access it in any page. You can even store array of last insert id, in SESSION, if needed. – Ajit Kumar Singh Aug 08 '16 at 14:01
  • @AjitKumarSingh I'm gonna try this. Thanks for this! – Francisunoxx Aug 08 '16 at 14:03
  • @Francisunoxx Also I'm pretty sure you can return the last insert id from `postMessage()` function as parameter. – Ajit Kumar Singh Aug 08 '16 at 14:04
  • @AjitKumarSingh Did you already tried this? – Francisunoxx Aug 08 '16 at 14:05
  • @Francisunoxx The last time I used laravel(5.0) was about 6months ago or maybe more. So Im really sorry. Im fuzzy about it. – Ajit Kumar Singh Aug 08 '16 at 14:06
  • @AjitKumarSingh I see. Can you give it a try so I can have idea :) – Francisunoxx Aug 08 '16 at 14:08
  • @Francisunoxx Have a look: http://stackoverflow.com/questions/21084833/laravel-get-last-insert-id-using-eloquent – Ajit Kumar Singh Aug 08 '16 at 14:09
  • Is it supposed to be show only after posting data? In this case you may do return redirect()->back()->withInput($someData); and then in method that serves GET request fetch it from there using $request->old() – Artyom Sokolov Aug 08 '16 at 14:21
  • @ArtyomSokolov Not really only to be show after posting data. I just wanted to get the last inserted values for each sender. Can you give some example? I can't visualize it. – Francisunoxx Aug 08 '16 at 14:23
  • Well, obviously, last inserted message will have the most recent `dateReceived`. So, something like `->orderBy('dateReceived', 'desc')->first()` will do the job, won't it? – Artyom Sokolov Aug 08 '16 at 18:16
  • I would order the messages by user_id and then by dateReceived or dateModified descending. So when looping through all the messages of a user, whenever the sender changes you first get the last sent message of that person. Is that what you try to achieve? – Kjell Aug 08 '16 at 18:56

0 Answers0