Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upload an image to a form with many to many relationships?

this is very similar to this question. I am using Laravel 5 and trying to add files(an image) to my database with a form. I have a form to add various data (title, description, image) to my Article class. An article also 'belongsToMany' categories and days (many to many r/ship). The code below allows me to upload my data, but it adds three instances of the article! The first two instances have the correct photo path/name (photo.jpg). And the third instance adds a name like this to the db: /tmp/phphJIIY1. It adds ids to the pivot tables correctly.

I think it's this line of the 'store' function

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

that is causing the problems, but I need that line or I get the error described in my last question.

How can I order/change this code so I can upload an image and add categories/days to my article? I have installed intervention\image but am not using it yet.

   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);

//ABOVE THIS LINE WORKS FINE BY ITSELF (if I comment out below here it works fine but i need my many to many r/ship to work)

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

//HAVE TO ADD THIS LINE ABOVE TO MAKE 'categories()' WORK.

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

}
like image 424
thomas jaunism Avatar asked Oct 30 '22 16:10

thomas jaunism


1 Answers

Sorry I misunderstood. I am new & trying to figure things out too. I had same problem, was saving 2 candidate records, and I did this to make it work:

    $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);
like image 189
Mayur Avatar answered Nov 09 '22 11:11

Mayur