【问题标题】:How can I get the user name who posted the post (laravel 5.8)如何获取发布帖子的用户名(laravel 5.8)
【发布时间】:2020-01-07 23:43:51
【问题描述】:

我正在尝试获取创建帖子的用户名女巫是(问题模型),我不知道有什么问题我尝试了Eager Loading在 laravel 文档中,我检查了这些 Questions 1 Questions 2 并且如果我使用 dd( $problem->user->name); 仍然得到 null 或 Trying to get property 'name' of non-object

我使用 laravel 5.8

ProblemsController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
use App\Problems ;
use App\Rules\Checkbox;
use Validator;
[...]
 public function index() 
    {
        //$users_row_num = User::count();
       // $problems_row_num = Problems::count();

       $problems = Problems::all();
       

        foreach ($problems as $problem) {
        /* Here should get name to send with view but I get null
         * if I try  $problem->user->name I get Trying to get property 'name' of non-object because user is null 
         */
            dd( $problem->user); 
        }
       
        return view('problems.index', [
            'problems' => $problem,
            'user_numder' => $users_row_num,
            'problem_number' => $problems_row_num,
        ]);
    }

[...]

Problems.php(模型)

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Problems extends Model
{
    
    public function user()
    {
        return $this->belongsTo('App\User');
    }
}

Usre.php(模型)

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
[...]

 public function problem()
    {
        return $this->hasMany('App\Problems');
    }

[...]
}

index.blade.php

 @foreach ($problems as $problem)
                            <tr>
                                <td>{{ $problem->accountNumber }}</td>
                                <td>{{ $problem->accountName }}</td>
                                <td>{{ $problem->accountEmail }}</td>
                                <td>{{ $problem->date }}</td>
                                <td>{{ $problem->problem }}</td>
                               
                                <td>{{ $problem->addedBy }}</td> <!-- Here I get user id (foreign key) I want to get name of that user -->
                               
                                <td>{{ $problem->comment }}</td>
                                <td>{{ $problem->solvedBy }}</td>
                                <td>{{ $problem->solved }}</td>
                                <td>{{ $problem->created_at->format('d/m/Y H:i') }}</td>
                                <td class="text-right"> </td>
                            </tr>
                            @endforeach

问题表

[...]
 public function up()
    {
        Schema::create('problems', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('accountNumber');
            $table->string('accountName');
            $table->string('accountEmail');
            $table->date('date');
            $table->longText('problem');
            $table->bigInteger('addedBy')->unsigned()->nullable();
            $table->longText('comment')->nullable();
            $table->string('solvedBy')->nullable();
            $table->boolean('solved');
            $table->timestamps();
        });
    }
[...]

ForingkeyAddedby 迁移

[...]
public function up()
    {
        Schema::table('problems', function(Blueprint $table){
            $table->foreign('addedBy')->references('id')->on('users')->onDelete('set null')->onUpdate('CASCADE');

        });
    }
[...]

我想使用$problem-&gt;user-&gt;name 来获取创建帖子的用户的姓名以将其显示在表格中

请帮忙。

谢谢大家。

【问题讨论】:

    标签: php laravel laravel-5.8


    【解决方案1】:

    您的 User 模型正在问题表中寻找user_id 字段以使关系正常工作。 Laravel 使用模型名称 + '_id' 自动为模型关系创建使用蛇形案例的关系。

    如果您将问题表上的外键更改为 user_id 而不是 addedBy,则可以正常使用。

    或者,如果你想保留 addedBy 键,你需要告诉 Laravel 你正在使用非标准键。所以,在你的 User 模型上:

    public function problem()
    {
        return $this->hasMany('App\Problems', 'addedBy');
    }
    

    应该可以解决问题。请参阅docs here

    不幸的是,您可能还没有在桌子上定义实际的 FK。在foreign 指定之前,我希望看到类似的内容:

    $table->unsignedInteger('user_id');
    $table->foreign('user_id')->references('id')->on('users');
    

    或者如果使用更新版本的 Laravel:

     $table->unsignedBigInteger('user_id');
     $table->foreign('user_id')->references('id')->on('users');
    

    docs for migrations are here for V6

    【讨论】:

    • 我已经尝试过这个return $this-&gt;hasMany('App\Problems', ''addedBy');,但即使我使用$problem-&gt;user 仍然有同样的问题,我得到null,这意味着我根本无法获得用户。
    • 看起来你有一个错字 - 我想这会因为额外的 ' 而失败,但如果没有在数据库上实际声明 FK,这将无法工作。如果您想这样做,您必须在foreign() 行上方输入类似$table-&gt;unsignedBigInteger('addedBy'); 的内容。强烈建议使用 user_id 来保持与惯例保持一致 - 它更容易。除非您将密钥正确添加到数据库,否则您将继续获得 null 用户。
    • 谢谢你,亲爱的,正如你所说,我已将 FK 的名称更改为 user_id。这是工作
    猜你喜欢
    • 2020-05-11
    • 2016-12-24
    • 2018-05-02
    • 2016-12-14
    • 2016-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多