I get errors in laravel how do I echo the values correctly. I have this multidimensional array. This is the controller:
$data = array('names'=>$names, 'fruits'=>$fruits);
return view('content', [
'data' => $data
]);
and here is where I echo the values:
#section('content')
<div class="row">
<div class="col-md-12">
#foreach ($data as $row)
{{$row['fruits']}}
#endforeach
</div>
</div>
#endsection
You can send an array with key and values to your views. The key will be the variable name in your view. This is all very basic Laravel stuff and you can read more about it in the docs.
The controller:
$data = array('names'=>$names, 'fruits'=>$fruits);
return view('content', $data);
You view:
#section('content')
<div class="row">
<div class="col-md-12">
#foreach ($fruits as $fruit)
<!-- Do stuff with $fruit -->
#endforeach
</div>
</div>
#endsection
If you want to loop over them you could do that with your original controller, but this is how it would look in your view:
#section('content')
<div class="row">
<div class="col-md-12">
#foreach ($data as $item)
{{ $item }}
#endforeach
</div>
</div>
#endsection
This will only work if $fruits and $names are convertible to strings. Otherwise you would get an error.
$data is also an array. Access it's values with the keys $data["fruits"] or $data["names"]. If you want to loop through the $fruits-array just use a foreach-loop in your template:
foreach ($data["fruits"] as $key => $value) {
// make something with the fruit-entries.
}
Related
I've just started using the framework. In plain PHP after the opening foreach I would then set the variables then close the php tag but then from what I can work out you have to then do the Laravel #foreach tags and then open and close #php. Is there a way around this as it seems like a lot of extra work and code?
#foreach($steps as $row)
#php
$title = $row->title;
$text = $row->text;
$i = 1;
#endphp
<div class="steps-item grid-wrap">
<div class="number"
#if($text || $title)
<div class="text-wrap">
#if($title)
<h2>{{$title}}</h2>
#endif
{!! $text !!}
</div>
#php
$i++;
#endphp
#endif
</div>{{--END steps-item--}}
#endforeach
Since blade is no PHP, you have to return to PHP with that directive. But you can set/use the variables without doing that in your case:
#foreach($steps as $i => $row)
<div class="steps-item grid-wrap">
<div class="number"
#if($text || $title)
<div class="text-wrap">
#if($title)
<h2>{{ $row->title }}</h2>
#endif
{!! $row->text !!}
</div>
#php
$i++;
#endphp
#endif
</div>{{--END steps-item--}}
#endforeach
If you still want to set variables, there's a Laravel package called alexdover/blade-set. But as #brombeer pointed out, in most cases it's highly recommended to set all necessary variables in the controller before passing them to the view.
Use laravel provided loop variables:
$loop->iteration The current loop iteration (starts at 1).
It will increment in every loop iteration automatically.
e.g:
First iteration = $loop->iteration => 1 ;
Second iteration = $loop->iteration => 2 ;
so on until loop ends.
Check docs:
The Loop Variables
You can use a #for directive with sizeof($steps) like that:
#for($i=0; $i<= sizeof($steps)-1; $i++)
#endfor
#foreach ($steps as $row)
<div class="steps-item grid-wrap">
<div class="number">
<div class="text-wrap">
#if ($row->title != '')
<h2>{{$row->title}}</h2>
/* if you want to display another title when its blank you can
use if-else here otherwise not need to use if conditions for
title and text */
#endif
#if ($row->text != '')
{!! $row->text !!}
#endif
</div>
</div>
</div>
#endforeach
{{--
for an example you have $steps values like
$steps =
Array(
[0] -> 1,
[1] -> 'title',
[2] -> 'text'
);
if you want this array key value pair you have to use #foreach like
#foreach ($steps as $key=>$value)
#endforeach
you can use key value in #foreach loop only
--}}
I am trying to display the given array, but I cannot seem to get my head around the dimensions of it. I am exporting data from an excel sheet then to an array.
<?php
Excel::selectSheets('active_transport_sheet_1');
$reader->formatdates(true, 'd-m-Y');
$data = $reader->get(array('project_status', 'event_area', 'date_of_event', 'area_to_split',
'planned_split', 'actual_staff_on_split_requiring_transport', 'vehicle_type_required',
'project_number', 'event_name', 'driver_responsible_person',
'hire_stauts', 'shift_start_time', 'departure_date', 'departure_time'))
->toArray();
$this->data = json_encode($data);
$this->data = json_decode($this->data, true);
Output
View
<div class="panel-body">
#foreach ($sheet as $sheets)
{{$sheets->date_of_event}}
#endforeach
</div>
Error
"Trying to get property of non-object (View:
C:\xampp\htdocs\wallboards\resources\views\boards\viewexcel.blade.php)"
<div class="panel-body">
#foreach ($sheet as $sheets)
{{$sheets->date_of_event}}
#endforeach
</div>
In the above code you are trying to access an object {{$sheets->date_of_event}} but its an array. So try $sheets['date_of_event']
So the full code is
<div class="panel-body">
#foreach ($sheet as $sheets)
{{$sheets['date_of_event']}}
#endforeach
</div>
I have created 2 collections(arrays) which each contain an array of 5 items/events.
$events = App\get_event_data( $args )
$collection = collect($events['events']);
$event_chunks = $collection->chunk(5);
$event_chunks->toArray();
#foreach ($event_chunks as $chunk)
Output of the above:
object(Illuminate\Support\Collection)[27632]
protected 'items' =>
array (size=2)
0 =>
object(Illuminate\Support\Collection)[27630]
protected 'items' =>
array (size=5)
...
1 =>
object(Illuminate\Support\Collection)[27631]
protected 'items' =>
array (size=5)
...
In my next loop, it simply goes through every item of the array 1 by 1.
I need to split the 5 items into a further 2 groups:
group of 2
group of 3
so I can wrap a div around each group.
Current loop:
#foreach ($chunk as $key => $event)
<div class="item">
{{ $event['title'] }}
</div>
#endforeach
What I need:
<div class="item-group">
[item1, item2]
</div>
<div class="item-group">
[item3, item4, item5]
</div>
Full code:
{{-- Get all events --}}
#if( $events = App\get_event_data( $args ) )
#php
$collection = collect($events['events']);
$event_chunks = $collection->chunk(5);
$event_chunks->toArray();
#endphp
#foreach ($event_chunks as $chunk)
<div class="{{ $block }}__flex-grid">
#foreach ($chunk as $key => $event)
<div class="item">
{{ $event['title'] }}
</div>
#endforeach
</div>
#endforeach
#endif
Assuming that you will allways have 5 items, a solution could be:
<div class="item-group">
#foreach (array_slice($chunk->toArray(),0,2) as $key => $event)
<div class="item">
{{ $event['title'] }}
</div>
#endforeach
</div>
<div class="item-group">
#foreach (array_slice($chunk->toArray(),2) as $key => $event)
<div class="item">
{{ $event['title'] }}
</div>
#endforeach
</div>
Or if want to avoid code duplication:
#php $chunk = [array_slice($chunk->toArray(),0,2), array_slice($chunk,2)];
#foreach ($chunk as $group)
<div class="item-group">
#foreach ($group as $key => $event)
<div class="item">
{{ $event['title'] }}
</div>
#endforeach
</div>
#endforeach
If you don't know or not sure that you have 5, you may need to change the logic of the chunks/slice.
It's maybe too late to answer, but I've made a function to split arrays into as groups as you like.
function SplitInto($array, $groups = 2)
{
$members = count($array) / $groups;
if (count($array) % $groups) $members = (int)(count($array) / $groups) + 1;
return array_chunk($array, $members);
}
I can`t access the values from this variable $post from controller:
in blade view:
#foreach($post as $p)
<div class="col-md-3">
<div class="boxpromo">
<h4>{{ $p->title }}</h4>
<p>{{ $p->post_description }}</p>
<div class="price">
<div class="data">{{ $p->out_date }}</div>
<div class="cost">{{ $p->price }}</div>
</div>
</div>
</div>
#endforeach
In controller i have:
public function postSearchTransport(Request $request, Post $post){
...
// create a new query builder
$post = $post->newQuery();
// add some filters from the user in the search query
if($request->has('searchWord')){
$post->where('title', 'LIKE', '%'.$request->input('searchWord').'%');
}
...//more filters
$post->paginate(5);
return view('searchVehicle.showSearchPost')->with('post', $post);
}
What i did wrong? I should be able to access in the view all the values from the query result, but i can`t this way.
Should i convert that $post std object from controller into an array then return it to the view???
You must pass to the view the returned value of paginate method like this :
$posts = $post->paginate(5);
return view('searchVehicle.showSearchPost')->with('post', $posts);
I have this search engine with some advanced search options but I get errors when I use advance options.
When I go to the next pages I see errors.
Here is my code:
Controller:
$Input = Input::all();
$makethis = Input::flash();
$items = Gamefarm::where('roost_hen', '=', Input::get('sex'))
->paginate(6);
return View::make('gamefarms/index', compact('items', 'makethis'));
For my views
#foreach(array_chunk($items->all(), 3) as $row)
<div class="row">
#foreach ($row as $item)
<div class="col-md-4">
<h2>{{ $item->bname}}</h2>
<img src="{{$item->img_loc}}">
<div>{{$item->desc}}</div>
</div>
#endforeach
</div>
#endforeach
{{ $items->appends(Request::except('page'))->links() }}
return View::make('gamefarms/index',compact('items','makethis'))->with('items',$items);