За последние 24 часа нас посетили 22853 программиста и 1268 роботов. Сейчас ищут 800 программистов ...

не работает count

Тема в разделе "Laravel", создана пользователем Qvei, 23 май 2020.

  1. Qvei

    Qvei Новичок

    С нами с:
    10 ноя 2019
    Сообщения:
    60
    Симпатии:
    4
    не работает eloquent comment4s_count.
    Количество постов пользователя считает, а кол-во коментариев пользователя не хочет.
    Ошибок нет, сайт работает нормально но кол-во коментариев отдельного пользователя просто не показывает игнорируя переменную $usercommentcount во view.blade которая прописана в контроллере. подскажите пожалуйста почему так?
    есть контроллер постов
    Код (Text):
    1. public function index()
    2.     {
    3.  
    4.         $newposts = \App\Newpost::latest('date')->get();
    5.         $newposts = \App\Newpost::incomplete();
    6.         $ccomment = \App\Newpost::count();
    7.         if (Auth::check()){
    8.         $user = auth()->user();
    9.         $userposts = $user->newposts_count;
    10.         $usercommentcount = $user->comment4s_count;
    11.         return view('newposts.index', compact('newposts', 'ccomment', 'userposts', 'usercommentcount'));
    12.     } else {
    13.         return view('newposts.index', compact('newposts', 'ccomment'));
    14.     }
    15.          
    16.     }
    есть контроллер коментов

    Код (Text):
    1. <?php
    2.  
    3. namespace App\Http\Controllers;
    4.  
    5. use Illuminate\Http\Request;
    6. use App\Comment4;
    7. use App\Newpost;
    8.  
    9. class Comment4Controller extends Controller
    10. {
    11.  
    12.         public function __construct()
    13.     {
    14.         $this->middleware('auth');
    15.     }
    16.  
    17.         public function store(Request $request)
    18.     {
    19.  
    20.         $this->validate($request, [
    21.         'comment4_body' => ['required','min:3' ,'max:500'],
    22.         'comment4_user_id' => 'required'
    23.         ]);
    24.        
    25.         $comment4 = new Comment4;
    26.         $comment4->body = $request->get('comment4_body');
    27.         $comment4->user_id = $request->get('comment4_user_id');
    28.         $comment4->user()->associate($request->user());
    29.         $newposts = Newpost::find($request->get('newposts_id'));
    30.         $newposts->comment4s()->save($comment4);
    31.  
    32.         return back();
    33.     }
    34. }
    есть модель User

    Код (Text):
    1. <?php
    2.  
    3. namespace App;
    4.  
    5. use Illuminate\Contracts\Auth\MustVerifyEmail;
    6. use Illuminate\Foundation\Auth\User as Authenticatable;
    7. use Illuminate\Notifications\Notifiable;
    8.  
    9. class User extends \TCG\Voyager\Models\User
    10. {
    11.     use Notifiable;
    12.  
    13.     /**
    14.      * The attributes that are mass assignable.
    15.      *
    16.      * @var array
    17.      */
    18.     protected $fillable = [
    19.         'name', 'email', 'password',
    20.     ];
    21.  
    22.     /**
    23.      * The attributes that should be hidden for arrays.
    24.      *
    25.      * @var array
    26.      */
    27.     protected $hidden = [
    28.         'password', 'remember_token',
    29.     ];
    30.  
    31.     /**
    32.      * The attributes that should be cast to native types.
    33.      *
    34.      * @var array
    35.      */
    36.     protected $casts = [
    37.         'email_verified_at' => 'datetime',
    38.     ];
    39.  
    40.  
    41.  
    42.  
    43.  
    44.     public function comment4s()
    45.     {
    46.     return $this->hasMany(Comment4::class);
    47.     }
    48.  
    49.    
    50.  
    51.     public function getComment4CountAttribute(){
    52.     return $this->comment4s->count();
    53.     }
    54.  
    55.     public function getNewpostCountAttribute(){
    56.     return $this->newposts->count();
    57. }
    58.  
    59.    
    60. }
    модель коментов Comment4

    Код (Text):
    1. <?php
    2.  
    3. namespace App;
    4.  
    5. use Illuminate\Database\Eloquent\Model;
    6. use App;
    7.  
    8. class Comment4 extends Model
    9. {
    10.  
    11.     protected $fillable = ['care', 'comment4_user_id', 'comment4_body'];
    12.  
    13.    
    14.     public function user()
    15.     {
    16.         return $this->belongsTo(User::class);
    17.     }
    18.  
    19.     public function newposts(){
    20.         return $this->belongsTo('App\Newpost');
    21.     }
    22.  
    23.     public function getComment4CountAttribute(){
    24.     return $this->comment4s->count();
    25.     }
    26. }
     
  2. twim32

    twim32 Активный пользователь

    С нами с:
    29 мар 2017
    Сообщения:
    275
    Симпатии:
    58
    Qvei нравится это.