Laravel - Call to a member function get() on a non-object - php

#foreach ($wishItems as $wishItem)
<?php $products = Product::find($wishItem->product_id)->get(); ?>
#foreach($products as $product)
#if($product->id == $wishItem->product_id)
Name: {{ $product->title }} <br>
Price: {{ $product->price }} €<br>
<hr>
#endif
#endforeach
#endforeach
$wishItems is var that is sent to this view and it is ok... $products = Product::find($wishItem->product_id)->get(); here I have problem.. I got error: Call to a member function get() on a non-object .... I tried to replace $wishItem->product_id with number for example 3 [it exists in DB] but again same...

I assume what you really want is:
#foreach ($wishItems as $wishItem)
<?php $product = Product::find($wishItem->product_id); ?>
Name: {{ $product->title }} <br>
Price: {{ $product->price }} €<br>
<hr>
#endforeach
Your $wishItem really just refer to a single product, thus eliminating the need for you to query a collection, and do a loop.

Assuming you have a WishItem and Product model
class WishItem extends Eloquent {
public function product()
{
return $this->hasOne('Product');
}
}
In your view
$product = $wishItem->product;
More info:
http://laravel.com/docs/eloquent#basic-usage

Related

Undefined property: Illuminate\Database\Eloquent\Relations\BelongsToMany::$balance

I have a Many to Many relationship between Wallet & User Models:
User.php:
public function wallets()
{
return $this->belongsToMany(Wallet::class,'user_wallet','user_id','wallet_id')->withPivot('balance');
}
Wallet.php:
public function users()
{
return $this->belongsToMany(User::class,'user_wallet','wallet_id','user_id')->withPivot('balance');
}
And the pivot table user_wallet goes like this:
Now I need to get the name of wallets and the balance of that wallet in a table at Blade.
So at the Controller, I added this:
public function index($id)
{
// $id is the user id
$userWallet = Wallet::with("users")->whereHas('users',function ($query)use($id){
$query->where('user_id',$id);
})->get();
}
And then at the Blade:
#forelse($userWallet as $wallet)
<div class="message_fullName">
{{ $wallet->name }}
</div>
<div class="message_tag">
{{ $wallet->users()->balance }}
</div>
#empty
Empty
#endforelse
But I get this error:
Undefined property: Illuminate\Database\Eloquent\Relations\BelongsToMany::$balance
So what's going wrong here? How can I properly get the balance at the pivot table?
You have to loop user
#forelse($userWallet as $wallet)
<div class="message_fullName">
{{ $wallet->name }}
</div>
<div class="message_tag">
#if(isset($wallet->users)&&count((array)$wallet->users))
#foreach($wallet->users as $user)
{{$user->pivot->balance }}
#endforeach
#endif
</div>
#empty
Empty
#endforelse
Your query should be
$userWallet = Wallet::with(["users"=>function ($query)use($id){
$query->where('user_id',$id);
}])->whereHas('users',function ($query)use($id){
$query->where('user_id',$id);
})->get();
That's because you use the relation's query builder object with users() but you need user property to get the collection:
#forelse($userWallet as $wallet)
<div class="message_fullName">
{{ $wallet->name }}
</div>
<div class="message_tag">
{{ $wallet->users->balance }}
</div>
#empty
Empty
#endforelse
But even the collection instance doesn't have a balance property as long as it is not a custom collection. You probably need to loop over the collection or use pluck to get the model's balance property:
#forelse($userWallet as $wallet)
<div class="message_fullName">
{{ $wallet->name }}
</div>
<div class="message_tag">
{{ $wallet->users->pluck('balance')->join(', ') }}
</div>
#empty
Empty
#endforelse

How to add two numbers in the blade Laravel

I need to add two numbers/amount displayed in my blade view, as shown in the image below
I need to display as 666.68 / 1000 AUD
Here's my blade codes
<td>
#foreach ($team->competitionPayments as $payment)
{{ $payment->amount }}
#endforeach
<br>
{{ $team->competitionPayments }}
/
#foreach ($team->competitions as $total)
{{ $total->pivot->total_fee .' AUD'}}
#endforeach
</td>
Here's my controller function
public function detailedRegistrations($competition)
{
$competitions = $this->competitionsRepo->findOrFail($competition);
$teams = $competitions->teams()->get();
$payments = $competitions->payments()->get();
return view('competitions.detailed-registrations',
[
'teams'=>$teams,
'payments'=>$payments,
'competitions'=>$competitions,
]
);
}
Here's the competitionPayments relationship in the Team model
public function competitionPayments()
{
return $this->hasMany(CompetitionPayment::class, 'team_id');
}
Please give me an idea of how to make this work
here competitionPayments is a collection so you can apply sum() function on it like
<td>
{{ $payment->competitionPayments->sum('amount') }}
<br>
ref link https://laravel.com/docs/8.x/collections#method-sum
This works
<td>
{{ $team->competitionPayments->sum('amount') }}
/
#foreach ($team->competitions as $total)
{{ $total->pivot->total_fee .' AUD'}}
#endforeach
</td>

