Database sql server with results but view is empty - php

something really strange is happening. So I have a table named Lactinfo_News with 1 row. I've created also a view called LACTINFO_VW_LatestNews which have "SELECT * FROM Lactinfo_News" and is returning the same 1 row.
I'm using Eloquent and In my news Manager I have,
public function GetLatestNews($rowsPerPage) {
$list = DB::table('LACTINFO_VW_LatestNews')
->orderBy('RegistedDate', 'DESC')
->paginate($rowsPerPage);
return $list;
}
where $rowsPerPage = 30.
In my controller I have,
// >> current page
$page = '1';
if (! empty ( $request->query ( 'page' ) )) {
$page = $request->query ( 'page' );
}
// >> search
$nM = new NewsManager();
$list = $nM->GetLatestNews($page, $this->nbOfRowsPage);
return view ('admin.news.index', [
'results' => compact($list),
'page' => $page,
'startDate' => $startDate,
'endDate'=>$endDate
] );
}
And in my view,
<table id="news-results" class="hover responsive" style="margin-top: 20px;">
<thead>
<tr>
<th scope="column">Título</th>
<th scope="column">Descrição</th>
<th scope="column">Data Início</th>
<th scope="column">Ficheiro</th>
<th scope="column">Registada em</th>
<th scope="column">Criada por</th>
</tr>
</thead>
<tbody>
#if (count($results) > 0)
#foreach ($results as $r)
<tr>
<td>{{ $r->title }}</td>
<td>{{ $r->description }}</td>
<td>{{ Carbon\Carbon::parse($r->startDate)->format('d/m/Y') }}</td>
<td>{{ $r->fileURL }}</td>
<td>{{ $r->registedDate }}</td>
<td>{{ $r->createdBy }}</td>
</tr>
#endforeach
#else
<tr><td colspan="11">Não existem notícias criadas</td></tr>
#endif
</tbody>
</table>
If I dump the $results var is empty..
What's happening? This is really strange :S I don't think I need to execute any artisan command because I've add a new function to my manager but I'm stuck with this problem for a few days...

You are using compact on an object of type LengthAwarePaginator, which is kind of weird IMHO.
Why don't you pass $list in your view?

My problem was in the Controller and in my View,
So I've changed my controller return to
return view ('admin.news.index', [
'results' => $list,
'page' => $page,
'startDate' => $startDate,
'endDate'=>$endDate
] );
And in my view I wasnt calling in a wrong way my parameters...
<tbody>
#if (count($results) > 0)
#foreach ($results as $r)
<tr>
<td>{{ $r->Title }}</td>
<td>{{ $r->Description }}</td>
<td>{{ Carbon\Carbon::parse($r->StartDate)->format('d/m/Y') }}</td>
<td>{{ $r->fileURL }}</td>
<td>{{ Carbon\Carbon::parse($r->RegistedDate)->format('d/m/Y') }}</td>
<td>{{ $r->refCredential }}</td>
</tr>
#endforeach
#else
<tr><td colspan="11">Não existem notícias criadas</td></tr>
#endif
</tbody>

Related

Laravel view foreach with two properties

i need help with my foreach loop in view
Controller.php
$expenses = Expense::groupBy('category_id')->selectRaw('sum(amount) as sum, category_id')->pluck('sum','category_id');
$categories = Category::all();
return view('dashboard.index', compact('expenses','categories'));
index.php
<table class="table">
<thead>
<th>Category</th>
<th>Expenses</th>
</thead>
<tbody>
#foreach($categories as $category)
<tr>
<td>{{ $category->name }}</td>
#foreach($expenses as $expense)
<td>{{ $expense }}</td>
#endforeach
</tr>
#endforeach
</tbody>
</table>
I get the output like this.
enter image description here
It outputs the expense 2 times, is there a limiter for "foreach loop" or is there another way on doing this?
You need to add a condition for that $category->id is the same as category_id
#foreach($expenses as $key => $expense)
#if($key == $category->id)
<td>{{ $expense }}</td>
#endif
#endforeach
The right way to do it is by using join or relationship.
$expenses = Expense::join('categories','categories.category_id','expenses.category_id')->groupBy('expenses.category_id')->selectRaw('sum(expenses.amount) as sum, expenses.category_id','categories.name')->get();
<table class="table">
<thead>
<th>Category</th>
<th>Expenses</th>
</thead>
<tbody>
#foreach($expenses as $expense)
<th>{{ $expense->name }}</th>
<td>{{ $expense->sum }}</td>
#endforeach
</tbody>
</table>
You should limit the results in controller, but here's how you can accomplish this in a blade. Not pretty
#foreach ($element['subs'] as $item)
#if($loop->index < 10)
// Your code
#endif
#endforeach
You should compare category_id with category->id.
#foreach($categories as $category)
<tr>
<td>{{ $category->name }}</td>
#foreach($expenses as $expense)
#if($expense->category_id == $category->id )
<td>{{ $expense->sum }}</td>
#endif
#endforeach
</tr>
#endforeach

