shahzadyounas's avatar

Trying to get property 'id' of non-object - how to fix

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)"
''

blade code is

                        <td ><?php echo e($row->id); ?></td>
                                                <td><?php echo e($row->departmentname); ?></td>
                                                <td><?php echo e($row->depstartdate); ?></td>

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


0 likes
3 replies
Cronix's avatar
Cronix
Best Answer
Level 67

Best guess, since you don't provide all of the view code, is because here:

$depart= Department::all()->toArray();

You're converting $depart to an array, but trying to access as if it were an object.

<td ><?php echo e($row->id); ?></td>
<td><?php echo e($row->departmentname); ?></td>
<td><?php echo e($row->depstartdate); ?></td>

Remove the ->toArray() and it should work fine.

I'm not sure why you're doing

<td ><?php echo e($row->id); ?></td>

when you can use blade syntax, like

<td >{{ $row->id }}</td>
5 likes

Please or to participate in this conversation.