2016-07-26 89 views
0

ErrorException:試圖讓非對象的屬性:PHP初學者ErrorException:試圖讓非對象的屬性:(使用laravel)

試圖從數據庫retreive數據。

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

數據庫: - database

+0

請在未來 – RiggsFolly

+0

行號通常與該錯誤的推移本網站發佈的代碼。 **會很有用**,並在代碼中顯示該行的位置 – RiggsFolly

+0

它實際上沒有顯示任何行號......我將在此發佈代碼謝謝先生.. – ramsai

回答

2

當你在下面一行返回的觀點:

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

您需要可以使用雙引號或根本不使用引號。使用單引號,您實際上將值設置爲字符串"$station"而不是您創建的對象。

+0

非常感謝您先生... – ramsai

0

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

+0

爲什麼要OP「試試這個」?一個**好的答案**總是會解釋做了什麼以及爲什麼這樣做, 不僅適用於OP,而且適用於SO的未來訪問者,可能會發現此問題並正在閱讀您的答案。 – RiggsFolly

0

你可以試試你的控制器的編輯線84這樣的:

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

採用緊湊的是在大多數情況下更快,更清潔,因爲你已經建立的變量。

0

更改方法public function create()到:

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