all. I have problems after making some new steps. I'm rails developer, but now i have to do some support in php/laravel project. After making some UI + backend in project(adding OpenGraph in project + admin) - i made a command - php artisan migrate:fresh. And now i have this code error.

(1/1) ErrorException Trying to get property 'title' of non-object

in MainController.php line 78

MetaTag.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

/**
 * App\Setting
 *
 * @mixin \Eloquent
 */
class MetaTag extends Model
{
    /**
     * @var array
     */
    protected $table = "meta_tags";
    protected $fillable = ['title', 'description', 'keywords','og_type','og_title','og_description'];

}

part of MainController.php




<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Jenssegers\Agent\Agent;
use App\Http\Requests;

class MainController extends Controller
{
    public function index()
    {
        $main_banner = \App\Banner::where('type', '1')
            ->where('page', '1')
            ->where('active', '1')
            ->orderBy('order', 'asc')
            ->first();

        $premium_blocks = \App\Banner::where('type', '2')
            ->where('active', '1')
            ->orderBy('order', 'asc')
            ->get();

        $active_animations = \App\Setting::where('alias', 'anumations_active')->first();
        $active_films = \App\Setting::where('alias', 'films_active')->first();
        $active_games = \App\Setting::where('alias', 'games_active')->first();
        $active_heroes = \App\Setting::where('alias', 'heroes_active')->first();
        $active_news = \App\Setting::where('alias', 'news_active')->first();
        $active_soundtracks = \App\Setting::where('alias', 'soundtracks_active')->first();

        $animations = \App\Animation::where('active', '1')
            ->orderBy('order', 'asc')
            ->get();
        $films = \App\Film::where('active', '1')
            ->where('checked', '1')
            ->orderBy('order', 'asc')
            ->get();
        $games = \App\Game::where('active', '1')
            ->where('checked', '1')
            ->orderBy('order', 'asc')
            ->get();
        $heroes = \App\Hero::where('active', '1')
            ->where('checked', '1')
            ->orderBy('order', 'asc')
            ->get();
        $news = \App\News::where('active', '1')
            ->where('checked', '1')
            ->orderBy('order', 'asc')
            ->get();
        $soundtracks = \App\Soundtrack::where('active', '1')
            ->where('checked', '1')
            ->orderBy('order', 'asc')
            ->get();

        $arr_month_rus = \Config::get('settings.arr_month_rus');
        $arr_month_rus_lower = \Config::get('settings.arr_month_rus_lower');
        $arr_games_types = \Config::get('settings.arr_games_types');
        $arr_news_types = \Config::get('settings.arr_news_types');

        $agent = new Agent();

        $browser = $agent->browser();
        $v = explode('.',$agent->version($agent->browser()));
        $version = $v[0];

        if (
            ($browser == "Internet Explorer" && $version > 10) ||
            ($browser == "Edge" && $version > 11) ||
            ($browser == "Firefox" && $version > 46) ||
            ($browser == "Opera" && $version > 38) ||
            ($browser == "Safari" && $version > 8) ||
            ($browser == "Chrome" && $version > 51)
        )
            $outdated = false;
        else $outdated = true;

        $meta_tags = \App\MetaTag::where('alias',  '=', 'main_page')->first();
        $title = $meta_tags->title;
        $description = $meta_tags->description;
        $keywords = $meta_tags->keywords;
        $og_title = $meta_tags->og_title;
        $og_description = $meta_tags->og_description;
        $og_type = $meta_tags->og_type;

        return view((($agent->isMobile()) ? 'mobile.home' : 'home'), compact(
            'main_banner',
            'premium_blocks',
            'animations',
            'films',
            'games',
            'heroes',
            'news',
            'soundtracks',
            'active_animations',
            'active_films',
            'active_games',
            'active_heroes',
            'active_news',
            'active_soundtracks',
            'arr_games_types',
            'arr_month_rus_lower',
            'arr_news_types',
            'arr_month_rus',
            'outdated',
            'title',
            'description',
            'keywords',
            'og_title',
            'og_type',
            'og_description'
        ));
    }

I expect just solve this little problem. Thanks!

3 Answers

0
Rouhollah Mazarei On

Check if this line returns anything:

$meta_tags = \App\MetaTag::where('alias',  '=', 'main_page')->first();

You can add an if statement like this (but set a default value for the variables to prevent errors):

$meta_tags = \App\MetaTag::where('alias', 'main_page')->first(); // You CAN ignore the equal sign
if($meta_tags){
    $title = $meta_tags->title;
    $description = $meta_tags->description;
    $keywords = $meta_tags->keywords;
    $og_title = $meta_tags->og_title;
    $og_description = $meta_tags->og_description;
    $og_type = $meta_tags->og_type;
} else {
    $title = "";
    $description = "";
    $keywords = "";
    $og_title = "";
    $og_description = "";
    $og_type = "";
}
0
samju On

I think you should try this. the logic here is to check if the record exist in the database first.

$meta_tags_count = \App\MetaTag::where('alias', 'main_page')->count();
if($meta_tags_count>0){
    $meta_tags = \App\MetaTag::where('alias', 'main_page')->first(); // You CAN  ignore the equal sign
    $title = $meta_tags->title;
    $description = $meta_tags->description;
    $keywords = $meta_tags->keywords;
    $og_title = $meta_tags->og_title;
    $og_description = $meta_tags->og_description;
    $og_type = $meta_tags->og_type;
}
 else {
    $title = "";
    $description = "";
    $keywords = "";
    $og_title = "";
    $og_description = "";
    $og_type = "";
}
0
UkraineInTheMembrane On

ErrorException Trying to get property 'title' of non-object...

typically means you're not returning any records / NULL records. Just re-write your query a bit to catch that.

Laravel has a great helper ->exists() that can help you out here.

Something along the lines of this.

if( \App\MetaTag::where('alias', 'main_page')->exists() >= 1 )
{
    // Your record exists 
    $metaTags = \App\MetaTag::where('alias', 'main_page')->first();
    $title = $metaTags->title;

    dd( $title );
}