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 运筹学排序问题中的在线排序
  • ¥15 关于#flink#的问题:关于docker部署flink集成hadoop的yarn,请教个问题flink启动yarn-session.sh连不上hadoop
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题