Anyone?
Jul 14, 2016
10
Level 5
Uploader Help / Dropzone.js
OVERVIEW
Note: **If anybody could help me I would be greatful. **
- Im using dropzone.js for image upload and I have heard people saying they have added an id on the create function by creating an "empty" model instance with (id, instance_id*, created_on, updated_on) and then redirect to the edit page one they have the id to attach to the images or files.
**If anyone could show me an example it would be great. **
Is this done in the Route::create ?
Or is this done in the create / store functions in the controller?
I am at a loss on how to attach the id of the model instance when saving to the database. Been working on this for a few days now so would really appreciate the assistance.
If anyone has an example of how this could be done differently I am up to learn that as well. I have gone through the flyers tutorial many times now and still cannot get it to work. Maybe store them temporarily in the sessions then when created move and save them to the database. If this can be done please share an example.
Project Overview:
MODELS:
Product Model:
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $guarded = ['id'];
public $timestamps = true;
protected $fillable = ['id','product_name', 'price', 'sku', 'upc', 'description'];
protected $visible = ['product_name', 'price', 'sku', 'upc', 'description'];
public function images() {return $this->hasMany(\App\Models\ProductImage::class); }
public function getPriceAttribute($price)
{
return '$'. number_format($price, 2, '.', '');
}
public function scopePreUploadImagesBeforeProductCreation($query, $id )
{
return $query->where(compact('id'))->first();
}
}
Photos Model:
namespace App\Models;
use Image;
use Illuminate\Database\Eloquent\Model;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class Photo extends Model{
public $table = 'photos';
protected $fillable = [ 'name', 'path'];
public function product() { return $this->belongsTo(\App\Models\Product::class);
}
Product Controller
use App\Http\Requests;
use App\Http\Requests\ProductCreateRequest;
use App\Models\Photo;
use App\Product;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Response;
class ProductController extends Controller
{
public function create()
{
return view('product.create');
}
public function store(Request $request)
{
Product::create($request->all());
flash()->success('Success!', 'Your product has been created');
return Redirect::route('product.index');
}
public function show($id)
{
$product = Product::findOrFail($id);
return view('product.show', compact('product'));
}
public function addPhoto(Product $product, Request $request)
{
$this->validate($request, [
'photo' => 'required|mimes:jpg,jpeg,png,bmp'
]);
$file = $request->file('photo');
$name = $file->getClientOriginalName();
$file->move('uploads/products/{name}');
$product = Product::where($product)->firstOrFail();
$product->photos()->create(['path' => "/uploads/products/{name}"]);
return "done";
}
}
Level 122
Well, you will need to grab the id of the created product
$product = Product::create($request->all());
flash()->success('Success!', 'Your product has been created');
return Redirect::route('product.edit', [$product->id]);
1 like
Please or to participate in this conversation.