第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何獲取發(fā)布帖子的用戶名(laravel 5.8)

如何獲取發(fā)布帖子的用戶名(laravel 5.8)

PHP
慕慕森 2022-07-16 18:18:47
我正在嘗試獲取創(chuàng)建帖子女巫的用戶名是(問(wèn)題模型),我不知道出了什么問(wèn)題我在 laravel 文檔中嘗試了Eager Loading ,我已經(jīng)檢查了這些問(wèn)題 1 問(wèn)題 2并且仍然為 null 或者Trying to get property 'name' of non-object如果我用dd( $problem->user->name);我使用 laravel 5.8問(wèn)題Controller.phpnamespace 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(模型)<?phpnamespace App;use Illuminate\Database\Eloquent\Model;class Problems extends Model{        public function user()    {        return $this->belongsTo('App\User');    }}Usre.php(模型)<?phpnamespace 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>我想用來(lái)$problem->user->name獲取創(chuàng)建帖子的用戶的名稱(chēng)以將其顯示在表格中
查看完整描述

1 回答

?
神不在的星期二

TA貢獻(xiàn)1963條經(jīng)驗(yàn) 獲得超6個(gè)贊

您的用戶模型正在問(wèn)題表中尋找一個(gè)user_id字段以使關(guān)系正常工作。Laravel 使用模型名稱(chēng) + '_id' 自動(dòng)為模型關(guān)系創(chuàng)建使用蛇形案例的關(guān)系。


如果您將問(wèn)題表上的外鍵更改為,user_id而不是addedBy,這將按原樣工作。


或者,如果您希望保留addedBy密鑰,則需要告訴 Laravel 您使用的是非標(biāo)準(zhǔn)密鑰。因此,在您的用戶模型上:


public function problem()

{

    return $this->hasMany('App\Problems', 'addedBy');

}

應(yīng)該做的伎倆。請(qǐng)參閱此處的文檔。


不幸的是,您可能還沒(méi)有在桌子上定義實(shí)際的 FK。在foreign指定之前,我希望看到如下內(nèi)容:


$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');

遷移文檔在此處適用于V6 。


查看完整回答
反對(duì) 回復(fù) 2022-07-16
  • 1 回答
  • 0 關(guān)注
  • 103 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)