Laravel一对多保存问题

  

违反完整性约束:1048列“ restaurant_id”不能为   null(SQL:插入cover_photospathrestaurant_id,   updated_atcreated_at)个值   (smartmockups_k1gdx0ke_1572876856.jpg,,2019-11-04 14:14:16,   2019-11-04 14:14:16))错误

我正在用名称和位置创建一家新餐厅。现在,它具有一个可供选择的封面图像,但是当我上传它时,会出现此错误。

这种关系是一对多的。

餐厅的照片

public function coverPhoto()
    {
        return $this->hasOne('App\CoverPhoto');
    }

封面模型

public function restaurant()
    {
        return $this->belongsTo('App\Restaurant');
    }

控制器

    <?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Restaurant;
use App\User;
use App\Photo;
use App\CoverPhoto;
class RestaurantController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $restaurants = Restaurant::findOrFail(1)->with('photos','coverPhoto')->get();
        //$restaurantCoverPhoto = Restaurant::findOrFail(1)->coverPhoto->get();

        return $restaurants;
        foreach($restaurants as $restaurant){
            echo $restaurant->coverPhoto->path;
        }



        return view('restaurants.restaurants')->with('restaurants',$restaurants);

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('restaurants.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
         $this->validate($request,[
            'name' => 'required','location' => 'required','cover_photo' => 'image|nullable|max:1999| mimes:jpeg,jpg,png'
        ]);
        // Handle File Upload
        if($request->hasFile('cover_photo')){

            // Get filename with the extension
            $filenameWithExt = $request->file('cover_photo')->getclientOriginalName();
            // Get just filename
            $filename = pathinfo($filenameWithExt,PATHINFO_FILENAME);
            // Get just ext
            $extension = $request->file('cover_photo')->getclientOriginalExtension();
            // Filename to store
            $fileNameToStore= $filename.'_'.time().'.'.$extension;
            // Upload Image
            $path = $request->file('cover_photo')->storeAs('public/cover_photo',$fileNameToStore);
        } else {
            $fileNameToStore = 'noimage.jpg';
        }
        // Create Post
        $restaurant = new Restaurant;
        $coverPhoto = new CoverPhoto;

        $restaurant->name = $request->input('name');
        $restaurant->location = $request->input('location');

        $coverPhoto->path = $fileNameToStore;
        $coverPhoto->restaurant_id = 1;
        //return $coverPhoto;
        $restaurant->coverPhoto()->save($coverPhoto);

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request,$id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

我认为正在发生的事情是,由于这是一家新餐厅和一张新封面照片,因此需要为一个或另一个定义ID。有没有办法创建一个新餐厅和一个单独的数据库Coverphoto?

wzy8352663 回答:Laravel一对多保存问题

首先只需保存您的$restaurant

$restaurant->save();
$restaurant->coverPhoto()->save($coverPhoto);

这样,当您通过关系插入$coverPhoto时,您的$restaurant将已经在数据库中并为其分配了id属性。

,

这很简单,您应该创建餐厅,然后才能创建 餐厅的封面照片

$restaurant = new Restaurant;
$coverPhoto = new CoverPhoto;

$restaurant->name = $request->input('name');
$restaurant->location = $request->input('location');
$restaurant->save();

$coverPhoto->path = $fileNameToStore;

$restaurant->coverPhoto()->save($coverPhoto);

由于每当您在餐厅中创建或插入数据时,它都会作为对象,然后您可以在与餐厅一对一关系的封面照片中创建或插入数据

本文链接:https://www.f2er.com/3166031.html

大家都在问