从laravel中的mysql表中检索值

时间:2017-01-09 08:28:36

标签: php mysql laravel-5

我的MessageController如下:

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
use App\Http\Requests;
use App\Message;


class MessageController extends Controller
{
    public function CreateMessage(Request $request)
    {

        $meso = new Message();
        $meso->body = $request['body'];
        $meso->subject = $request['subject'];


        $request->user()->messages()->save($meso) ;


       return redirect()->route('mail');
    }
    public function getmailbox()
    {
        $message = Message::orderBy('created_at', 'desc')->get();
        return view('dashboard', ['messages' => $message]);
    }
}

我试图在mail.blade.php中找到这些消息:

<table class="table table-inbox table-hover">
                            <tbody>
                              @foreach($messages as $message)
                              <tr class="unread">
                                  <td class="inbox-small-cells">
                                      <input type="checkbox" class="mail-checkbox">
                                  </td>
                                  <td class="inbox-small-cells"><i class="fa fa-star"></i></td>
                                  <td class="view-message dont-show">Facebook</td>
                                  <td class="view-message view-message"><p>{{ $message->body }}</p></td>
                                  <td class="view-message inbox-small-cells"><i class="fa fa-paperclip"></i></td>
                                  <td class="view-message text-right">feb 14</td>
                              </tr>
                          </tbody>
                          @endforeach
                          </table>

然而,这是我从该查询得到的结果: ed6d9659a06e690c67323133798cd26570bdc7a9.php第200行中的 ErrorException: 未定义的变量:消息(查看:C:\ xampp \ htdocs \ dating \ resources \ views \ mail.blade.php) 这里有任何帮助

1 个答案:

答案 0 :(得分:0)

Laravel 5

更改功能结构:

public function CreateMessage(Request $request)
{

    $meso = new Message();
    $meso->body = $request['body'];
    $meso->subject = $request['subject'];

    $meso->save();
    //$request->user()->messages()->save($meso) ;

    /**
    * You can get the last inserted ID 
    *
    * If your message table having primaty key column name is id
    */

    // $last_insert_id = $meso->id;


    return redirect()->route('mail');
}
public function getmailbox()
{
    $message = Message::orderBy('created_at', 'desc')->get();
    return view('mail', ['messages' => $message]);
}