簡體   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