使用Laravel更新照片

68de4m5k  于 2022-11-26  发布在  其他
关注(0)|答案(1)|浏览(109)

我用迁移创建了一个叫做“directors”的表。然后我创建了模型,视图,控制器。在“DirectorController”中,我为我的表写了一个粗糙的操作逻辑。它工作,直到我试图编辑一个现有的记录。要清楚的是,它更新了所有的信息,除了图像,它保持不变。有人能告诉我什么是错的吗?

  • 文件夹“images”已公开。

enter image description here
//迁移

public function up()
{
    Schema::create('directors', function (Blueprint $table) {
        $table->id();
        $table->string('first_name');
        $table->string('last_name');
        $table->datetime('birth');
        $table->string('town');
        $table->string('country');
        $table->string('image');
        $table->timestamps();
    });
}

//控制器

public function update(Request $request, Director $director)
{

    $request->validate([
        'first_name' => 'required',
        'last_name' => 'required',
        'birth' => 'required',
        'town' => 'required',
        'country' => 'required'
    ]);

    $input = $request->all();

    if ($image = $request->file('image')) {
        $destinationPath = 'images/';
        $profileImage = date('YmdHis') . "." . $image->getClientOriginalExtension();
        $image->move($destinationPath, $profileImage);
        $input['image'] = "$profileImage";
    }else{
        unset($input['image']);
    }

    $director->update($input);

    return redirect()->route('directors.index')->with('success', 'Director updated successfully.');

}

//目录模型

protected $fillable = [
    'first_name',
    'last_name',
    'birth',
    'town',
    'country',
    'image'];

public function movies() {
    return $this->hasMany(Movie::class);
}

//编辑. blade.php

@extends('directors.layout')

@section('content')

<div class="row">
    <div class="col-lg-12 d-flex justify-content-between mt-5 mb-4">
        <div class="pull-left">
            <h2>Edit Director</h2>
        </div>
        <div class="pull-right">
            <a class="btn btn-primary" href="{{ route('directors.index') }}"><i class="fa-solid fa-delete-left"></i> Back</a>
        </div>
    </div>
</div>

@if ($errors->any())
    <div class="alert alert-danger">
        <div class="first d-flex justify-content-between">
            <strong>Whoops!</strong>
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
        There were some problems with your input. <br><br>
        <ul>
            @foreach ( $errors->all() as $error )
                <li>{{ $error }}</li>
            @endforeach
        </ul>
        
    </div>
@endif

<form action="{{ route('directors.update', $director->id) }}" method="POST">
    @csrf
    @method('PUT')

    <div class="row">
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>First Name:</strong>
                <input type="text" name="first_name" value="{{ $director->first_name }}" class="form-control" placeholder="First Name">  
            </div>
        </div>

        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Last Name:</strong>
                <input type="text" name="last_name" value="{{ $director->last_name }}" class="form-control" placeholder="Last Name">  
            </div>
        </div>

        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Birth:</strong>
                <input type="text" name="birth" value="{{ $director->birth }}" class="form-control" placeholder="Birth">  
            </div>
        </div>

        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Town:</strong>
                <input type="text" name="town" value="{{ $director->town }}" class="form-control" placeholder="Town">  
            </div>
        </div>

        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Country:</strong>
                <input type="text" name="country" value="{{ $director->country }}" class="form-control" placeholder="Country">  
            </div>
        </div>

        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Image:</strong>
                <input type="file" name="image" class="form-control" placeholder="image">
                <img src="/images/{{ $director->image }}" width="300px">
            </div>
        </div>

        <div class="col-xs-12 col-sm-12 col-md12 text-center">
            <button type="submit" class="btn btn-primary"><i class="fa-solid fa-square-plus"></i> Submit</button>
        </div>
    </div>
</form>

@endsection
y1aodyip

y1aodyip1#

您需要将表单更改为:

<form action="{{ route('directors.update', $director->id) }}" method="POST" enctype="multipart/form-data">

相关问题