3

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?

4
  • 1
    there are a lot of questions that explains this, typing your question title will lead you to a tons of similar questions. Commented Apr 8, 2017 at 10:59
  • 1
    i figured, I just didn't know how to formulate it until I already wrote the question, so I let it be... Commented Apr 8, 2017 at 11:08
  • Does this answer your question? Passing data from controller to view in Laravel Commented Jan 23, 2023 at 16:20
  • @steven7mwesigwa my question is so old, I don't even work with laravel anymore haha Commented Feb 3, 2023 at 10:58

2 Answers 2

5

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
Sign up to request clarification or add additional context in comments.

Comments

1

You can pass your data to your view like that:

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

  return view('pages.eventPage', compact('id', 'event', 'n'));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.