繁体   English   中英

如何将图像上传到具有多对多关系的表单?

[英]How to upload an image to a form with many to many relationships?

这与这个问题非常相似。 我正在使用Laravel 5并尝试使用表单将文件(图像)添加到我的数据库中。 我有一个表单,可以在我的文章类中添加各种数据(标题,描述,图像)。 一篇文章还有'belongsToMany'类别和天数(很多到很多r / ship)。 下面的代码允许我上传我的数据, 它添加了文章的三个实例 前两个实例具有正确的照片路径/名称(photo.jpg)。 第三个实例将这样的名称添加到db:/ tmp / phphJIIY1。 它正确地为数据透视表添加了ID。

我认为这就是'商店'功能的这一行

        $article = Article::create($request->all());    

这导致了问题,但我需要那条线,否则我会收到上一个问题中描述的错误。

如何订购/更改此代码,以便我可以上传图片在文章中添加类别/天数? 我已经安装了干预\\图像但尚未使用它。

   public function create()
{

    $categories = Category::lists('name', 'id');
    $days = Day::lists('dayname', 'id');
    return view('articles.create', compact('categories', 'days'));
}

public function store(ArticleRequest $request)
{

   $image_name = $request->file('image')->getClientOriginalName();
   $request->file('image')->move(base_path().'/public/images', $image_name);
   $article = ($request->except(['image']));
   $article['image'] = $image_name;
   Article::create($article);

//上面这条线路本身很好用(如果我在这里评论它可以正常工作,但我需要我的很多很多人去工作)

    $article = Article::create($request->all());

//必须添加以上行才能使'categories()'工作。

    $categoriesId = $request->input('categoryList');
    $article->categories()->attach($categoriesId);
    $daysId = $request->input('dayList');
    $article->days()->attach($daysId);
    return redirect()->route('articles_path');

}

对不起我误会了。 我是新人,并试图解决问题。 我有同样的问题,正在保存2个候选记录,我这样做是为了让它工作:

    $file = Request::file('resume');
    $extension = $file->getClientOriginalExtension();
    Storage::disk('local')->put($file->getFilename().'.'.$extension,  File::get($file));
    $resume = new Resume();
    $resume->mime = $file->getClientMimeType();
    $resume->filename = $file->getFilename().'.'.$extension;
    //save resume & put candidate's id as foreign key
    $candidate=new Candidate();
    $data=array_except($data, array('_token','resume'));
    //attach blank candidate to current user
    $user->candidate()->save($candidate);
    $candidate->resume()->save($resume);

    //find the right instance of candidate we want to update*
    $candidate=$user->candidate($user);
    //Now update the candidate with data once it's been attached.
    $candidate->update($data);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM