dtvfxzq3802 2017-12-19 02:13
浏览 60
已采纳

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

So I have this relationship defined in the holiday model to access the holiday_type model.

MHoliday model:

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 model:

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 model:

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',
    ];
}

Then, I try show the result like:

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

The Office name gives me result, but the holiday type throws me error "Trying to get property of non-object". What could be lacking?

Update

In my view, it is partly like this:

@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

In the Holiday controller:

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条回答 默认 最新

  • doulao7572 2017-12-19 12:40
    关注

    You should try to debug the result for your collection first because this exception mines that your table

    m_holiday_type

    have no records who match the relation.

    do this in your controller before the return view :

    dd($holidays);
    

    and tell display me the result please.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动