您的用户
我试图获取创建帖子巫婆的用户名是((问题模型)),我不知道我在laravel文档中尝试过Eager Loading并出了问题,我已经检查了这些Questions 1 Questions 2,如果我使用Trying to get property 'name' of non-object
]仍然会为null或dd( $problem->user->name);
我使用laravel 5.8
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,
]);
}
[...]
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Problems extends Model
{
public function user()
{
return $this->belongsTo('App\User');
}
}
<?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');
}
[...]
}
@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();
});
}
[...]
[...]
public function up()
{
Schema::table('problems', function(Blueprint $table){
$table->foreign('addedBy')->references('id')->on('users')->onDelete('set null')->onUpdate('CASCADE');
});
}
[...]
我想使用$problem->user->name
获取创建帖子以在表格中显示的用户的名称
请帮助。
全部感谢。
] >>我尝试获取创建帖子巫婆的用户名是(问题模型),我不知道这是什么错误,我已经尝试在laravel文档中进行Eager Loading,并且已经检查了这些Questions 1 Questions 2 ...] >
您的用户
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');
您的用户