im über Daten mit haMany Relatiions Speicherung wo Autos mehr als Bild haben können, und jedes Bild nur ein AutoSpeichern und Aktualisieren hat n-Beziehungen Laravel
Auto Modem Car.php
class Car extends Model
haben
{
protected $table = 'cars';
public $timestamps = true;
use SoftDeletes;
protected $dates = ['deleted_at'];
protected $fillable = array('Marque', 'Model', 'sieges', 'climatisation', 'portes', 'transmition', 'price','url','car_id');
public function cars_images()
{
return $this->hasMany(CarsImage::class);
}
CarsImage Modell
class CarsImage extends Model
{
protected $table = 'cars_images';
public $timestamps = true;
use SoftDeletes;
protected $dates = ['deleted_at'];
protected $fillable = array('url');
public function cars()
{
return $this->belongsTo(Car::class);
}
}
MyController:
public function store(Request $request) {
$car = new Car;
$car->Marque = Input::get('Marque');
$car->Model = Input::get('Model');
$car->sieges = Input::get('sieges');
$car->climatisation = Input::get('climatisation');
$car->portes = Input::get('portes');
$car->transmition = Input::get('transmition');
$car->price = Input::get('price');
$img = new CarsImage;
$img->url = 'jean Luc Picard';
DB::transaction(function() use ($car, $img) {
$car = $car->save();
Car::find($car->id)->cars_images()->save($img);
});
return 'ok';
}
das Problem ist, das Auto wird gespeichert und die URL auch gespeichert, aber nicht mit der car_id
Beliebig Hilfe bitte ?
Wahrscheinlich ist das Problem mit der DB-Struktur verbunden. Können Sie Ihre Migrationen zeigen? Oder versuchen Sie, in der Beziehungserklärung einen gültigen local_key anzugeben - return $ this-> hasOne ('App \ Phone', 'Foreign_key', 'local_key'); – Laraleg
'Klasse CreateForeignKeys erstreckt Migration { \t public function nach oben() \t { \t \t Schema :: Tabelle ('cars_images', function ($ Blueprint-Tabelle) { \t \t \t $ table-> ausländische ('car_id') -> Referenzen ('id') -> auf ('Autos') \t \t \t \t \t \t -> onDelete ('Kaskade') \t \t \t \t \t \t -> onUpd gegessen ('Kaskade'); \t \t}); ' –
wenn ich versuche ' Auto :: find (1) -> cars_images() -> speichern ($ img); ' alle richtig –