如何使用 Eloquent 从两个不同的表中检索数据

我正在设置一个系统来检查用户是否向另一个用户发送了消息。


消息控制器.php


use App\Message;

use App\User;


class MessagesController extends Controller

{

    public function index()

    {

        return view('admin.messages')->with('messages', Message::all())

                                     ->with('users', User::all());


    }

消息迁移有以下数据:


Schema::create('messages', function (Blueprint $table) {

    $table->increments('id');

            $table->integer('from');

            $table->integer('to');

            $table->mediumText('text');

            $table->integer('status');

            $table->timestamps();

});


和 web.php


Route::get('/admin/messages', [

        'uses' => 'MessagesController@index',

        'as' => 'messages'

    ]);


所以我们的想法是呈现一个带有消息数据的面板,显示:


<tbody>

    <tr>

        @foreach ($messages as $message)


        <td>

            {{ $message->from}}

        </td>

        <td>

            {{ $message->to}}

        </td>

        <td>

            {{ $message->text}}

        </td>

        <td>

            {{ $message->status}}

        </td>

        <td>

            {{ $message->created_at}}

        </td>


        @endforeach

    </tr>

</tbody>


这将正确加载 Message 表上的所有信息。但是,它会显示“from”和“to” as 和 ID,因为它应该。


我希望通过 Message 表和 Users 表之间的关系填充该表,而不是用户的 ID,而是用户的名称。


我错过了什么?


素胚勾勒不出你
浏览 141回答 2
2回答

慕的地6264312

首先,您需要定义您的关系。看起来 aUser有很多Messages,所以:消息.phppublic function sender(){&nbsp; &nbsp; return $this->belongsTo(User::class, 'from');}public function receiver(){&nbsp; &nbsp; return $this->belongsTo(User::class, 'to');}注意:这里我使用了两个带有自定义外键的belongsTo关系,这是因为 Laravel{model}_id默认会查找列,但这样 Laravel 将知道要搜索的列。然后在您的控制器中,您可以返回您的消息,但Eager Loading您需要的关系以便能够在您的视图中显示它们:use App\Message;use App\User;class MessagesController extends Controller{&nbsp; &nbsp; public function index()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $messages = Message::with(['sender', 'receiver'])->get();&nbsp; &nbsp; &nbsp; &nbsp; return view('admin.messages')->with('messages', $messages);&nbsp; &nbsp; }}那么在你看来:<tbody><tr>&nbsp; &nbsp; @foreach ($messages as $message)&nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{ $message->sender->name}}&nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{ $message->receiver->name}}&nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; // ...&nbsp; &nbsp; @endforeach</tr></tbody>观察鉴于主键是正整数,请将外键类型更新为:Schema::create('messages', function (Blueprint $table) {&nbsp; &nbsp; &nbsp; &nbsp; $table->increments('id');&nbsp; &nbsp; &nbsp; &nbsp; $table->unsignedInteger('from'); // <-----&nbsp; &nbsp; &nbsp; &nbsp; $table->unsignedInteger('to');&nbsp; // <------&nbsp; &nbsp; &nbsp; &nbsp; $table->mediumText('text');&nbsp; &nbsp; &nbsp; &nbsp; $table->integer('status');&nbsp; &nbsp; &nbsp; &nbsp; $table->timestamps();&nbsp; &nbsp; });这将帮助您避免将来出现一些麻烦。此外,在 Laravel 中5.8,主键默认为 Big Increments。所以如果你使用这些,你的外键应该是这样的:$table->unsignedBigIncrement('column_name');

慕桂英4014372

在您的消息模型中,您必须定义关系。我们定义了 from 和 to 关系,这些将需要一个额外的参数来定义键名,因为它不是您遵循的 Laravel 标准的一部分。public class Message{&nbsp; &nbsp; public function from()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return $this->belongsTo(User::class, 'from');&nbsp; &nbsp; }&nbsp; &nbsp; public function to()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return $this->belongsTo(User::class, 'to');&nbsp; &nbsp; }}完成此操作后,您将能够执行类似的操作。public function index(){&nbsp; &nbsp; return view('admin.messages')->with('messages', Message::all())}如果像这样访问用户 $message->from,您将获得一个对象。如果您访问该方法,您将获得查询构建器,例如 $message->from()。现在您可以在刀片模板中使用它。<tbody>&nbsp; <tr>&nbsp; &nbsp; @foreach ($messages as $message)&nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; {{ $message->from->name}}&nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; {{ $message->to->name}}&nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; ....&nbsp; &nbsp; @endforeach&nbsp; </tr></tbody>
打开App,查看更多内容
随时随地看视频慕课网APP