Unexpected data \r\n with Carbon laravel - laravel

I'm trying to get all of the students from a table using "Student::all()". I don't even have carbon in this function. It's not getting students by date or anything. When i try to run the function, I get the error shown in the picture. It's pointing to a carbon file in the vendor folder but I don't know what to change. The data from the database pulls fine in every other function of the program, just not this one exact thing. When I die/dump my function call,I get the students list just fine. Something is simply irritating Carbon for no apparent reason. If I use a carbon parameter, it works, the function doesn't stall, but it does not return the correct data even though the field in the database holds that info. Why would a line break be causing this issue?
This function does not produce the error, but it does not result in the correct data.
public function exportRoster(){
$students = Student::whereDate('updated_at', Carbon::today())->get();
$pdf = PDF::loadView('printRoster', compact('students'), [], ['orientation' => 'L']);
return $pdf->stream('roster'.Carbon::today().'.pdf');
}
This function would obviously have the right data, but it produces the error of "Unexpected data".
public function exportRoster(){
$students = Student::all();
$pdf = PDF::loadView('printRoster', compact('students'), [], ['orientation' => 'L']);
return $pdf->stream('roster.pdf');
}
Just in case it helps, i've also included a screen shot of my database
Here is the table i'm using with my students variable in the printRoster view. If I remove this table, all queries work.
<table style="width:100%;">
<tr>
<th align = "left">Student Name</th>
<th align = "left">Student ID</th>
<th align = "left">District</th>
<th align = "left">School</th>
<th align = "left">Test Date/Time</th>
<th align = "left">Vision</th>
<th align = "left">Color</th>
<th>Nurse</th>
</tr>
#foreach($students as $student)
#php
$grade = array('20/40', '20/50', '20/60', '20/80', '20/100', '20/200', '20/400');
#endphp
<tr>
<td>{{$student->fname.' '.$student->lname}}</td>
<td>{{$student->student_number}}</td>
<td>{{$student->district}}</td>
<td>{{$student->school}}</td>
<td>{{$student->updated_at}}</td>
#if(in_array($student->od_dist, $grade) || in_array($student->os_dist, $grade) || in_array($student->ou_dist, $grade))
<td>Failed</td>
#else
<td>
Passed
</td>
#endif
<td>{{$student->ou_color}}</td>
<td>{{$student->nurse}}</td>
</tr>
#endforeach
</table>

The updated_at column is a timestamp so maybe you need to convert it in a raw date in order to show it. Please try if it works
#foreach($students as $student)
#php
$grade = array('20/40', '20/50', '20/60', '20/80', '20/100', '20/200', '20/400');
#endphp
<tr>
<td>{{$student->fname.' '.$student->lname}}</td>
<td>{{$student->student_number}}</td>
<td>{{$student->district}}</td>
<td>{{$student->school}}</td>
<td>{{date('Y-m-d H:i:s', strtotime($student->updated_at))}}</td>
#if(in_array($student->od_dist, $grade) || in_array($student->os_dist, $grade) || in_array($student->ou_dist, $grade))
<td>Failed</td>
#else
<td>
Passed
</td>
#endif
<td>{{$student->ou_color}}</td>
<td>{{$student->nurse}}</td>
</tr>
#endforeach
Edit
public function exportRoster(){
$students = Student::all();
$pdf = PDF::loadView('printRoster', $students);
$pdf->setPaper('A4', 'landscape');
return $pdf->stream('roster.pdf');
}

Related

how to display search result in laravel

