I am trying to return a specific value in my Laravel view. I define a variable in my controller and pass it on to my view. This is what I get when my blade code looks like this:
<h1>{{ $sport }}</h1>
However, since this is returned in my view and I only want to have the "sport" itself I did this:
<h1>{{ $sport->sport }}</h1>
Undefined property: Illuminate\Database\Eloquent\Collection::$sport
Any help would be much appreciated.
Thanks
You are referencing an Array Collection not an object itself
Try
<h1>{{ $sport->first()->sport }}</h1>
Or modify your controller like this
// Controller
return View::make('sports', array('user' => $sport->first()));
// View
{{ $sport->sport }}
Related
I have created a relationship between products and users using a foreign key called "previous_owner". I can see when I dump the value that I'm passing to the view that the data I'm after is available under:
$product->relations->product_owner->user->attributes->name
But how do I access it from within my view. I'm trying to loop through the products and then do something like:
{{ $product->product_owner->user->name }}
or
{{ $product->product_owner->name }}
But non of it works, keep getting:
" Trying to get property 'name' of non-object "
In your view code must be similar this
#foreach($products as $product)
{{ $product->product_owner->name }}
#endforeach
This code mean each product has product_owner. When one product does not have product_owner that case value of $product->product_owner is Null and then your code is null->name and you get error. For fix it you must be check relation is null or use ?? operator. Change your code to
#foreach($products as $product)
{{ $product->product_owner->name ?? ''}}
#endforeach
or
#foreach($products as $product)
#if($product->product_owner)
{{ $product->product_owner->name}}
#endif
#endforeach
If name is attribute and product_owner is relationship function then try the below:
{{ $product->product_owner()->name }}
Your product_owner isn't stdClass. It is array. So you can't access this key of this array with "->" operator. try
{{ $product->product_owner['name'] }}
I've an error like Trying to get property of non-object, I use var_dump and realize that I've array not object but idk how to access
public function ptkp($id){
$halaman="tindaklayanan";
$keluhan = keluhan::findOrFail($id);
$tindak = DB::table('tindakans')
->join('keluhans','keluhans.id','=','tindakans.id_keluhan')
->select(DB::raw('tindakans.id, id_keluhan, perbaikan_sementara, revisi_dokumen, target_verifikasi, ttd_tanggung1,
ttd_tanggung2'))->get();
$analisa = DB::table('analisas')
->join('tindakans','tindakans.id','=','analisas.id_tindakan')
->join('keluhans','keluhans.id','=','tindakans.id_keluhan')
->select(DB::raw('id_tindakan, analisa, tindakan, pic, tanggal_pelaksanaan'))->get();
return view('Laporan.ptkp',compact('keluhan','tindak','analisa','halaman'));
//$pdf = \PDF::loadView('laporan.ptkp', compact('keluhan','tindak','analisa','halaman'));
//return $pdf->stream();
}
Look at $tindak when I use var_dump the result is array, in View I try to access using <?php echo $tindak->perbaikan_sementara ?> but error.
Since you are in Laravel, I assume you use a blade templating engine, so you can try this in the view to access property of object :
{{ $tindak->perbaikan_sementara }}
Or if it's an array of objects :
#foreach ($tindak as $example)
{{ $example->perbaikan_sementara }}
#endforeach
if you really have an array it's in your Laporan.ptkp (better use laporan.blade.php instead)
#foreach ($tindak as $item)
{{ $item['perbaikan_sementara'] }}
#endforeach
I am trying to make foreach loop in laravel to list all users but when I use the code that laravel suggested I get this error:
Undefined variable: users (View: /home/vagrant/Code/SimFly/resources/views/profile.blade.php)
The code causing the error is:
#foreach ($users as $user)
<p>This is user {{ $user->id }}</p>
#endforeach
Please can someone help me.
To use users variable in a view you need to pass it from a controller first:
$users = User::all();
return view('profile', compact('users'));
Its also a great practice to check state (isset, count for example) of the array/variable before looping through it, to protect any unforeseen errors.
As per the suggestions above, the problem is that the view was not being passed the variable.
To catch this in the blade template use something like a #forelse instead of a #foreach
ProfileController
$users = User::all();
return view('profile', [
'users' => $users
]);
Profile Blade
#forelse
<p>This is user {{ $user->id }}</p>
#empty
<p>There are no users</p>
#endforelse
I have Post model which has user function returning User model - creator of the post.
class Post extends Model
{
/**
* Get the user that owns the post.
*/
public function user()
{
return $this->belongsTo(User::class);
}
}
In blade view I achieved how to print author name
{{ $post->user->name or 'Anonymous' }}
Above code works, but it is very hmm sensitive?
For example, I tried change this code to:
{{ $post->user->name, 'Anonymous' }}
<?php $post->user->name or 'Anonymous' ?>
Results? Trying to get property of non-object error on line with this code.
Maybe I am skipping something simple but important. How to print URL or default value in blade view. Pseudo code what I mean:
{{ '' or 'Anonymous' }}
Try
{{ isset($post->user->name) ? '' : 'Anonymous' }}
If this doesn't work (I haven't check it) try something like this:
#if (isset($post->user->name))
#else
Anonymous
#endif
Actually the error is not because the code is sensitive, it is because you are actually trying to access non objects values.
To achieve what you are looking for:
{{ isset($post->user)?'' : 'Anonymous' }}
In my routes I have this route defined as:
// app/routes.php
Route::resource('CharacterController');
The corresponding method in the controller is:
// app/controllers/CharacterController.php
public function index(){
$characters = Character::all();
$houses = House::all();
return View::make('characters.index')->with(array('characters'=>$characters, 'houses' => $houses));
}
Finally, in the view:
// app/views/characters/index.blade.php
#this fires an error:
{{ $houses[$characters->house_id]->name }}
# at the same time this gives correct result:
{{ $houses[1]->name }}
# and this IS equal to 1:
{{ $characters->house_id }}
You can't use the id as index of the array to access the object with given id.
Since you have an Eloquent Collection you can use its various functions. One of them being find() for retrieving one item by id
{{ $houses->find($characters->house_id)->name }}