ramsai
ramsai

Reputation: 91

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

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

Trying to retreive data from database.

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

Upvotes: 0

Views: 8449

Answers (4)

Kamil Kiełczewski
Kamil Kiełczewski

Reputation: 92327

Change method public function create() to:

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

Upvotes: 1

Ryan
Ryan

Reputation: 178

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.

Upvotes: 2

Jose Garrido
Jose Garrido

Reputation: 732

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

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

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

Upvotes: 1

Sid
Sid

Reputation: 5833

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

Upvotes: 0

Related Questions