i can not show search result using laravel. below is the code for route, controller and views:
route:
Route::resource('search', 'SearchController');
controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Order;
use Redirect;
use Auth;
use DB;
class SearchController extends Controller
{
public function index(Request $request)
{
$order = Order::all();
return view(search.index', compact(search));
}
public function search(Request $request)
{
$search = $request->search;
$order = DB::table('order')
->where('custName','like',"%".$search."%");
return view('search.index',compact('search'));
}
}
view
<form action="search" method="GET">
<input type="text" name="custName" class="form-control" <br>
<button type="submit" class="btn btn-primary">SEARCH</button>
</div>
</div>
<table class="table table-bordered" width="500">
<tr>
<td><font color="white">RESULT :</font></td>
</tr>
#foreach($order as $order)
<tr>
<td>{{ $order->custName }}</td>
<td>{{ $order->orderStatus }}</td>
</tr>
#endforeach
</table>
directory location
c:/xampp/htdocs/web/resources/views/search/index.blade.php
show error
Undefined variable: order (View: c:\xampp\htdocs\web\resources\views\search\index.blade.php)
above is my code to search record in the database. but the result always undefined variable.
how to display the searched result using code above. thank you
In your Controller search method, assign search result to $orders
$orders = DB::table('order')
->where('custName','like',"%".$search."%");
then send it to the view:
return view('search.index',compact('orders'));
and in the view do this:
#foreach($orders as $order)
<tr>
<td>{{ $order->custName }}</td>
<td>{{ $order->orderStatus }}</td>
</tr>
#endforeach
It will solve your problem.
There are some issues with the code you have shared, I try to tell the ones I see.
there is a misspelling in Route::resource where instead of search you coded seearch
as for search method it is not used anywhere by the route you provided. You can see more details on Route::resource for methods and views at this question
now considering that you are loading index.blade.php from the public function index method, the usage of compact is troubled. At this page, you can see how compact and with are different and how to use it. The main problem here is that you are not actually passing the order to your view at all.
The last but not least, it is good practice to name items having a collection of values plurally. For example here the order should change to orders. This would prevent the misuse of it in foreach. In your foreach, the name of the iterating array and the temp variable keeping the data should be different, or otherwise, they would have conflict. Here you have 2 variables called order in the same scope.
for instance i have records :
a
aa
aaa
b
c
d
e
the code show all record no1 until no7.
but now its solved i fixed the code in my controller in index() section as below
public function index()
{
return view('search.index', compact('order'));
}
public function search(Request $request)
{
$search = $request->search;
$order = DB::table('order')->where('custName', 'like', "%".$search."%")->get();
return view('search.result', compact('order'));
}

Show function at controller not getting value from index

I really get here to bother you only when I can't find an answer.
I don't know Why I get a null at show function at productoservicioevento. It's the same code that I use at grupo and there I get the id...
index at module productoservicioevento
#foreach($productoservicioeventos as $productoservicioevento)
<tr>
<td> {{ $productoservicioevento->idProductoServicioEvento }}></td>
<!--<td> {{ $productoservicioevento->strConcepto }}></td>-->
<td>{{ $productoservicioevento->dcmPrecio }}</td>
<td>{{ $productoservicioevento->evento->strNombreEvento }}</td>
</tr>
#endforeach
show function at productoservicioeventocontroller
public function show(ProductoServicioEvento $productoServicioEvento)
{
//
//$productoServicioEvento = ProductoServicioEvento::all();
dd($productoServicioEvento);
dd($productoServicioEvento->idProductoServicioEvento);
$productoServicioEvento = ProductoServicioEvento::find($productoServicioEvento->idProductoServicioEvento);
I get null when click at index
The module from productoservicioevento was actually copied from
index at grupo
#foreach($productoservicioeventos as $productoservicioevento)
<tr>
<td> {{ $productoservicioevento->idProductoServicioEvento }}></td>
<!--<td> {{ $productoservicioevento->strConcepto }}></td>-->
<td>{{ $productoservicioevento->dcmPrecio }}</td>
<td>{{ $productoservicioevento->evento->strNombreEvento }}</td>
</tr>
#endforeach
Everything is Ok at gruposcontroller, show function
public function show(grupo $grupo)
{
//
dd($grupo);
$grupo = grupo::find($grupo->idGrupo);
Everything goes fine there...
Te routes, Totally basic.
Route::resource('lockers','LockersController');
Route::resource('escuelas','EscuelasController');
Route::resource('grupos','GruposController');
Route::resource('productoservicioeventos','ProductoServicioEventosController');
Route::resource('horarioperiodicos','HorarioPeriodicosController');
Route::resource('eventos','EventosController');
I changed this.
public function show(ProductoServicioEvento $productoServicioEvento)
{
//
//$productoServicioEvento = ProductoServicioEvento::all();
dd($productoServicioEvento);
dd($productoServicioEvento->idProductoServicioEvento);
$productoServicioEvento = ProductoServicioEvento::find($productoServicioEvento->idProductoServicioEvento);
For this
public function show( $producto)
{
//
//dd($producto);
/*dd($productoServicioEvento->idProductoServicioEvento);*/
$productoServicioEvento = null;
$productoServicioEvento = ProductoServicioEvento::find($producto);
Now I'm able to work but don't know the reason why I had to change model to id...
So I'm totally stuck, please help.
Thanks in advance.

ErrorException Undefined property stdClass::$Start

I ran into an error while fetching data from my goals table. I'm trying to display all the tables data using foreach loop. But I can't due to unknown property error. I'm still learning laravel so kindly help.
Here's my controller code.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\goal;
use DB;
class GoalController extends Controller
{
public function postAddGoal(Request $request){
$this->validate($request, [
'goal'=>'required',
'checklist'=>'required'
]);
$goal_name=$request['goal'];
$checklist=$request['checklist'];
$goal = new goal();
$goal->goal=$goal_name;
$goal->checklist=$checklist;
$goal->status='PENDING';
$goal->start='1002-1-1';
$goal->finish='1002-1-1';
$goal->remarks="NULL";
$goal->score="0";
$goal->save();
redirect()->route('seegoals');
}
public function getGoals(){
$goals = DB::select(' select * from goals ');
if(empty($goals)){
$goals=0;
}
return view('/index', ['goals'=>$goals] );
}
}
Then my index.blade.php code
#foreach($goals as $goal)
<tr>
<th colspan="3">{{ $goal->goal }}</th>
<th colspan="7">{{$goal->checklist}}</th>
<th colspan="2">{{$goal->status}}</th>
<th colspan="2">{{$goal->Start}}</th>
<th colspan="2">{{$goal->finish}}</th>
<th colspan="2">{{$goal->score}}</th>
<th colspan="7">{{$goal->remarks}}</th>
</tr>
#endforeach
I get an exception:
(2/2) ErrorException Undefined property: stdClass::$Start (View:
/opt/lampp/htdocs/ToDo/resources/views/index.blade.php)
What could be the problem with my code?
There is misleading of first letter. Change Start to start
<th colspan="2">{{$goal->start}}</th>
And for good practice in controller change code to
$goals = DB::select('select `checklist`, `status`, `start`, `finish`, `score`, `remarks` from goals');
You must be get in db only needed info
also delete this part
if (empty($goals)) {
$goals = 0;
}
For not get error when goals is empty

Laravel 5.1 - compare Carbon dates

Here I need to check does auction for article in my app is live so I write in Article model:
public function scopeCloseauction($query){
$query->where(Carbon::parse('to')->subDays('auction_end'),'>',Carbon::now());
}
and view I have:
#if ($article->Closeauction())
<td>
auction is live
</td>
#else
<td>
auction is closed
</td>
#endif
but I have a problem becouse I got error:
UPDATE:
I also try:
in model to add function:
public function isLive($to,$auction_end) {
$first = Carbon::create($to).subDays($auction_end);
$second = Carbon::now();
return ($first->lte($second));
}
and in view:
#if ($article->isLive($article->to,$article->auction_end))
<td>
live
</td>
#else
<td>
closed
</td>
#endif
but now give me this error:
ErrorException in Carbon.php line 425: Unexpected data found.
Unexpected data found. Unexpected data found. Trailing data (View:
C:\wamp\www\project\resources\views\articles\index.blade.php)
You can do something add such function into your Article model:
public function isLive()
{
$first = Carbon::parse($this->to)->subDays($this->auction_end);
$second = Carbon::now();
return $first->lte($second);
}
and now in your view you can use:
#if ($article->isLive())
<td>
live
</td>
#else
<td>
closed
</td>
#endif
I think your problem is here : Carbon::parse('to'). What do you want to do here ? the Carbon::parse method try to convert a string date into a Carbon date. See the doc. I dont think that Carbon can parse the string 'to'. If you want to check the diff between dates, you should have a look to the appropriated methods like diffInDays.

is forelse remove in laravel 4.2.4?

I was trying to use the #forelse in laravel but it give me this error
Undefined variable: data
is #forelse remove from laravel 4.2.4? because that's the version that i am using. This is my code
in my view
#forelse ($result as $data)
<tr><td> $data->name </td></tr>
#empty
<tr><td>No name match</td></tr>
#endforelse
in my controller
$result = User::find(1)->get();
return View::make('view')->with('result', $result);
I don't think #forelse has been around since Laravel 3. I could be wrong. I know it was removed at some point though. You need to use a standard #if and #foreach now.
#if (empty($result))
<tr><td>No name match</td></tr>
#else
#foreach ($result as $data)
<tr><td> $data->name </td></tr>
#endforeach
#endif
UPDATE
As pointed out by Antonio it was brought back and is available in v4.2.7+, so you'll need to update if you want it.
I found the same thing. In the laracast video on Laravel 5 it is mentioned. Although right after he explains that a better practice is using the if else with the count function:
#if (count($result))
has result
#ifelse
has no result
#endif

Resources