showing user existing data in the database for different input fields in blade laravel with old value

Viewed 296

when i try to show existing data in my input field before editing am getting this error "Trying to get property 'posts' of non-object (View: E:\Projects\htdocs\zipad\resources\views\posts\edit.blade.php)" However when i try this code below it works perfectly

working code in blade

    @foreach($user->posts as $post)
<div class="col-4"> {{$post->about }}
</div>
@endforeach

edit.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>

postcontroller

public function edit(User $user)
{
    return view('posts.edit', compact('user'));
}
1 Answers

When you are iterating $user->posts it works because you are accessing each post and rendering it.

But in your posts.edit are using $user->posts->about, it doesn't know which post's about.

Solution: You have to iterate all posts of that user like the working code you have given.

.....
@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
....
Related