@foreac laravel code example

Example 1: @foreac laravel

@foreach ($users as $user)
    

This is user {{ $user->id }}

@endforeach @forelse ($users as $user)
  • {{ $user->name }}
  • @empty

    No users

    @endforelse

    Example 2: @foreac laravel

    @if(count($posts) > 1)
        @foreach($posts as $post)
            

    {{$post->title}}

    @endforeach @else

    no posts found

    @endif

    Example 3: @foreac laravel

    public function index()
    {
        $posts = Post::all();
        return view('Pages.welcome')->with('posts', $posts);
    }

    Example 4: @foreac laravel

    Route::get('/index','PageController@HomePage');

    Example 5: @foreac laravel

    @foreach($posts as $post)

    Example 6: @foreac laravel

    /**
         * Compile the for-each statements into valid PHP.
         *
         * @param  string  $expression
         * @return string
         */
        protected function compileForeach($expression)
        {
            preg_match('/\( *(.*) +as *(.*)\)$/is', $expression, $matches);
    
            $iteratee = trim($matches[1]);
    
            $iteration = trim($matches[2]);
    
            $initLoop = "\$__currentLoopData = {$iteratee}; \$__env->addLoop(\$__currentLoopData);";
    
            $iterateLoop = '$__env->incrementLoopIndices(); $loop = $__env->getLastLoop();';
    
            return "";
        }
    
    
       /**
         * Compile the for-else statements into valid PHP.
         *
         * @param  string  $expression
         * @return string
         */
        protected function compileForelse($expression)
        {
            $empty = '$__empty_'.++$this->forElseCounter;
    
            preg_match('/\( *(.*) +as *(.*)\)$/is', $expression, $matches);
    
            $iteratee = trim($matches[1]);
    
            $iteration = trim($matches[2]);
    
            $initLoop = "\$__currentLoopData = {$iteratee}; \$__env->addLoop(\$__currentLoopData);";
    
            $iterateLoop = '$__env->incrementLoopIndices(); $loop = $__env->getLastLoop();';
    
            return "";
        }

    Example 7: @foreac laravel

    public function __construct()
    {
        $this->middleware('auth');
    }
    
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {
        return view('home');
    }

    Example 8: @foreac laravel

    public function index()
    {
        $posts = Post::all();
        return view('posts.index')->with('posts', $posts);
    }

    Example 9: @foreac laravel

    Route::get('/', 'PageController@index');
      Route::get('/welcome','PageController@Welcome');
      Route::get('/services', 'PageController@services');
      Route::get('/register', 'PageController@register');
      Route::get('/Create', 'PageController@Create');
      Route::get('/search', 'PageController@search');
      Route::get('/payment', 'PageController@Payment');
    
    
    
      Route::resource('posts', 'PostsController');
      Route::resource('search', 'SearchController');
      Route::resource('reviews', 'ReviewsController');

    Tags:

    Misc Example