Postingan lainnya
cara mengatasi Undifined method hasMany ?
masih belum paham di bagian hashmany error undifined nya, cara solving nya gimana ya ?
BadMethodCallException
Call to undefined method App\Models\Post::hashMany() (View: /home/wudstn/Documents/webstuf/iglike/resources/views/home.blade.php)
ini view nya:
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Dashboard') }}</div>
<div class="card-body">
<h3>Feed</h3>
@foreach ($posts as $post)
<div>
<img src="{{asset('images/posts/' . $post->image)}}" alt="{{$post->caption}}" width="200px" height="200px"/>
<a href="/{{'@'.$post->user->username}}">{{'@'.$post->user->username}}</a>
<button class="btn btn-primary" onclick="like({{$post->user->id}}, this)">
{{ ($post->is_liked() ? 'unlike' : 'like' ) }}
</button>
<script>
function like(id, el) {
fetch('/like/' + id)
.then(response => response.json())
.then(data => {
el.innerText = (data.status == 'LIKE') ? 'unlike' : 'like'
});
}
</script>`
</div>
@endforeach
</div>
</div>
</div>
</div>
</div>
@endsection
ini model:
<?php
namespace App\Models;
use Auth;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Post extends Model
{
use HasFactory;
protected $guarded = ['id'];
public function user()
{
return $this->belongsTo('App\Models\User');
}
public function likes()
{
return $this->hashMany('App\Models\Like');
}
public function is_liked()
{
return $this->likes->where('user_id', Auth::user()->id)->count();
}
}
Screenshot from 2021-01-25 14-40-24.pngScreenshot from 2021-01-25 14-40-20.pngScreenshot from 2021-01-25 14-23-03.png
0
1 Jawaban:
<div>lokasinya App\Models\Post udah bener???</div>
0
Tanggapan
udah bener