Call to undefined method error when I try to use links()

When I try to use $student->links() I see this error :
Facade\Ignition\Exceptions\ViewException
Call to undefined method App\Student::links()
I checked the controller, model etc but all of them seem OK... How can I fix this?
(I tried this code both on my Macbook and VPS -CentOS7- but same problem occurs)
That part of my view looks like this:
</tr>
#endforeach
</tbody>
</table>
{{ $student->links() }}
</div>
#endsection
Change
{{ $student->links() }}
to
{{ $students->links() }}
(use plural form).
You need to paginate in your backend code. $students = App\Student::paginate(15);
And then you can access the links()
<div class="container">
#foreach ($students as $student)
{{ $student->name }}
#endforeach
</div>
{{ $students->links() }}

PHP Laravel Pagination: Method links does not exist

Hi I'm trying to setup pagination for a simple comments page.
I'm getting the error Method links does not exist.
Is it due to the comments being referenced from it's relationship to the post class?
Can't work out what to do here...
CommentController.php
public function show($id)
{
$comments = Comment::find($id)->paginate(5);
return view('posts.show')->with('comments', $comments);
}
show.blade.php
#foreach ($post->comments as $comment)
<li>
User Name: {{ $comment->user_name }} <br>
Comment: {{ $comment->comment }} <br>
</li><br>
#endforeach
{{ $post->comments->links() }}
find() function find only one record and paginate workes with query builder or an Eloquent query, so you can use
$comments= Comment::where('post_id', $id)->paginate(5);
and replace $post->comments to $comments, it should be
#foreach ($comments as $comment)
<li>
User Name: {{$comment->user_name}} <br>
Comment: {{$comment->comment}} <br>
</li><br>
#endforeach
{{$comments->links()}}
In your controller method you are paginating $comments but use $post->comments in your view. Replace your code with:
#foreach ($comments as $comment)
<li>
User Name: {{$comment->user_name}} <br>
Comment: {{$comment->comment}} <br>
</li><br>
#endforeach
{{$comments->links()}}
create the pagination links using the render method:
#foreach ($post->comments as $comment)
<li>
User Name: {{$comment->user_name}} <br>
Comment: {{$comment->comment}} <br>
</li><br>
#endforeach
{!! $comments->render() !!}

hasMany relationship Laravel 5.4 Trying to get property of non-object

I have a relationship in my models with foreing keys, protected table.
This is my models:
Lot.php:
<?php
namespace App;
class Lot extends Model
{
protected $table = 'auct_lots_full';
protected $primaryKey = 'lot_id';
public function comments()
{
return $this->hasMany(Comment::class,'lot_id', 'lot_id');
}
}
Than related model Comment.php:
<?php
namespace App;
class Comment extends Model
{
public function lot()
{
return $this->belongsTo(Lot::class, 'lot_id', 'lot_id');
}
}
In my view I try to do this:
#foreach ($comments as $comment)
<dt>{{ $comment->lot }}</td>
#endforeach
It result this:
{"lot_id":391959148,"date":"2017-05-21","company":"MITSUBISHI","model_year_en":2005"}
Than in view I need to get only company information and I do this:
#foreach ($comments as $comment)
<td> {{ $comment->lot->company }}</td>
#endforeach
this result Error:
Trying to get property of non-object
I try to do this:
#foreach ($comments as $comment)
<td>
#foreach($comment->lot as $lot)
{{ $lot->company }}
#endforeach
</td>
#endforeach
And it also gives error:
Trying to get property of non-object
How can I get $comment->lot->company work?
Thanks, to Snapey from laracasts
when you do this
#foreach ($comments as $comment)
<td> {{ $comment->lot->company }}</td>
#endforeach
you are relying on EVERY comment having a lot and every lot having a company
You can use this to protect against empty properties
#foreach ($comments as $comment)
<td> {{ $comment->lot->company or '---'}}</td>
#endforeach
Then the loop will continue even if the lot does not have company

Categories