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

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

以舊值顯示 Blade Laravel 中不同輸入字段的數據庫中的用戶現有數據

以舊值顯示 Blade Laravel 中不同輸入字段的數據庫中的用戶現有數據

PHP
HUX布斯 2023-07-21 16:11:54
當我在編輯之前嘗試在輸入字段中顯示現有數據時,收到此錯誤“嘗試獲取非對象的屬性“帖子”(視圖:E:\Projects\htdocs\zipad\resources\views\posts\edit.blade.php)”但是,當我嘗試下面的代碼時,它工作得很好Blade 中的工作代碼    @foreach($user->posts as $post)<div class="col-4"> {{$post->about }}</div>@endforeach編輯.blade.php    <form action="/p" enctype="multipart/form-data" method="post">@csrf<div class="col-8 offset-2"><div class="form-group row">                            <label for="about" class="col-md-4 col-form-label text-md-right">{{ __(' post about') }}</label>                            <div class="col-md-6">                                <input id="about" type="text" class="form-control @error('about') is-invalid @enderror" name="about"  value="{{ old('about') ?? $user->posts->about }}" required autocomplete="about" autofocus>                                @error('about')                                    <span class="invalid-feedback" role="alert">                                        <strong>{{ $message }}</strong>                                    </span>                                @enderror                            </div>                            <label for="image" class="col-md-4 col-form-label text-md-right">{{ __(' post image') }}</label>                            <input type="file", class="form-control-file" id ="image" name="image">                            @error('image')                                    <div class="invalid-feedback" role="alert">                                        <strong>{{ $message }}</strong> </div>                                                                    @enderror                                <div class="btn btn-primary">                                <button> add img post</button>                                </div>                        </div></div>    </form>后置控制器public function edit(User $user){    return view('posts.edit', compact('user'));}
查看完整描述

1 回答

?
蝴蝶不菲

TA貢獻1810條經驗 獲得超4個贊

當您迭代時,$user->posts它會起作用,因為您正在訪問每個帖子并渲染它。


但在您posts.edit使用時$user->posts->about,它不知道哪篇文章是關于哪篇文章的。


解決方案: 您必須像您提供的工作代碼一樣迭代該用戶的所有帖子。


.....

@foreach($user->posts as $post)

<label for="about" class="col-md-4 col-form-label text-md-right">{{ __(' post about') }}</label>

<div class="col-md-6">

    <input id="about" type="text" class="form-control @error('about') is-invalid 

    @enderror" name="about"  value="{{ old('about') ?? $post->about }}" 

    required autocomplete="about" autofocus>

    @error('about')

    <span class="invalid-feedback" role="alert">

    <strong>{{ $message }}</strong>

    </span>

    @enderror

</div>

@endforeach

....


查看完整回答
反對 回復 2023-07-21
  • 1 回答
  • 0 關注
  • 117 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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