Autour de Laravel

injecter une variable dans mon controller

Avatar de ciol07
ciol07

Bonjour, j'ai récupérer les publications sur la vue /home, mais j'aimerai compter le nombre de commentaires de chaque publication en fonction de son ID spécifique et l'afficher dans /home avec une autre vue, j'arrive à le faire en injectant l'id de la publication. Mais au niveau de home c'est un foreach qui travail. Comment récupérer l'ID dépuis le foreach in l'envoyé au controller pour compter le nombre de commentaire spécifique à cet ID ?

 //ici ça marche car j'ai injecté l'ID,
$publi = Publish::find($request->id);
        $comments = $publi->comments;
        $nbcomment = $comments->count();
       
        
       // Mais je veux récupérer l'ID dépuis une boucle foreach
       //et remplacer $request->id par exemple : $publish->id
       // ou par $publish_id = $publish->id;
        
        //j'aimerai avoir ceci :
        
        $publi = Publish::find($publish_id);
        $comments = $publi->comments;
        $nbcomment = $comments->count();
        
       // $publish_id = id récupérer à la page home dans une boucle foreach
Avatar de Atef
Atef
$publi = Publish::withCount('comments')->get();

Normalement ça marchera comme ça

Avatar de ciol07
ciol07

public function index()
    {
        $publishes = Publish::where('user_id',Auth::user()->id)->orderBy('updated_at','DESC')->get();
        
        $publi = Publish::withCount('comments')->get();
        
        
        return view('home',
                    [
                     'publishes' => $publishes,
                     'publi' => $publi,
                     ]);
    }
Ce code m'affiche le contenu de publish repété plusieurs fois.
Avatar de ciol07
ciol07

j'obtiens ça

[{"id":44,"user_id":1,"type":"bigmatch","title":"test","district":"Plein ciel","stadium":"Ecole de biss\u00e9gu\u00e9","date":"2020-10-31","time":"23:56","description":"test EZFZFZ","image":"1604103711.jpg","created_at":"2020-10-28T05:20:59.000000Z","updated_at":"2020-10-31T00:21:51.000000Z","category":"","price":0,"contat":"062503339","state":"publish","comments_count":1},{"id":42,"user_id":2,"type":"bigmatch","title":"big match","district":"Mhindoub\u00e9","stadium":"Au tournant \u00e0 c\u00f4t\u00e9 de l'\u00e9cole","date":"2020-11-01","time":"15:00","description":"test publication","image":"","created_at":"2020-10-25T21:42:43.000000Z","updated_at":"2020-11-01T14:19:12.000000Z","category":"","price":0,"contat":"074067964","state":"publish","comments_count":1},{"id":41,"user_id":2,"type":"annonce","title":"test","district":"","stadium":"","date":"","time":"","description":"","image":"","created_at":"2020-10-25T21:42:30.000000Z","updated_at":"2020-11-01T14:24:28.000000Z","category":"","price":0,"contat":"074067964","state":"publish","comments_count":2},{"id":40,"user_id":2,"type":"annonce","title":"big match","district":"","stadium":"","date":"","time":"","description":"","image":"","created_at":"2020-10-25T21:40:18.000000Z","updated_at":"2020-10-25T21:40:18.000000Z","category":"","price":0,"contat":"","state":"no-publish","comments_count":0},{"id":39,"user_id":2,"type":"bigmatch","title":"test Tyshanne","district":"","stadium":"","date":"","time":"","description":"","image":"","created_at":"2020-10-25T21:38:38.000000Z","updated_at":"2020-10-25T21:38:38.000000Z","category":"","price":0,"contat":"","state":"no-publish","comments_count":0},{"id":50,"user_id":1,"type":"bigmatch","title":"Venez biguer entre pro","district":"Plein ciel","stadium":"Ecole de biss\u00e9gu\u00e9","date":"2020-11-07","time":"17:15","description":""On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse pains."","image":"SM1604107753.jpg","created_at":"2020-10-31T00:26:29.000000Z","updated_at":"2020-10-31T19:20:03.000000Z","category":"","price":0,"contat":"062503339","state":"publish","comments_count":16}]

public function index()
    {
        // Je récupère les publications avec ce code en utilisant la boucle foreach()
        $publishes = Publish::where('user_id',Auth::user()->id)->orderBy('updated_at','DESC')->get();
        
        
        // Menant je souhaite récupérer les commentaires de chaque id de $publishes
        // Ma plus grande difficulté c'est d'extraire chaque ID et de l'insérer dans une
        //fonction pour extraire les commentaires ratacher à ce ID
        //dans la vue /home
        
        $publi = Publish::withCount('comments')->get();
        
        
        return view('home',
                    [
                     'publishes' => $publishes,
                     'publi' => $publi
                     ]);
    }

Je suis nouveau en Laravel

Avatar de ciol07
ciol07

ça marche merci @Atef

public function index()
    {
        /*
        $publishes = Publish::where('user_id',Auth::user()->id)->orderBy('updated_at','DESC')->get();
        
        $publi = Publish::withCount('comments')->get();
        */
        $publishes = Publish::withCount(['comments',
                                         'comments as active_comment'])->where('user_id',Auth::user()->id)
        ->orderBy('updated_at','DESC')->get();
       
        
        
        return view('home',
                    [
                     'publishes' => $publishes
                     ]);
    }

Vous ne pouvez pas répondre à ce sujet.