I have nested looping view on laravel and i am having problem when i try to show the list of my items.
Here is the code where i have the error
#foreach($project->codes->items as $item)
<tr>
<td>{!! $item->item !!}</td>
</tr>
#endforeach
and it produces this error.
Cannot access protected property Illuminate\Database\Eloquent\Collection::$items
I have setup the models for project, code and items as.
Project Model
class Project extends Model
{
public $table = 'projects';
public function codes() {
return $this->hasMany('App\Models\Code');
}
}
Code Model
public function code() {
return $this->belongsTo('App\Models\Project');
}
public function items() {
return $this->hasMany('App\Models\Item');
}
item model
public function item() {
return $this->belongsTo('App\Models\Code');
}
And here's how i implement it on the controller
public function index(Request $request)
{
$this->projectRepository->pushCriteria(new RequestCriteria($request));
$projects = $this->projectRepository->with('codes.items')->all();
return view('projects.index')
->with('projects', $projects);
}
I have run the code on artisan tinker and it shows the correct data. But not loading on the output php.
Thank You for your support.
Please share the output
i think it should be like this
#foreach($project->codes as $code)
#foreach($code->items as $it)
<tr>
<td>{!! $it->item !!}</td>
</tr>
#endforeach
#endforeach
Related
i have a function that displays tasks of current user
public function test(Request $request)
{
$user= auth()->user();
return view('welcome')->with('tasks',$user->tasks);
}
This is the code that displays the posts in home view
#foreach($tasks as $task)
<tr>
<td class="table-text">
<div>{{$task->item}}</div>
</td>
</tr>
#endforeach
Am getting the error above
relationship in user model
public function tasks(){
return $this->HasMany('App\Task');
}
I think you need to fix your code lower case h in the hasMany() function
It should be
public function tasks(){
return $this->hasMany('App\Task');
}
When calling a Controller function, in a Laravel 5.6 blade/view, I get the following error message.
Method Illuminate\Database\Query\Builder::show does not exist
I can't find where the error or the source of this error is. I read dozens of posts here with the same error, but none of them were related to code/context/type of models/controllers(...) that I have.
Blade view where the error message is shown.
Blade
#foreach (\App\Portfolio::show() as $port)
<option value="{{$port->id_portfolio}}">{{$port->name}}</option>
#endforeach
Controller
class PortfolioController extends Controller
{
public function show()
{
$portfolio = \App\Portfolio::where([
['flg_active', '=', true],
['id_user', '=', Auth::user()->id]
])->get();
return $portfolio;
}
}
Model
class Portfolio extends Model
{
public function transaction()
{
return $this->hasMany('App\Transaction', 'id_portfolio', 'id_portfolio');
}
public function user()
{
return $this->belongsTo('App\User', 'id_user', 'id');
}
}
Test with all() instead of show(), and it works.
In Blade you call the model.
And the method you need is in the controller PortfolioController
PortfolioController
public static function show()
Blade
#foreach (\App\PortfolioController ::show() as $port)
<option value="{{$port->id_portfolio}}">{{$port->name}}</option>
#endforeach
I have three tables viz:
Site:
id|name|slug|location|description
Accounthead:
id|accountname|slug
Transaction:
id|name|slug|site_id|accounthead_id|...
My Site model looks like:
class Site extends Model
{
protected $fillable = [
'name',
'slug',
'location',
'description'
];
public function transactions()
{
return $this->hasMany('App\Transaction', 'site_id');
}
}
My AccountHead Model Looks like:
class AccountHead extends Model
{
protected $fillable = [
'slug',
'accountname'
];
public function transactions()
{
return $this->hasMany('App\Transaction','accounthead_id');
}
}
And my Transaction model looks like:
class Transaction extends Model
{
public function site()
{
return $this->belongsTo('App\Site','id');
}
public function accounthead()
{
return $this->belongsTo('App\AccountHead','id');
}
}
In One of my blade I want to display all the transactions and the associated fields:
My blade File
#forelse($transactions as $key => $transaction)
<tr>
<td>{{++$key}}</td>
<td>{{$transaction->updated_at->format('M d Y')}}</td>
<td>{{str_limit($transaction->name, 47) }}</td>
<td>{{str_limit($transaction->accounthead->accountname,47)}}</td>
<td>{{str_limit($transaction->site->Name,47)}}</td>
<td>{{str_limit($transaction->amount,47)}}</td>
</tr>
#empty
<tr>
<td colspan="4" class="text-center">No Transactions available.</td>
</tr>
#endforelse
It is working fine, unless one Site/AccountHead is used for more than one Transaction. Once a Site/Accounthead is used for more than one transaction Its throwing:
(2/2) ErrorException
Trying to get property of non-object
Am I missing anything really stupid here?
The problem is typo in your code. Instead of:
str_limit($transaction->accountheads->accountname,47)
you should have:
str_limit($transaction->accounthead->accountname,47)
because that's the name of your relationship.
Also it might happen later that you don't have accounthead for some relationship - in such case take a look at Laravel 5 get data other table
First of all, you should fix the relationships:
public function site()
{
return $this->belongsTo('App\Site', 'site_id', 'id');
}
public function accounthead()
{
return $this->belongsTo('App\AccountHead', 'accounthead_id' 'id');
}
Or just:
public function site()
{
return $this->belongsTo('App\Site');
}
public function accounthead()
{
return $this->belongsTo('App\AccountHead');
}
After that, if it still doesn't work, you need to make sure every transaction has both 'site' and 'accounthead' relationships. In other words, every row in the transactions table should have a correct ID in site_id and column_id columns.
If not every transaction has site or account head, you need to check it before trying to use related object property:
<td>{{ str_limit(optional($transaction->accounthead)->accountname, 47) }}</td>
<td>{{ str_limit(optional($transaction->site)->Name, 47) }}</td>
I'm trying to get a relationship between models in a laravel template.
I want to get the product fields respective to an ad.
This is my AdController:
public function ads(Request $request)
{
$this->adRepository->pushCriteria(new RequestCriteria($request));
$hash = Auth::user()->environment_hash;
$ads = $this->adRepository->findByHash($hash);
return view('ads.index')
->with('ads', $ads);
}
This how I'm parsing to my blade template:
#foreach($ads as $ad)
<tr>
<td>{!! $ad->product->name !!}</td>
</tr>
#endforeach
But I keep getting this error:
Trying to get property of non-object
This is the adRepository file:
public function findByHash($hash = null)
{
$model = $this->model;
if($hash)
$this->model->where('environment_hash', $hash);
else
$this->model->where('environment_hash', Auth::user()->environment_hash);
return $model;
}
This my Ad Model: Ad.php
<?php
namespace App\Models;
use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Ad extends Model
{
public function product()
{
return $this->belongsTo(Product::class);
}
}
This is my ad table:
id
product_id
environment_hash
And my product table:
id
environment_hash
name
What is wrong with the code?
Not sure but there must be some ad which has no product in your database. So this may be the reason you are getting error. Make sure that all of your ad has related product in database.
You can test it by running following code:
#foreach($ads as $ad) <tr> <td>Test {!! $ad->product !!}</td> </tr> #endforeach
If you get result like this:
Test some_name
Test
Test some_another_name
Then this is confirm that some ad has no related product.
If this is the case then you need to if() check before accessing the attributes of product.
Update
The problem is that you are missing get() in your findByHash().
It should be as:
public function findByHash($hash = null)
{
$model = $this->model;
if($hash)
$this->model->where('environment_hash', $hash);
else
$this->model->where('environment_hash', Auth::user()->environment_hash);
return $model->get();
}
I have this problem. In Laravel, I have 3 Models, ie:
class Department{
public function coordinations(){
return $this->has_many('Coordination');
}
}
class Coordination{
public function sites(){
return $this->has_many('Site');
}
}
class Site{
public function assets(){
return $this->has_many('FAssets');
}
}
class FAsset{}
I want to display all the asset grouped by Departments. I tryied to do something like:
#foreach($dependencias as $dep)
<tr>
<td colspan="8" style="width:100%">Dependencia: {{ $dep->code }} {{ $dep->description }}</td>
</tr>
#foreach($dep->coordinations()->sites()->assets()->get() as $asset)
...
But I got the "Method [sites] is not defined on the Query class" error. I was looking for the eager loading way but I dont see how it could work with an extra level on the relationships.
Also, I was considering to querying all the assets and filtering the assets given the actual coordination, but I think that this is going to take a lot of time to load...
Thanks in advance
class Deparment{
public function department_assets(){
$assets = array();
foreach($this->has_many('Coordination')->get() as $coordination){
foreach($coordination->sites()->get() as $site){
foreach($site->assets() as $asset)
{
$assets[] = $asset;
}
}
}
return $assets;
}
}
Now we can...
#foreach($dep->department_assets() as $asset)