Are you passing a Tutorial model to your view?
Showing your Controller might help.
i want to show my tutorials page in localhost:8000/tutorials and make relationship between Tutorial Model and Category Model
Tutorial.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use TCG\Voyager\Facades\Voyager;
use TCG\Voyager\Traits\Translatable;
use App\Comment;
use TCG\Voyager\Models\Category;
class Tutorial extends Model
{
use Translatable;
protected $translatable = ['title', 'seo_title', 'excerpt', 'body', 'slug', 'meta_description', 'meta_keywords'];
const PUBLISHED = 'PUBLISHED';
protected $guarded = [];
public function save(array $options = [])
{
// If no author has been assigned, assign the current user's id as the author of the post
if (!$this->author_id && Auth::user()) {
$this->author_id = Auth::user()->id;
}
parent::save();
}
public function authorId()
{
return $this->belongsTo(Voyager::modelClass('User'), 'author_id', 'id');
}
/**
* Scope a query to only published scopes.
*
* @param \Illuminate\Database\Eloquent\Builder $query
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopePublished(Builder $query)
{
return $query->where('status', '=', static::PUBLISHED);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function category()
{
return $this->hasOne(Voyager::modelClass('Category'), 'id', 'category_id');
}
/**
* Method for returning specific thumbnail for post.
*/
public function thumbnail($type)
{
// We take image from posts field
$image = $this->attributes['image'];
// We need to get extension type ( .jpeg , .png ...)
$ext = pathinfo($image, PATHINFO_EXTENSION);
// We remove extension from file name so we can append thumbnail type
$name = rtrim($image, '.'.$ext);
// We merge original name + type + extension
return $name.'-'.$type.'.'.$ext;
}
public function comments()
{
return $this->hasMany(Comment::class);
}
}
and the Category.php is
<?php
namespace TCG\Voyager\Models;
use Illuminate\Database\Eloquent\Model;
use TCG\Voyager\Facades\Voyager;
use TCG\Voyager\Traits\Translatable;
use TCG\Voyager\Models\Post;
use App\Tutorial;
class Category extends Model
{
use Translatable;
protected $translatable = ['slug', 'name'];
protected $table = 'categories';
protected $fillable = ['slug', 'name'];
public function posts()
{
return $this->hasMany(Voyager::modelClass('Post'))
->published()
->orderBy('created_at', 'DESC');
}
//
public function tutorials()
{
return $this->belongsTo(Tutorial::class);
}
//
public function parentId()
{
return $this->belongsTo(self::class);
}
public function getRouteKeyName()
{
return 'slug';
}
}
but its show whoops called ''undefined variable tutorial"
this is my index.blade.php to show the page
@if ($tutorial->category->slug == 'desain-grafis')
@foreach($tutorials as $tutorial)
<div class="col-md-3">
<a href="{{url('tutorial',$tutorial->slug)}}"><div class="card">
<img class="card-image-top" src="/storage/{{$tutorial->image}}" alt"card image"></a>
<a href="{{url('category',$tutorial->category->slug)}}" <span class="category">{{$tutorial->category->name}}</span></br>
<h2 class="card-title">{{$tutorial->title}}</h2>
<div class="commit-button-default">
<a href="{{url('tutorial',$tutorial->slug)}}" id="commit-button">See Detail</a>
</div>
</div>
</div>
@endforeach
@endif
Please or to participate in this conversation.