Comparte:
vídeo 28 del curso desarrollo de un blog con manejo de roles y permisos
Al guardar la imagen mediante dropzone en la tabla Photos no guarda el id del proudcto, he añadido el campo nullable en la migración de la tabla Photos pero al hacer eso el campo Post_id aparece Null.
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePhotosTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('photos', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('product_id');
$table->string('url');
$table->timestamps();
});
}
PhotosController
<?php
namespace App\Http\Controllers\Admin;
use App\Product;
use App\Photo;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Storage;
class PhotosController extends Controller
{
public function store(Product $product)
{
$this->validate(request(), [
'photo' => 'required|image|max:2048'
]);
$photo = request()->file('photo')->store('public');
Photo::create([
'url' => Storage::url($photo),
'product_id' => $product->id,
]);
}
}
Model Product
public function photos()
{
return $this->hasMany(Photo::class);
}
el error que aparece al guardar es:
'product_id' => $product->id,
ya que cuando la guarda en la tabla fotos... es Null.
<form method="POST" action="{{ route('admin.products.update', $product) }}">
@method('PUT')
@csrf
<div class="box-body">
<div class="form-group {{$errors->has('title') ? 'has-error' : ''}}">
<label>Title</label>
<input name="title" value="{{old('title', $product->title)}}" class="form-control" placeholder="Ecribir nombre del producto...">
{!! $errors->first('title', '<span class="alert-danger">:message</span>')!!}
</div>
<div class="form-group {{$errors->has('description') ? 'has-error' : ''}}">
<label>Extracto</label>
<textarea name="description" class="form-control" placeholder="Introduce un extracto">{{old('description', $product->description)}}</textarea>
{!! $errors->first('description', '<span class="alert-danger">:message</span>')!!}
namespace App\Http\Controllers\Admin;
use App\Photo;
use App\Product;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use App\Http\Controllers\Controller;
class PhotosController extends Controller
{
public function store(Product $product)
{
dd($product->id);
$this->validate(request(), [
'photo' => 'required|image|max:2048'
]);
$photo = request()->file('photo')->store('public');
foreach($request->product as $product) {
$this->productStockRepository->create($product);
}
Photo::create([
'url' => Storage::url($photo),
'product_id' => $product->id,
]);
}
}