违反完整性约束:1048列“ restaurant_id”不能为 null(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。有没有办法创建一个新餐厅和一个单独的数据库Coverphoto?