Accessing in arrays using Twig templating engine

I am able to grab {{ value.title }} which displays fine however getting the inner array value it is not showing and not sure what I'm doing wrong here. I have not used Twig before.
PHP
$product_array = array();
foreach ($shopifyProducts as $product) {
$item = array(
"title" => $product["title"], // product title
"variants" => array() // for now an empty array, we will fill it in the next step
);
foreach($product["variants"] as $variant) {
$item["variants"][] = array(
"barcode" => $variant["barcode"], // product barcode
"sku" => $variant["sku"] // product SKU
);
}
$product_array[] = $item; // now we add the item with all the variants to the array with all the products
}
TWIG
<div class="table-responsive">
<table class="table">
<thead>
<th>Title</th>
<th>Barcode</th>
<th>SKU</th>
</thead>
<tbody>
<tr ng-repeat="value in remote_data">
<td>{{ value.title }}</td>
<td>{{ value.variants.barcode }}</td>
<td>{{ value.variants.sku }}</td>
<td>{{ value.variants }}</td>
</tr>
</tbody>
</table>
</div>
If I output value.variants it outputs
[{"barcode":"5060315468600","sku":"PLU#1"}]
But I can't seem to get the barcode and sku to display.
The variants attribute is an array, you should cycle in it, as example:
{% for variant in value.variants %}
<td>{{ value.title }}</td>
<td>{{ variant.barcode }}</td>
<td>{{ variant.sku }}</td>
{% endfor %}
Here a working example with the sample data provided.
Probably, seen your code, is better you change the structure of your data (you need to display always the product's title) and simplify the twig code, as example I propose you the following:
PHP
$product_array = array();
foreach ($shopifyProducts as $product) {
foreach($product["variants"] as $variant) {
$product_array[] = array(
'title' => $product["title"], // always display the product title
"barcode" => $variant["barcode"], // product barcode
"sku" => $variant["sku"] // product SKU
);
}// end foreach
}// end foreach
TWIG
<div class="table-responsive">
<table class="table">
<thead>
<th>Title</th>
<th>Barcode</th>
<th>SKU</th>
</thead>
<tbody>
<tr ng-repeat="value in remote_data">
<td>{{ value.title }}</td>
<td>{{ value.barcode }}</td>
<td>{{ value.sku }}</td>
</tr>
</tbody>
</table>
</div>

Check if no records exist in view laravel

Okay so I've hit a wall for some reason and I can't figure this out. I wan't to check if there are any records in a query and then use an #if in my view to say either "yes, there are records" or "none". Nothing I've tried is working..
What I want:
<div class="col-md-12 col-style">
<h1 class="no-border">YOUR LATEST CONVERSIONS</h1>
#if(something here that lets me test if $transactions is empty)
<table class="table table-striped">
<tr>
<th>#</th>
<th>Type</th>
<th>User</th>
<th>Amount</th>
<th>Value</th>
<th>Result</th>
<th>Result Value</th>
<th>Date</th>
</tr>
#foreach($transactions as $transaction)
<tr>
<td>{{ $transaction->id }}</td>
<td>{{ $transaction->status }}</td>
<td>{{ $transaction->username }}</td>
<td>{{ $transaction->amount }}</td>
<td>{{ $transaction->value }}</td>
<td>{{ number_format($transaction->result, 2) }}</td>
<td>{{ $transaction->result_value }}</td>
<td>{{ $transaction->created_at }}</td>
</tr>
#endforeach
</table>
#else
yo no records
#endif
</div>
My controller:
public function index($username)
{
$user = User::where('username', $username)->firstOrFail();
$id = $user->id;
$transactions = Transactions::where('user_id', '=', $id)->take(5)->get();
return view('user.profile', compact('user', '=', 'userCurrency', 'transactions'));
So if you see in the top part there's a basic #if. Maybe I'm over thinking it, or am I passing in something that cant be checked the way I want it to?
Thanks!
#if (!$transactions->isEmpty())
...
#endif
this use laravel collection method do the trick.
it is as easy as:
#if(count($transactions) > 0)
#if($transactions) should do the trick.

Custom query using DB::select() on laravel 4.2 controller

I had used a custom query in my controller in laravel here is my code in my controller:
public function c_viewSystemUser()
{
$title = "View System Users";
$jSu = DB::select(DB::raw("SELECT dbo_systemusers.SystemUserID,dbo_systemtypes.SystemType, dbo_systemusers.SystemUserName INNER JOIN dbo_systemtypes ON dbo_systemusers.SystemUserTypeID = dbo_systemtypes.SystemUserTypeID"));
return View::make('ssims.view_systemUser',compact('title','jSu'));
}
I wanted to display its result in my blade file view_systemUser
here is my code inside the view:
#if ($jSu)
<table class="table table-striped table-hover" id="detailTable">
<thead>
<tr>
<th>System User ID</th>
<th>SystemUser Type ID</th>
<th>System UserName</th>
<th>System User Description</th>
</tr>
</thead>
<tbody>
#foreach ($jSu as $vu)
<tr>
<td>{{ $vu->dbo_systemusers.SystemUserID }}</td>
<td>{{ $vu->dbo_systemtypes.SystemType }}</td>
<td>{{ $vu->dbo_systemusers.SystemUserName}}</td>
<td>{{ $vu->dbo_systemusers.SystemUserDescription}}</td>
</tr>
#endforeach
</tbody>
</table>
And I am having an error:
Undefined property: stdClass::$dbo_systemusers (View:
C:\xampp\htdocs\laravel3\app\views\ssims\view_systemUser.blade.php)
Any suggestions?
Try this:
#foreach ($jSu as $key => $vu)
<tr>
<td>{{ $vu->SystemUserID }}</td>
<td>{{ $vu->SystemType }}</td>
<td>{{ $vu->SystemUserName}}</td>
<td>{{ $vu->SystemUserDescription}}</td>
</tr>
#endforeach
You forgot to join dbo_systemusers with dbo_systemtypes.
Try this code
SELECT dbo_systemusers.SystemUserID,dbo_systemtypes.SystemType, dbo_systemusers.SystemUserName from dbo_systemusers INNER JOIN dbo_systemtypes ON dbo_systemusers.SystemUserTypeID = dbo_systemtypes.SystemUserTypeID
And Try this on View page
#foreach ($jSu as $vu)
<tr>
<td>{{ $vu->SystemUserID }}</td>
<td>{{ $vu->SystemType }}</td>
<td>{{ $vu->SystemUserName}}</td>
<td>{{ $vu->SystemUserDescription}}</td>
</tr>
#endforeach

Select List From Database With Parameter

Hi there i have some problem getting list from database using laravel 4.1
I have session that stored some value about companyid. Then i want to print the list to table based on the companyid itself. Let say i can access the session using {{ Session::get('name')}}
This is my controller index()
public function index($cid) {
$pengguna = User::where('id_company', '=', $cid)->get();
$pengguna->toarray();
$data = array(
'pengguna' => $pengguna,
'page' => 'master',
'index' => 0
);
return View::make('console.pengguna')->with($data);
}
then i want to print the data to table in view
#if($pengguna->count())
<table class="table table-striped table-bordered" id="table_pengguna_list">
<thead>
<tr>
<th>No</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Username</th>
<th>Email</th>
<th>Status</th>
<th>Last Login</th>
<th class="td-actions">Action</th>
</tr>
</thead>
<tbody>
#foreach ($pengguna as $pgn)
<tr>
<td>{{ $index++ }}</td>
<td>{{ $pgn->firstname }}</td>
<td>{{ $pgn->lastname }}</td>
<td>{{ $pgn->username }}</td>
<td>{{ $pgn->email }}</td>
<td>{{ $pgn->level }}</td>
<td>{{ $pgn->updated_at }}</td>
</tr>
#endforeach
</tbody>
</table>
....
how to pass the parameter from session to the controller then? so basically pass the {{ Session::get('name')}} to $cid in index controller.
thank you.
hi there i have found the solution, just place refactor the index controller to;
public function index() {
$cid = Session::get('name');
$pengguna = User::where('id_company', '=', $cid)->get();
$pengguna->toarray();
$data = array(
'pengguna' => $pengguna,
'page' => 'master',
'index' => 0
);
return View::make('console.pengguna')->with($data);
}

Categories