Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

650
Views
passing data from controller to blade view laravel

I am trying to send parameters through a href to a page from an events list to an event page.

My route is

 Route::get('eventpage', 'EventController@index')->name('eventpage');

And my event Controller is

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

   class EventController extends Controller
{

public function index(Request $request)
{
    $id = $request->query('id');
    $event = DB::table('events')->where('id',$id)->get();
    $pics = DB::table('pictures')->where('event_id',$id)->get();
    $n = count($pics); // the number of pictures for a particular event
    return view('pages.eventPage');
}

}

The trouble is that for the first variable I try to use, $n, it gives me an error, "Undefined variable: n "

My blade code is as follows

@for($i = 1; $i < $n; $i++)
<li data-target="#carousel-example-generic" data-slide-to="{{ $i }}"></li>
@endfor

What am I doing wrong?

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

route

 Route::get('eventpage', 'EventController@index')->name('eventpage');

event Controller

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

   class EventController extends Controller
{

public function index(Request $request)
{
    $id = $request->query('id');
    $event = DB::table('events')->where('id',$id)->get();
    $pics = DB::table('pictures')->where('event_id',$id)->get();

return view('pages.eventPage',compact('event','pics'));

}

}

blade code

@for($i = 1; $i < count($pics); $i++)
<li data-target="#carousel-example-generic" data-slide-to="{{ $i }}"></li>
@endfor
about 4 years ago · Santiago Trujillo Report

0

You can pass your data to your view like that:

public function index(Request $request)
{
  ....

  return view('pages.eventPage', compact('id', 'event', 'n'));
}
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!