从空值laravel创建默认对象

时间:2017-06-26 08:56:04

标签: php laravel laravel-5.4

我一直在收到错误Creating default object from empty value laravel我成功地使用以下代码插入新行但是今天当我尝试测试代码时,它返回指向行$reviw->rating = $request->productrating;的错误

我的db表的结构是:

id|fname|lname|email|country|title|content|rating|thumbnail|tour_id|status

    public function store(Request $request)
{
    // dd($request->all());
    $this->validate($request, [
        'fname'     => 'required',
        'lname'     =>  'required',
        'email'     => 'required',
        'country'   => 'required',
        'title'   => 'required|min:10',
        'productrating' => 'required',
        'content'   => 'required|min:10'
        ]);        
    // dd($request->productrating);
    $review = new Review;
    $review->fname = $request->fname;
    $review->lname = $request->lname;
    $review->email = $request->email;
    $review->country = $request->country;
    $review->title = $request->title;
    $review->content = $request->content;        
    $reviw->rating = $request->productrating;
    if($request->hasFile('fileupload1')){
        $image = $request->file('fileupload1');
        $filename = 'thumb'.time().'.'.$image->getClientOriginalExtension();
        $location = 'images/client_review/'.$filename;
        Image::make($image)->resize(200, 200)->save($location);
        $review->thumbnail = $location;
    }
    $review->tour_id = $request->product_id;
    $review->status = false;
    $review->save();
    Session::flash('success','Thank You for submitting us your review.');
    return view('public.pages.message-review');        
}

我从表单中发送以下数据以保存到表格中。enter image description here

2 个答案:

答案 0 :(得分:0)

$reviw->rating = $request->productrating;$reviw更改为$review时出现错误,并且可以正常运行

答案 1 :(得分:0)

我认为您需要更新代码,如:

    $review = new Review;
    $review->fname = $request->fname;
    $review->lname = $request->lname;
    $review->email = $request->email;
    $review->country = $request->country;
    $review->title = $request->title;
    $review->content = $request->content;        
    $review->rating = $request->productrating;