beginner
beginner

Reputation: 2032

Laravel get relation data

I'm trying to get the relation data but I get Trying to get property 'district_name' of non-object error. I'm Just learning laravel.

enter image description here

Here is my models:

LibSchool

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class LibSchool extends Model
{
    function getAllSchool()
    {
        return LibSchool::all();
    }

    public function district()
    {
        return $this->belongsTo('App\LibDistrict');
    }
}

LibDistrict

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\LibSchool;

class LibDistrict extends Model
{
    public function schools()
    {
        return $this->hasMany('App\LibSchool');
    }
}

Tables

enter image description here enter image description here

CONTROLLER

class SchoolController extends Controller
{
    public function index()
    {
        $q = Input::get('q');
        if ($q != "") {
            $schools = LibSchool::where('school_name', 'LIKE', '%' . $q . '%')
                        ->with('district')
                        ->orWhere('school_id', 'LIKE', '%' . $q . '%')
                        ->orWhere('address', 'LIKE', '%' . $q . '%')
                        ->orWhere('school_head', 'LIKE', '%' . $q . '%')
                        ->orWhere('level', 'LIKE', '%' . $q . '%')
                        ->orderBy('school_name', 'asc')
                        ->paginate(15)->appends('q', $q);
        } else {
            $schools = LibSchool::with('district')
                                ->orderBy('school_name', 'asc')
                                ->paginate(15);
        }
        return view('schools.index')->with('data', ['schools' => $schools, 'q' => $q]);
    }
}

VIEW

<table class="table table-hover table-bordered table-striped">
    <tr>
        <th>School ID</th>
        <th>School NAME</th>
        <th>Address</th>
        <th>School Head</th>
        <th>Level</th>
        <th>Division</th>
        <th>District</th>
    </tr>
    @if($data['schools']->total() > 0)
    @foreach($data['schools'] as $school)
    <tr>
        <td>{{$school->school_id}}</td>
        <td>{{$school->school_name}}</td>
        <td>{{$school->address}}</td>
        <td>{{$school->school_head}}</td>
        <td>{{$school->level}}</td>
        <td>{{$school->district->district_name}}</td>
    </tr>
    @endforeach
    @else
    <td colspan="7" class="text-danger">No records</td>
    @endif
</table>
{{$data['schools']->links()}}

Upvotes: 0

Views: 154

Answers (3)

Subin Shakya
Subin Shakya

Reputation: 60

The problem in your given code is that you have used

public function district()
{
return $this->belongsTo('App\LibDistrict');
}

which assumes that you have libdistrict_id as foreign key in district table. But looking at your table it seems that you have assigned it as lib_district_id. So, now you have to explicitly put the proper column name like follows:

public function district()
{
return $this->belongsTo('App\LibDistrict', 'lib_district_id', 'id');
}

Note: It's always better to use the naming convention provided by laravel. That way our code can be much less and clean.

Hope this helps!! Have fun coding!! :)

Upvotes: 1

Lakhwinder Singh
Lakhwinder Singh

Reputation: 5582

you may try this

{{ $school->district ? $school->district->district_name : 'N/A' }}

By this, if your school district exists then it will print district name otherwise it will show N/A.

Upvotes: 1

mohamed hassan
mohamed hassan

Reputation: 489

Can you write {{ dd($school->district) }} and check the result in the index page before

<td>{{$school->school_id}}</td>

Upvotes: 1

Related Questions