Issue with post categories in blade

Posted 1 month ago by Gabotronix

I'm trying to display font awesome icons depending of the category name of my posts but I'm getting the following error:

Trying to get property 'name' of non-object (View: C:\xampp\htdocs\TemplateTest\resources\views\inicio\index.blade.php)

This line right here:

@switch($post->postcategories->name)

This is my blade template:

<section id="eventos" class="section_post_palo_maincontainer section_margin" style="">
    @foreach($posts as $post)
    <div class="section_post_palo_content_container" style="">
        <div class="section_post_palo_content_image" style="background-image:url('{{ url($post->image) }}');"></div>
        <div class="section_post_palo_content_text_container" style="">
            <span class="section_post_palo_content_text_title" style="">
                @switch($post->postcategories->name)
                    @case('Evento')
                    <i class=" fa far fa-calendar-alt" style=""></i>
                    @break
                    @case('Notícia')
                    <i class="fa fas far fa-newspaper" style=""></i>
                    @break
                    @case('Artículo')
                    <i class="fa fas fa-bullhorn" style=""></i>
                    @break
                @endswitch
                {{  $post->title  }}
            </span>
            <span class="section_post_palo_content_text_paragraph" style="">{!!  App\Post::getExcerpt($post->body)  !!}</span>
            <a class="section_post_palo_content_text_link" href="/publicaciones-y-eventos" style="">( leer más )</a>
        </div>
    </div>
    @endforeach
</section>

This is the controller where I grab my posts data:

public function inicio()
    {

        $pageInfo = 
        [
            'page_title'      => 'Inicio',
            'menu_active'     => 'Inicio',
            'submenu_active' => '',
        ];
        
        $posts = Post::lastThreeActivesWithCategories();


        
        return view('inicio.index', compact('pageInfo', 'posts'));
    }

Post.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    protected $table = 'posts';
    protected $fillable = ['title', 'body', 'isVisible', 'image'];
    
    
    
    public function postcategories()
    {
        return $this->belongsTo('App\PostCategory');
    }



    public function scopeLastThreeActivesWithCategories($query)
    {
        return $query->where('isVisible', 1)->with('postcategories')->latest()->take(3)->get();
    }
    

PostCategory.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class PostCategory extends Model
{
    protected $table = 'postcategories';
    protected $fillable = ['name', 'isVisible'];
    
    
    
    
    public function posts()
    {
        return $this->hasMany('App\Post');
    }
}

Migration files

public function up()
    {



        //CREATE CATEGORIES TABLE
        Schema::create('postcategories', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->boolean('isVisible')->default(true);
            $table->timestamps();
        });
        //FILL CATEGORIES TABLE WITH CATEGORIES
        DB::table('postcategories')->insert([
            'name' => 'Evento',
        ]);
        DB::table('postcategories')->insert([
            'name' => 'Notícia',
        ]);
        DB::table('postcategories')->insert([
            'name' => 'Artículo',
        ]);




        //CREATE POSTS TABLE
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('postcategory_id')->index();
            $table->foreign('postcategory_id')->references('id')->on('postcategories')->onDelete('cascade');
            $table->string('title');
            $table->longText('body');
            $table->string('image')->nullable();
            $table->boolean('isVisible')->default(true);
            $table->timestamps();
        });
        //FILL POSTS TABLE WITH DUMMY POSTS
        DB::table('posts')->insert([
            'postcategory_id' => 2,
            'title' => config('globals.post_title_1'),
            'body' => config('globals.post_body_1'),
            'image' => config('globals.post_image_1'),
            'isVisible' => true,
            'created_at' => Carbon::now(),
        ]);
        DB::table('posts')->insert([
            'postcategory_id' => 1,
            'title' => config('globals.post_title_2'),
            'body' => config('globals.post_body_2'),
            'image' => config('globals.post_image_2'),
            'isVisible' => true,
            'created_at' => Carbon::now(),
        ]);
        DB::table('posts')->insert([
            'postcategory_id' => 2,
            'title' => config('globals.post_title_3'),
            'body' => config('globals.post_body_3'),
            'image' => config('globals.post_image_3'),
            'isVisible' => true,
            'created_at' => Carbon::now(),
        ]);
        

    }

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.