ErrorException: 试图获取非对象的属性:(使用 laravel)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38593196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:12:54  来源:igfitidea点击:

ErrorException: Trying to get property of non-object: (using laravel)

phpdatabaselaravelexceptionlaravel-5.2

提问by ramsai

ErrorException: Trying to get property of non-object: PHP beginner

ErrorException: 试图获取非对象的属性:PHP 初学者

Trying to retreive data from database.

试图从数据库中检索数据。

show.blade.php

show.blade.php

@extends('main')

@section('title','| Station details')

@section('content')

    <h1>{{ $station->station_name }}</h1>

    <p class="lead">{{ $station->station_name }}</p>

@endsection

controller -

控制器 -

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Station;
use Session;

class StationController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // validate the data
        $this->validate($request, array(
                'station_name' => 'required|max:255',
                'date'=>'required'
            ));

        // store in the database
        $station = new Station;

        $station->station_name = $request->station_name;
        $station->date = $request->date;
        $station->PET = $request->PET;
        $station->max_temp = $request->max_temp;
        $station->min_temp = $request->min_temp;
        $station->min_rh = $request->min_rh;
        $station->solar_rad = $request->solar_rad;
        $station->rainfall = $request->rainfall;
        $station->wind4am = $request->wind4am;
        $station->wind4pm = $request->wind4pm;

        $station->save();

        Session::flash('success','Station data added');

        return redirect()->route('stations.show', $station->id);


        // redirect to another page
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $station = Station::find($id);

        return view('stations.show')->withStation('$station');
    }

    /**
     * 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)
    {
        //
    }
}

Database:- database

数据库:- 数据库

回答by Ryan

When you are returning the view in the following line:

当您在以下行中返回视图时:

return view('stations.show')->withStation('$station');

You need to either use double quotes or not use quotes at all. With the single quotes you are actually setting the value to the literal string "$station"instead of the object you created.

您需要使用双引号或根本不使用引号。使用单引号,您实际上是将值设置为文字字符串,"$station"而不是您创建的对象。

回答by Jose Garrido

You can try editing line 84 of your controller like this:

您可以尝试像这样编辑控制器的第 84 行:

return $view->with(compact('station'));

Using compact is faster and cleaner in most cases since you already built the variable.

由于您已经构建了变量,因此在大多数情况下使用 compact 更快更干净。

回答by Kamil Kie?czewski

Change method public function create()to:

将方法更改public function create()为:

public function create()
{
    $station = new Station;
    return view('stations.create')->with(compact('station'));
}

回答by Sid

please re-write return view('stations.show')->withStation('$station');like return view('stations.show')->with(['station' => $station]);

请重新写return view('stations.show')->withStation('$station');return view('stations.show')->with(['station' => $station]);