Laravel Blade Foreach Output - php

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>

Related

Undefined variable $data

error:
Undefined variable $data
#foreach ($data as $d)
<div class="col-md-4 mb-3">
<div class="bg-welcome3">
<img src="{{ $d->image }}" alt="">
</div>
</div>
#endforeach
my route:
Route::resource('welcome', WelcomeController::class);
WelcomeController:
public function index()
{
$data = Product::all();
return view('welcome',compact('data'));
}
I want to display data in view('welcome') but the data is undefine, even though the routes and controllers that I use are correct
try this instead Check and make sure that welcome.blade.php is present in inside [yourprojectname]\resources\views

Laravel multidimensional echo values?

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.
}

can`t access laravel Query Builder result from view

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);

Trying to get property of non object Laravel 5.2

The error occurs when i print the messages for the logged in user sent by others to him. I did var_dump in my controller as well as in my view. I also did {{$threads->count()}} in my view and it showed 27(thread count). But when i access the view file it throws me the above mentioned error.
Here is my controller code
public function index()
{
$currentUserId = Auth::user()->id;
$threads = Thread::getAllLatest()->get();
return view('chatindex', compact('threads', 'currentUserId'));
}
I also printed the currentUserId in view and it also works fine
Here is my view code (where the error occurs)
#extends('layouts.app')
#section('content')
#if (Session::has('error_message'))
<div class="alert alert-danger" role="alert">
{!! Session::get('error_message') !!}
</div>
#endif
#if($threads->count() > 0)
#foreach($threads as $thread)
<?php $class = $thread->isUnread($currentUserId) ? 'alert-info' : ''; ?>
<div class="media alert {!!$class!!}">
<h4 class="media-heading">{!! link_to('messages/' . $thread->id, $thread->subject) !!}</h4>
<p>{!! $thread->latestMessage->body !!}</p>
<p><small><strong>Creator:</strong> {!! $thread->creator()->name !!}</small></p>
<p><small><strong>Participants:</strong> {!! $thread->participantsString(Auth::id()) !!}</small></p>
</div>
#endforeach
#else
<p>Sorry, no threads.</p>
#endif
#stop
It throws me the error Trying to get property of non-object (View: C:\xampp\htdocs\laravel-projects\user_prof\resources\views\chatindex.blade.php)
maybe in your foreach $thread is an array() so use it like one as
$thread['id'], $thread['subject']

Laravel pagination Links not working on queries

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);

Categories