Laravel获取关系数据

时间:2019-07-16 04:29:21

标签: laravel

我正在尝试获取关系数据,但出现Trying to get property 'district_name' of non-object错误。我只是在学习laravel。

enter image description here

这是我的模特:

图书馆学校

<?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');
    }
}

图书馆分区

<?php

namespace App;

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

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

表格

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

查看

<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()}}

3 个答案:

答案 0 :(得分:1)

您能写{{ dd($school->district) }}并在之前的索引页中检查结果吗

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

答案 1 :(得分:1)

您可以尝试

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

通过这种方式,如果您所在的学区存在,那么它将打印学区名称,否则将显示N/A

答案 2 :(得分:1)

给定代码中的问题是您使用过

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

假定您在libdistrict_id表中将district作为外键。但是从表上看,您似乎已将其分配为lib_district_id。因此,现在您必须显式地输入正确的列名称,如下所示:

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

注意:最好使用laravel提供的命名约定。这样,我们的代码就可以更少,更干净了。

希望这会有所帮助!!祝您编码愉快! :)