首页 > 解决方案 > Laravel 一对多保存问题

问题描述

完整性约束违规:1048 列 'restaurant_id' 不能为空(SQL:插入cover_photos( path, restaurant_id, updated_at, created_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。有没有办法创建一个新的餐厅和一个单独的数据库的封面照片?

标签: phplaraveleloquent

解决方案


只需保存您的$restaurant第一个:

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

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


推荐阅读