Изображение сохраняется как файл .tmp в базе данных laravel - PullRequest
0 голосов
/ 16 октября 2019

Я пытаюсь сохранить изображение с помощью Laravel

Изображение сохраняется как файл tmp в базе данных, почему так?

изображение сохраняется как C: \ xampp \ tmp \ php50B5.tmpв базе данных

как это исправить?

контроллер:

  public function store(Request $request)
    {
        $new_file_data=[
            'small_explain'=>$request->input('small_explain'),
            'title'=>$request->input('title'),
            'body'=>$request->input('body'),
            'important_body'=>$request->input('important_body'),
            'quote'=>$request->input('quote'),
            'author_quote'=>$request->input('author_quote'),
            'index_image' => $request->file('index_image'),
            'header_image' => $request->file('header_image'),
            'text_image' =>$request->file('text_image'),
        ];
        $request->file('index_image' )->store('Images');
        $request->file('header_image' )->store('Images');
        $request->file('text_image' )->store('Images');
        Article::created($new_file_data);
    }

модель:

class Article extends Model
{
    protected $primaryKey = 'id';
    protected $guarded = ['id'];
}

index.blade:

<tbody>
        @foreach( $article as $articles )
            <tr>
                <td style="width: 10%">{{ $articles->id }}</td>
                <td style="width: 20%">{{ $articles->title }}</td>
                <td style="width: 20%">{{ $articles->small_explain }}</td>
                <td style="width: 10%">>{{ $articles->index_image }}t</td>
                <td style="width: 10%">>{{ $articles->header_image }}</td>
                <td style="width: 10%">{{ $articles->text_image }}</td>
                <td>
                    <button {{--href="{{ route('$articles.edit' , ['id'=>$articles->id])}}"--}} class="btn btn-primary btn-xs"><i class="icon-pencil"></i></button>

                </td>
                <td>
                    <form{{-- action="{{route('$articles.destroy' ,  ['id'=>$articles->id])}}"--}} method="post">
                        {{ method_field('delete') }}
                        {{csrf_field()}}
                    <button type="submit" class="btn btn-danger btn-xs"><i class="icon-trash "></i></button>
                    </form>
                </td>
            </tr>
        @endforeach
        </tbody>

Ответы [ 2 ]

0 голосов
/ 16 октября 2019

$request->file('index_image' ) Путь к месту хранения изображения при загрузке, который является временным каталогом. Когда вы вызываете ->store(), этот метод возвращает новый путь к месту хранения файла, но вы не передаете его в свой метод create.

Этот код будет делать то, что вам нужно, все, что я сделал, это переместил методы хранилища до назначения new_file_data

public function store(Request $request)
    {
        $new_file_data=[
            'small_explain'=>$request->input('small_explain'),
            'title'=>$request->input('title'),
            'body'=>$request->input('body'),
            'important_body'=>$request->input('important_body'),
            'quote'=>$request->input('quote'),
            'author_quote'=>$request->input('author_quote'),
            'index_image' => $request->file('index_image')->store('Images');,
            'header_image' => $request->file('header_image')->store('Images');,
            'text_image' =>$request->file('text_image')->store('Images');,
        ];
        Article::created($new_file_data);
    }
0 голосов
/ 16 октября 2019

Попробуйте изменить это:

$request->file('index_image' )->store('Images');

На это:

$file = $request->file('index_image');
$file->storeAs('Images', "my-cool-name." . $file->getClientOriginalExtension());
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...