亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從 Laravel 中的對象獲取用戶名

如何從 Laravel 中的對象獲取用戶名

PHP
慕容森 2021-06-30 17:58:54
我是 Laravel 的初學者,我有以下遷移。Schema::create('users', function (Blueprint $table) {    $table->bigIncrements('id');    $table->bigInteger('company_id')->unsigned();    $table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');    $table->boolean('enable')->default(0);    $table->string('name', 120)->nullable();    $table->string('surname', 120)->nullable();    $table->string('email', 120)->unique();    $table->timestamp('email_verified_at')->nullable();    $table->string('password');    $table->boolean('enable_map')->default(0);    $table->rememberToken();    $table->timestamps();    $table->engine = "InnoDB";});Schema::create('comments', function (Blueprint $table) {    $table->bigIncrements('id');    $table->bigInteger('user_id')->unsigned();    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');    $table->Integer('rating');    $table->text('content');    $table->dateTime('date_time');    $table->string('ip', 25);    $table->engine = "InnoDB";});用戶名class User extends Authenticatable implements MustVerifyEmail{    use Notifiable;    use psCMS\Presenters\UserPresenter;    public static $roles = [];    protected $fillable = ['company_id', 'enable', 'name', 'surname', 'email', 'email_verified_at', 'password', 'counter', 'url_address',  'isCompany', 'isMailing', 'content', 'nip1', 'business1', 'phone1', 'street1', 'number1', 'postal_code1', 'city1', 'country_id1', 'provincial_id1', 'nip2', 'business2', 'phone2', 'street2', 'number2', 'postal_code2', 'city2', 'country_id2', 'provincial_id2', 'nip3', 'business3', 'phone3', 'street3', 'number3', 'postal_code3', 'city3', 'country_id3', 'provincial_id3', 'cash', 'lng', 'lat', 'enable_map', 'remember_token', 'created_at', 'updated_at', 'last_login_at', 'last_login_ip' ];    protected $hidden = [        'password', 'remember_token',    ]; 
查看完整描述

3 回答

?
暮色呼如

TA貢獻1853條經驗 獲得超9個贊

Comments::with('user')->where('content','LIKE','%'.$query.'%')->orWhere('id','LIKE','%'.$query .'%')->orderBy($sortColumn, $sortMethod)->paginate(25);


查看完整回答
反對 回復 2021-07-02
?
吃雞游戲

TA貢獻1829條經驗 獲得超7個贊

我沒有關于用戶的信息。我只有來自評論的價值。為什么?


因為您只在模型 Comments 上進行查詢


我如何獲取有關添加此評論的用戶的信息?


將用戶添加到查詢中:


$comments_with_user_info = Comments::with('user')->where('content','LIKE','%'.$query.'%')->orWhere('id','LIKE','%'.$query.'%')->orderBy($sortColumn, $sortMethod)->paginate(25);

我想顯示評論列表以及有關用戶的信息。


您可以通過這種方式獲取名稱(例如在刀片中):


@foreach($comments_with_user_info as $comment)

    {{ $comment->user->name }}

@endforeach


查看完整回答
反對 回復 2021-07-02
  • 3 回答
  • 0 關注
  • 261 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號