Rodrigo
Rodrigo

Reputation: 37

Laravel Invalid argument supplied for foreach() fetching individual columns

can somebody help with this? I'm trying to pass some data from my Controller page to my index, but I'm getting this error "Invalid argument supplied for foreach()"

Here's my model

class Bookings extends Model
{
    //Table name
  protected $table = 'bookings';
  //Primary key
  public $primaryKey = 'book_id';
}

My Controller

use Illuminate\Http\Request;
use App\Bookings;

class PagesController extends Controller
{
    public function index(){

      $data = Bookings::orderBy('NameENG', 'DetailsENG')->get();
      $data = Bookings::all();
      $marks = 'NameENG';
      $briefs = 'DetailsENG';
      return view('pages/index', compact(['data', 'marks', 'briefs']));
    }
}

and part of my index

<div class = "container">
      @if(count($data) > 0)
        @foreach($marks as $mark)
          @foreach($briefs as $brief)
            <div class="row">
              <div class="col-md-4">
                <div class="card mb-4 shadow-sm">
                  <img class="card-img-top" alt="Thumbnail [100%x225]" style="height: 225px; width: 100%; display: block;" src="images/calle_yungay.png">
                  <div class="card-body">
                  <h5>{{$mark}}</h5>
                  <p>{{$brief}}</p>

Can someone help please?

Upvotes: 0

Views: 44

Answers (1)

Mayank Pandeyz
Mayank Pandeyz

Reputation: 26258

On controller function:

$routes = 'tourNameENG';  // is a string

and on view, you are trying to iterate it:

@foreach($routes as $route)

that's why the error Invalid argument supplied for foreach().

foreach(): works only on arrays and objects, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable.

Upvotes: 4

Related Questions