【问题标题】:ErrorException: Trying to get property of non-object: (using laravel)ErrorException:试图获取非对象的属性:(使用 laravel)
【发布时间】:2016-11-30 07:08:27
【问题描述】:

ErrorException: Trying to get property of non-object: 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

控制器 -

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

数据库:-

【问题讨论】:

  • 以后请在本站发布代码
  • 行号通常会出现该错误。这会很有用,并且在您的代码中也有该行存在的一些指示
  • 它实际上没有显示任何行号......我会在这里发布代码,谢谢你先生......

标签: php database laravel exception laravel-5.2


【解决方案1】:

请改写return view('stations.show')-&gt;withStation('$station');like return view('stations.show')-&gt;with(['station' =&gt; $station]);

【讨论】:

  • 为什么 OP 应该“试试这个”? 好的答案将始终解释所做的工作以及这样做的原因,不仅适用于 OP,而且适用于可能会发现此问题并正在阅读您的答案的 SO 的未来访问者。
【解决方案2】:

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

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

在大多数情况下使用 compact 更快更简洁,因为您已经构建了变量。

【讨论】:

    【解决方案3】:

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

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

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

    【讨论】:

      【解决方案4】:

      将方法public function create()改为:

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

      【讨论】:

        猜你喜欢
        • 2017-08-02
        • 2018-04-14
        • 1970-01-01
        • 1970-01-01
        • 2020-09-30
        • 1970-01-01
        • 2021-07-19
        • 2017-02-25
        • 1970-01-01
        相关资源
        最近更新 更多