не работает eloquent comment4s_count. Количество постов пользователя считает, а кол-во коментариев пользователя не хочет. Ошибок нет, сайт работает нормально но кол-во коментариев отдельного пользователя просто не показывает игнорируя переменную $usercommentcount во view.blade которая прописана в контроллере. подскажите пожалуйста почему так? есть контроллер постов Код (Text): public function index() { $newposts = \App\Newpost::latest('date')->get(); $newposts = \App\Newpost::incomplete(); $ccomment = \App\Newpost::count(); if (Auth::check()){ $user = auth()->user(); $userposts = $user->newposts_count; $usercommentcount = $user->comment4s_count; return view('newposts.index', compact('newposts', 'ccomment', 'userposts', 'usercommentcount')); } else { return view('newposts.index', compact('newposts', 'ccomment')); } } есть контроллер коментов Код (Text): <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Comment4; use App\Newpost; class Comment4Controller extends Controller { public function __construct() { $this->middleware('auth'); } public function store(Request $request) { $this->validate($request, [ 'comment4_body' => ['required','min:3' ,'max:500'], 'comment4_user_id' => 'required' ]); $comment4 = new Comment4; $comment4->body = $request->get('comment4_body'); $comment4->user_id = $request->get('comment4_user_id'); $comment4->user()->associate($request->user()); $newposts = Newpost::find($request->get('newposts_id')); $newposts->comment4s()->save($comment4); return back(); } } есть модель User Код (Text): <?php namespace App; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; class User extends \TCG\Voyager\Models\User { use Notifiable; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', 'email', 'password', ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; /** * The attributes that should be cast to native types. * * @var array */ protected $casts = [ 'email_verified_at' => 'datetime', ]; public function comment4s() { return $this->hasMany(Comment4::class); } public function getComment4CountAttribute(){ return $this->comment4s->count(); } public function getNewpostCountAttribute(){ return $this->newposts->count(); } } модель коментов Comment4 Код (Text): <?php namespace App; use Illuminate\Database\Eloquent\Model; use App; class Comment4 extends Model { protected $fillable = ['care', 'comment4_user_id', 'comment4_body']; public function user() { return $this->belongsTo(User::class); } public function newposts(){ return $this->belongsTo('App\Newpost'); } public function getComment4CountAttribute(){ return $this->comment4s->count(); } }
Вопрос 1: зачем? Я бы убрал это для начала и пользовался обычным $user->comments->count(); PHP: public function getComment4CountAttribute(){ return $this->comment4s->count(); } Вопрос 2: Зачем делать Коммент1, 2,3,4...? Всё гораздо проще! https://laravel.com/docs/7.x/eloquent-relationships#one-to-many-polymorphic-relations