E
E
Eugene2017-07-02 17:29:41
Laravel
Eugene, 2017-07-02 17:29:41

How to apply the name of the uploaded file to the column of the database table via Request?

Hello!
For my blog, I want to upload an image for each post. I format the image properly, change its name, and save it to a folder. Next, I want to apply the new image name to the "article_wall" database field, but it keeps the path to the tmp. Thank you in advance for your response.
The last image shows var_dump($request['article_wall']);
Controller

public function store(ArticleRequest $request)
    {
        if($request->hasFile('article_wall')) {
            $image = $request->file('article_wall');
            $imageName = time() . "." . $image->getClientOriginalExtension();
            $savePath = public_path('/uploads/articleImages/' . $imageName);
            Image::make($image)
                ->resize(320,240)
                ->save($savePath);
            $request['article_wall'] = $imageName;
        }
        echo '<pre>';
            var_dump($request['article_wall']);
        echo '</pre>';
        var_dump($imageName);
        //сохранение статьи
        Auth::user()->articles()
            ->create($request->all());
        //$article = Article::create();
       // $article->save();
        session()->flash('flash_message', 'Статья успешно создана');
        return redirect('news');
    }

Articles Model
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;

class Article extends Model
{
    /**
     * Разрешённые поля для статей
     *
     * @var array
     */
    protected $fillable = [
        'title',
        'body',
        'published_at',
        'article_wall'
    ];
    /**
     * даты, которые рассматриваются как экземпляры Carbon
     *
     * @var array
     */
    protected $dates = ['published_at'];

    /**
     * Отображает настоящие статьи
     *
     * @param $query
     */
    public function scopePublished($query) {
        $query->where('published_at', '<=', Carbon::now());
    }
    /**
     * Статьи времени больше настоящего
     *
     * @param $query
     */
    public function scopeUnpublished($query) {
        $query->where('published_at', '>', Carbon::now());
    }

    /**
     * Даёт аттрибут времени для статьи
     *
     * @param $date
     */
    public function setPublishedAtAttribute($date) {
        $this->attributes['published_at'] = Carbon::parse($date);
    }

    /**
     * Статья пренадлежит пользователю
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function user() {
        return $this->belongsTo('App\User');
    }



}

The form
<div class="form-group">
    {!! Form::label('title', 'Заголовок :') !!}
    {!! Form::text('title', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
    {!! Form::label('body', 'Контент') !!}
    {!! Form::textarea('body', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
    {!! Form::label('published_at',"Дата публикации :") !!}
    {!! Form::input('date', 'published_at', date('Y-m-d'), ['class' => 'form-control']) !!}
</div>

<div class="form-group">
    {!! Form::label('article_wall', 'Изображение') !!}
    {!! Form::file('article_wall') !!}
</div>
<div class="form-group">
    {!! Form::submit($submitButtonText, ['class' => 'btn btn-primary form-control']) !!}
</div>

How
21c0228654424bbb9e5a0be23c25ce91.png
is var_dump($request['article_wall']);
df22410a6f674f22831123199edc6ec8.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Ernest Fayzullin, 2017-07-03
@Shelderr

Auth::user()->articles()
            ->create($request->all()); // в массиве $request->all() надо заменить значение ключа pathName на $imageName

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question