如何只编辑一篇文章而不是迭代循环所有文章?

逻辑说唱

foreach($user->posts as $post)在我的输入字段上使用,以便我能够使用它,value="{{ old('about') ?? $post->about }}"因为没有迭代我在使用该值时出现错误(试图获取对象的属性)。但是,当我尝试编辑时,我不知道我的 edit.blade 看起来像这样我只需要一个输入字段,我只需要一个特定的帖子,我正在尝试编辑

在此处输入图片说明

编辑刀片.php

@extends('layouts.app')

@section('content')
<div class="container">
<form action="/p/update/{{ $user->id}}" enctype="multipart/form-data" method="POST">

@csrf
@method ('PATCH')

<div class="col-8 offset-2">
<div class="form-group row">

@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

    <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> save </button>
      </div>
    </div> 
</div>
</form>
</div>
@endsection

路线

Route::get('/post/edit/{user}', 'PostController@edit')->name('post.edit');


Route::patch('/p/update/{user}', 'PostController@update')->name('post.update');

后控制器

 public function edit(User $user)
{
   

  return view('posts.edit', compact('user'));
}

 public function update(User $user)
{
$data = request()->validate([
    'about' => 'required',
    'image' => '',

  ]);

  if(request('image')){
    $imagePath = request('image')->store('uploads','public');
    $image = Image::make(public_path("storage/{$imagePath}"))->fit(500,500);
  
    $image->save();

    $imageArray = ['image' => $imagePath];

  }
  auth()->user()->posts()->update(array_merge( $data, $imageArray ?? [] ) );
  
  return redirect("/user/{$user->id} ");
}
易卜拉欣·埃扎特

为了通过一个帖子进行编辑,您必须通过以下帖子:

路线

Route::get('/post/edit/{post}', 'PostController@edit')->name('post.edit');

控制器

 public function edit(Post $post)
{
   
  return view('posts.edit', compact('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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何从一篇文章中获取所有段落而不是一个段落?

只选择作者的最后一篇文章

WordPress的第一篇文章只在首页上有所不同

共同作者加上Wordpress插件-列出同一篇文章的所有作者

Table 使用 yajra/laravel-datatables 为一篇文章带来所有评论

Meteor - 发布所有作者且仅发布最后一篇文章的最佳方法

如何在wp_query循环中获取下一篇文章的类别

为什么get_posts函数显示除一篇文章以外的所有文章?

如何滚动到具有相同类的下一篇文章 ON SCROLL

在 Wordpress 循环中的第一篇文章后添加一个 div

为什么我循环中的第一篇文章意外缩进

Wordpress Post Meta 仅适用于循环中的一篇文章

Rails App中的上一篇文章

用laravel记录一篇文章的浏览次数?

渲染另一篇文章的引用

Wordpress 只显示一篇文章

显示wordpress的第一篇文章

链接到Blogdown中的另一篇文章

在MATLAB中复制一篇文章的图

获取Instagram用户的第一篇文章

Joomla显示内容的另一篇文章

PHP - Wordpress 上一篇文章链接显示在最后一篇文章中

是否有可能获得一篇文章被引用的次数?

为什么只有一篇文章标记它的样式发生了变化?

给定一篇文章的 DOI,如何使用 python 获取域名?

如何在静态页面中显示最后一篇文章的标题?

如何获得上一篇文章的用户名?

跳过上一篇文章时如何忽略jekyll中的偏移

如何显示每个标签的第一篇文章