dimanche 9 juillet 2017

Laravel 5.4 upload multiple image and store their path in DB

Laravel 5.4:

public function store(Ad $ad)
{

  $this->validate(request(), [
    'title' => 'required',
    'body' => 'required',
    'price' => 'required',
    'city' => 'required'
  ]);

  Ad::create([
    'title' => request('title'),
    'body' => request('body'),
    'price' => request('price'),
    'city' => request('city')
  ]);

  Pic::create([
    'url' => request()->file('pics')->store('pics'),
    'ad_id' => $ad->id
  ]);

  return redirect('/');
}

View:

<form method="POST" action="/" enctype="multipart/form-data">
  
  <label>Title</label>
  <input type="text" name="title" placeholder="Title" required>
  <label>Body</label>
  <textarea name="body" placeholder="Body" required></textarea>
  <label>Price</label>
  <input type="number" name="price" placeholder="Price" required>
  <label>City</label>
  <input type="text" name="city" placeholder="City" required>
  <label>Pictures</label>
  <input type="file" name="pics" accept="image/jpg, image/jpeg, image/png">
  <input type="submit" value="Add">
</form>

So the problem is that I get an error saying "Undefined variable: ad" however a new ad is being created, displayed, stored to DB->ads table and the file is uploaded to host (but it's not stored to DB->pics table).

I want to make a multiple img upload and connect it to a certain ad. As a basis for this i'm using Laracast's "Laravel 5.4 From Scratch: Add Comments" tutorial, however can't figure out the problem since whole day now.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2t21AUz
via IFTTT

Aucun commentaire:

Enregistrer un commentaire