Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

abduljakul-salsalani's avatar

I can't display this php statement inside my blade file from flatMap result in many2 many

this is in my blade file where I display my results


@php
                $activityTypeId   =          $paper->id ;
                $activityCreator  =            $paper->pivot->display_name;
                $paperId          =         $paper->pivot->paper_id;

                        $route            =         `<a class="text-black" href="{{ route('papers.show',$paperId) }}"></a>` ;         

                        switch ($activityTypeId) {
                            case 1:
                            $activityTypePhrase = $activityCreator . " Activity 1 " .$route  ;
                            break;    

                            case 2 :
                            $activityTypePhrase = $activityCreator . " Activity 2".$route; 
                            break;
                        }

                   @endphp
                        <p class="text-black">{{  $activityTypePhrase }}</p>

from this query that this static function returns the results in a many to many relationship

public static function fetchActivitiesForAStudent(){

        if (auth()->check() == false) {
            abort(403, "Unauthorized");
        }

        $userId = auth()->id();

        $papers = Article::where('owner','=',$userId)->has('activityCreators')
                        ->with('activityCreators','activityTypes')
                        ->get();


        return $papers->flatMap->activityCreators ; 
        // return collect([$papers, $activity]) ;
        

    }
0 likes
9 replies
Snapey's avatar

You have blade tags {{ }} inside a PHP block. Thats not going to work. You should just concatenate the strings together.

$route            =         `<a class="text-black" href="` . route('papers.show',$paperId) . `"></a>`

By the way, PHP in your view (and a large piece as well) is sign of a bad design.

abduljakul-salsalani's avatar

and what's in this : laravel~5.5/docs/5.5/blade

 @switch($i)
    @case(1)
        First case...
        @break

    @case(2)
        Second case...
        @break

    @default
        Default case...
@endswitch
jlrdw's avatar

and what's in this : laravel~5.5/docs/5.5/blade

A switch statement. ??

abduljakul-salsalani's avatar

Therefore you can use switch statements in a blade file, I just used a more traditional approach with php syntax not hte ones using blade

jlrdw's avatar

But as snapey said you put some blade inside the php

href="{{ route('papers.show',$paperId) }}

Pick one or the other. Blade compiles to php at runtime.

Snapey's avatar

but whats all this;

                $activityTypeId   =          $paper->id ;
                $activityCreator  =            $paper->pivot->display_name;
                $paperId          =         $paper->pivot->paper_id;

why not

    @switch($paper->id)

by the way, using 'magic numbers' is also a code smell

case 1:

case 2:

What do they mean to me? what do they mean to you in a year?

Please or to participate in this conversation.