使用belongsTo()函数给我错误"试图获取非对象的属性"

时间:2017-12-19 02:13:10

标签: php laravel eloquent laravel-eloquent belongs-to

所以我在holiday模型中定义了这种关系来访问holiday_type模型。

MHoliday模型:

use Arkitecht\Attributions\Traits\Attributions;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class MHoliday extends Model
{
    use Attributions;
    use SoftDeletes;

    protected $dates = ['deleted_at'];
    protected $table = 'm_holiday';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'holiday_name',
        'holiday_date',
        'holiday_type_id',
        'office_id',
        'public_holiday_flag'
    ];

    public function holidayType()
    {
        return $this->belongsTo('App\MHolidayType', 'holiday_type_id');
    }

    public function officeName()
    {
        return $this->belongsTo('App\MOffice', 'office_id');
    }
}

MHolidayType模型:

namespace App;

use Arkitecht\Attributions\Traits\Attributions;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class MHolidayType extends Model
{
    use Attributions;
    use SoftDeletes;

    protected $dates = ['deleted_at'];
    protected $table = 'm_holiday_type';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'holiday_type_name'
    ];
}

MOffice模型:

namespace App;

use Arkitecht\Attributions\Traits\Attributions;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class MOffice extends Model
{
    use Attributions;
    use SoftDeletes;

    protected $dates = ['deleted_at'];
    protected $table = 'm_office';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'office_name',
        'office_authority_id',
        'note',
        'first_name',
        'employee_authority_id',
        'note',
    ];
}

然后,我尝试显示结果如:

<tr>
    <td>{{ $item->officeName->office_name }}</td>
    <td>{{ $item->holidayType->holiday_type_name}}</td>
</tr>

Office名称给我结果,但假日类型会引发错误&#34;试图获取非对象的属性&#34;。什么可能缺乏?

更新

在我看来,部分是这样的:

@foreach($holiday as $item)
    <tr>
        <td><input type="checkbox" class="items-selected" value="{{ $item->id }}"></td>
        <td>{{ $item->id }}</td>
        <td>{{ $item->holiday_name }}</td>
        <td>{{ $item->holidayType->holiday_type_name }}</td>

        <td>{{ $item->officeName->office_name }}</td>
        <td>{{ $item->public_holiday_flag }}</td>
@endforeach

在假日控制器中:

namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\MHoliday;
use App\MOffice;
use App\MHolidayType;
use Illuminate\Http\Request;
use Carbon\Carbon;

class HolidayController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\View\View
     */
    public function index(Request $request)
    {
        $keyword = $request->get('holiday');
        $getSearchby = $request->searchby;
        $perPage = 25;

        if ($keyword) {
            $holiday = MHoliday::join('m_office', 'm_office.id', '=', 'm_holiday.office_id')
                    ->where('m_office.office_name', 'like', '%'.$keyword.'%')
                    ->paginate($perPage);
        } else {
            $holiday = MHoliday::paginate($perPage);
        }

        return view('holiday.index', compact('holiday','searchby','getSearchby'));
    }
}

1 个答案:

答案 0 :(得分:1)

您应首先尝试调试集合的结果,因为此异常会挖掘您的表

  

m_holiday_type

没有与该关系匹配的记录。

return view

之前在您的控制器中执行此操作
dd($holidays);

请告诉我结果。