G
G
greschnikoff2018-04-04 20:02:15
Laravel
greschnikoff, 2018-04-04 20:02:15

LARAVEL Trying to get property of non-object how to fix?

Throws this error:
ErrorException in PagesController.php line 477:
Trying to get property of non-object
at HandleExceptions->handleError('8', 'Trying to get property of non-object', 'C:\laragon\www\astrixbets \app\Http\Controllers\PagesController.php', '477', array('history' => object(Collection), 'lives' => object(Collection), 'coming' => object(Collection), 'latest ' => null)) in PagesController.php line 477
at PagesController->index()

public function index(){
        $history = History::where('id','>',0)->orderBy('id','desc')->limit(3)->get();
        $lives = Tournaments::where('live',1)->where('status',1)->get();
        $coming = Tournaments::where('live',0)->where('status',0)
            ->where('team1','!=','no team1')
            ->where('team2','!=','no team2')
            ->where('date','>=',Carbon::now()->timestamp)
            ->orderBy('date','ASC')
            ->take(10)
            ->get();
        $latest = Tournaments::where('live',0)->where('status',0)->where('date','>=',Carbon::now()->timestamp)->orderBy('date','ASC')->first();
        $last = Carbon::createFromTimestamp($latest->date)->toDateTimeString();//477 stroka
        if(count($lives) == 0){
            $c = 0;
        }else{
            $c = 1;
        }
        if(count($coming) == 0){
            $pages = 1;
        }else{
            $pages = round(count($coming)/10);
        }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Kirill Nesmeyanov, 2018-04-04
@SerafimArts

LARAVEL Trying to get property of non-object how to fix?

If the message is "cannot access a field of something that is not an object", then the fix is ​​simple:
You should try to get the field from the object, and not from something else.
You can't thank. Always happy to help.

V
Vladislav Nagorny, 2018-04-05
@esvils

$latest выдает null
and then there is a crash
for laravel 5.6 you can use
the same instead of
You can use a mutator

в Tournaments
/**
     * The attributes that should be mutated to dates.
     *
     * @var array
     */
    protected $dates = [
        'created_at',
        'updated_at',
        'date'
    ];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question