如何获取 post 编辑了 post (laravel 5.8) 的用户名

How can I get the user name who posted the post (laravel 5.8)

我试图获取创建 post 的用户名是 (问题模型),我不知道有什么问题我已经尝试 Eager Loading in laravel documentation and I have check these 并且仍然得到 null 或 Trying to get property 'name' of non-object 如果我使用 dd( $problem->user->name);

我用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

问题table

[...]
 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();
        });
    }
[...]

迁移添加的 Foringkey

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

        });
    }
[...]

我想使用 $problem->user->name 获取创建 post 的用户名,以便在 table

中显示

请帮忙

谢谢大家。

您的 User 模型正在问题 table 中寻找 user_id 字段来建立关系。 Laravel 使用模型名称 + '_id' 为模型关系使用 snake case 自动创建关系。

如果您将问题 table 的外键从 addedBy 更改为 user_id,这将照原样工作。

或者,如果您希望保留 addedBy 密钥,则需要告知 Laravel 您使用的是非标准密钥。因此,在您的 User 模型上:

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

应该可以解决问题。见 docs here.

不幸的是,您似乎还没有在 table 上定义实际的 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.