Laravel:如何在从db获取数据并将修改后的数据发送到视图后使用用户定义的函数?

时间:2018-01-05 15:00:19

标签: php laravel laravel-5.5

我有简单的收件箱控制器

代码:InboxController

<?php

namespace App\Http\Controllers;

use App\User;
use App\Message;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class InboxController extends Controller
{
    public function index()
    {
        $array = array('messages'=>$this->user_messages());
        return view('index', $array);
    }

    public function user_messages()
    {
        $user_id = Auth::user()->id;
        $messages = Message::where('to', $user_id)->orderBy('created_at','desc')->get();
        return $messages;
    }

    // Compose
    public function compose(Request $request)
    {
        if($request->isMethod('post'))
        {

            // Sended data from html form 
            $to      = $request->to;
            $from    = Auth::user()->id;
            $subject = $request->subject;
            $message = $request->message;

            // Get info about receiver user

            $receiver_user_id = User::where('email', $to)->first()->id;
            $receiver_user_type = User::where('id', $receiver_user_id)->first()->type;  


            // Get info about sender user 
            $sender_user_type = Auth::user()->type;

            // Send message
            Message::create(
                [
                    'subject'=>$subject, 
                    'message'=>$message, 
                    'from'=>$from, 
                    'to'=>$receiver_user_id
                ]
            );
        }

        $array = array('messages'=>$this->user_messages());

        return view('compose', $array);
    }

    // Read message
    public function message(Request $request, $id=false)
    {
        $messages = Message::where('id', $id)->get();
        $subject = $messages->first()->subject;
        $message = $messages->first()->message;
        $status  = $messages->first()->status;
        $date    = $messages->first()->created_at;
        $sender_id = $messages->first()->from;
        $sender = User::where('id', $sender_id)->first()->name;
        $array = [
                    'subject' => $subject,
                    'message' => $message,
                    'date'    => $date,
                    'messages'=> $messages,
                    'sender'  => $sender,
                    'status'  => $status
                 ];
        Message::where('id', $id)->update(['status' => 1]);

        return view('message', $array);
    }
}

index()方法上,我将返回$array,其中包含当前用户的所有消息。在使用messages的{​​{1}}视图中,我将显示所有用户消息:

代码:messages.blade.php

foreach

此处所有人都留言<tbody> @foreach($messages as $message) <tr @if($message->status == 0) class="unread" @elseif($message->status == 1) class="" @endif > <td class="inbox-small-cells"> <div class="checkbox checkbox-default inline-block"> <input type="checkbox" id="checkbox012"/> <label for="checkbox012"></label> </div> <i class="zmdi zmdi-star inline-block font-16"></i> </td> <td class="view-message dont-show"> <a href="{{ route('message') }}/{{$message->id}}">{{$message->subject}}</a> @if($message->status == 0) <span class="label label-warning pull-right">new</span> @endif </td> <td class="view-message ">{{$message->message}}</td> <td class="view-message text-right"> <i class="zmdi zmdi-attachment inline-block mr-15 font-16"></i> <span class="time-chat-history inline-block">{{$message->created_at}}</span> </td> </tr> @endforeach </tbody> 。在这里,我使用消息ID创建消息链接:

id

我在这里如何使用我的函数编码<a href="{{ route('message') }}/{{$message->id}}">{{$message->subject}}</a> 消息进行编码并在链接中显示。当用户点击消息时,我将使用我的另一个函数id并通过id从db获取消息。一般来说,我可以将我的功能用于decodeencode

我的id

的编码和解码函数
decode

3 个答案:

答案 0 :(得分:2)

您可以使用encrypt()decrypt()助手。这些助手使用Laravel encrypter

建立链接:

{{ route('message') . '/' . encrypt($id) }}

然后解密它:

Message::find(decrypt($id));

答案 1 :(得分:2)

Laravel有助手方法encrypt()decrypt(),可以在代码中的任何位置使用。这些函数encrypt使用您在配置中设置的唯一加密密钥值。

因此,在您看来,您可以使用

<a href="{{ route('message') }}/{{ encrypt($message->id) }}">{{$message->subject}}</a>

在你的控制器中:

$message = Message::where('id', decrypt($id))->first();

答案 2 :(得分:1)

首先制作一个Utility类,并将所有方法设为静态。

public class Utility{
   public static function generate_xor_key($length)
   {
      $result = array_fill(0, $length, 0);

      for ($i = 0, $bit = 1; $i < $length; $i++) {
      for ($j = 0; $j < 3; $j++, $bit++) {
           $result[$i] |= ($bit % 2) << $j;
      }
}

return implode('', array_map('chr', $result));
}

 public static function number_encode($id, $encodedLength = 7, $rawBits = 16, $key = null)
{
$maxRawBits = $encodedLength * 3;
if ($rawBits > $maxRawBits) {
    trigger_error('number_encode(): $rawBits must be no more than 3 times greater than $encodedLength');
    return false;
}

if ($key === null) {
    $key = $this->generate_xor_key($encodedLength);
}

$result = array_fill(0, $encodedLength, 0x30);

for ($position = 0; $position < $rawBits; $position++) {
    $bit = (($id >> $position) & 0x01) << floor($position / $encodedLength);
    $index = $position % $encodedLength;
    $result[$index] |= $bit;
}

do {
    $index = $position % $encodedLength;
    $bit = ($position % 2) << floor($position / $encodedLength);
    $result[$index] |= $bit;
} while (++$position < $maxRawBits);

return implode('', array_map('chr', $result)) ^ $key;
}

public static function number_decode($id, $encodedLength = 7, $rawBits = 16, $key = null)
    {
    if ($key === null) {
        $key = $this->generate_xor_key($encodedLength);
    }

    $bytes = array_map(
    'ord',
    str_split(
        str_pad($id, $encodedLength, '0', STR_PAD_LEFT) ^ $key,
        1
       )
   );

   $result = 0;

   for ($position = 0; $position < $rawBits; $position++) {
       $index = $position % $encodedLength;
       $bit = (($bytes[$index] >> floor($position / $encodedLength)) & 0x01) << $position;
       $result |= $bit;
   }

   return $result;
     }
   }

在您看来 -

<a href="{{ route('message') }}/{{Utility::number_encode($message->id)}}">{{$message->subject}}</a>

在你的控制器中 -

$messages = Messages::find(Utility::number_decode($encoded_id));//do your task here after decrypt

如果您不想要一个单独的课而不想要帮助函数,那么您可以关注this链接。