shahzadyounas's avatar

Trying to get property 'id' of non-object

My project working well, but suddenly i got an error when i add data in from and submit .

"Trying to get property 'id' of non-object (View: C:\xampp\htdocs\opms1\resources\views\admin\allDepartment.blade.php)"

my code is

  <tr class="even">
                                                <td ><?php echo e($row->id); ?></td>
                                                <td><?php echo e($row->departmentname); ?></td>
                                                <td><?php echo e($row->depstartdate); ?></td>
                                            

0 likes
3 replies
click's avatar
click
Best Answer
Level 35

The error says it all: variable $row is not an object. It is something else. With only this piece of code we can't give you a solution.

shahzadyounas's avatar

this is department controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Department;

class DepartmentController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $depart= Department::all()->toArray();
        return view('admin.allDepartment', compact('depart'));

    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        
        $s = new Department();
        $s->departmentname = $request->departmentname;
        $s->depstartdate = $request->depstartdate;
       
        $s->save();
        return redirect("department");
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
         
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $editdepart= department::find($id);
        return view ('admin.editDepartment', compact('editdepart'));

    }

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

and this is migrate table code

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddDepartmentToTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
         Schema::create('department', function (Blueprint $table) {

            $table->increments('id');
            $table->string('departmentname')->unique();
            $table->string('depstartdate');
            $table->rememberToken();
            $table->timestamps();
            });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('department', function (Blueprint $table) {
    
        });
    }
}


click's avatar

your migration is not related to the issue. Your view is. What view are we looking at?

  <tr class="even">
                                                <td ><?php echo e($row->id); ?></td>
                                                <td><?php echo e($row->departmentname); ?></td>
                                                <td><?php echo e($row->depstartdate); ?></td>

Which view is this? And what is wrapped around it? I suppose you are using a foreach to loop over multiple rows?

Figuring this out should be part of PHP 101 and you should be able to figure this out with basic debugging logic:

  • Ok, you have an error that says '$row' is not an object. You should ask yourself: Why is this not an object and if it is not an object what is it? An array? A String?
  • How can you figure that out? Simplest way is to just print the variable $row like: dd($row) or var_export($row). I guess you will see a string or an array in this case.
  • The question now is, why do you see that value? Probably because the variable in your foreach is not correct. It should be an array of objects or an collection of objects. If it is something else you will get an error like the one you see.

btw, why are you using: <?php echo e() ?> aren't you using blade as a html template engine? https://laravel.com/docs/5.6/blade

1 like

Please or to participate in this conversation.