andil01
andil01

Reputation: 375

using belongsTo() function gives me error "Trying to get property of non-object"

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

Upvotes: 0

Views: 53

Answers (1)

Mathieu Mourareau
Mathieu Mourareau

Reputation: 1220

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.

Upvotes: 1

Related Questions