我正在尝试在缩略图中添加描述,但不会在数据库中更新。
你们知道为什么吗?
唯一的thumb_description
不会更新。
已填写填充项。当我完成$content->save()
时,它将返回true。
控制器:
public function detail(Request $request, $id)
{
$content = Content_blocks::find($id);
if ($request->isMethod('post')) {
if ($request->hasFile('image')) {
$folder = 'uploads';
$name = $request->image->getClientOriginalName();
$store = $request->image->store($folder, 'public');
$file = pathinfo(Storage::url($store));
$thumb_description = $request->input('thumb_description');
$thumbnail = new Thumbnail();
$thumbnail->name = $file['filename'];
$thumbnail->extenstion = $file['extension'];
$thumbnail->path = $folder;
$thumbnail->thumb_description = $thumb_description;
$thumbnail->save();
$content->thumbnail_id = $thumbnail->id;
}
$content->title = $request->input('title');
$content->description = $request->input('description');
$content->link = $request->input('link');
$content->linkname = $request->input('linkname');
$content->order = $request->input('order');
$content->contentcategory_id = $request->input('categorie');
$content->thumbnail->thumb_description = $request->input('thumb_description');
if ($request->input('ontkoppel') === 'deletion') {
Thumbnail::find($content->thumbnail_id)->delete();
}
// dd($content->thumbnail->thumb_description);
$content->save();
return redirect('admin/content/webcategorie/homepage/contentblocks/' . $content->content_id);
}
模型
<?php
namespace App\Models;
use Illuminate\Support\Facades\Storage;
class Thumbnail extends Model
{
protected $fillable = [
"thumb_description",
"name",
"path",
"extenstion"
];
protected $table = 'thumbnail';
public function content()
{
return $this->belongsTo(Content::class);
}
public static function getFile($id)
{
$thumbnail = self::find($id);
if ($thumbnail === null) {
return null;
}
$url = sprintf('%s/%s.%s', $thumbnail->path, $thumbnail->name, $thumbnail->extenstion);
return Storage::disk('public')->url($url);
}
public static function getDescription($id)
{
$thumbnail = self::find($id);
if ($thumbnail === null) {
return null;
}
$description = $thumbnail->thumb_description;
return $description;
}
}
迁移
*/
public function up()
{
Schema::create('thumbnail', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 255);
$table->string('path', 150);
$table->string('extenstion', 10);
$table->text('thumb_description');
$table->timestamps();
});
}
在chrome开发工具中发布
------WebKitFormBoundaryfun4SMk5TA604OZE
Content-Disposition:表单数据; name =“ thumb_description”
bruyanga
答案 0 :(得分:2)
$content->save();
仅将属性保存在$content
中,而不保存其关系。
请尝试使用$content->push();
,它应该级联已加载的关系并保存所有相关模型。
https://laravel.com/api/5.6/Illuminate/Database/Eloquent/Model.html#method_push