Answer the question
In order to leave comments, you need to log in
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');
}
<?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');
}
}
<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>
Answer the question
In order to leave comments, you need to log in
Auth::user()->articles()
->create($request->all()); // в массиве $request->all() надо заменить значение ключа pathName на $imageName
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question