PHP Laravel Pagination: Method links does not exist - php

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() !!}

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

get unique Tasks From relation

I have relation tags that have many tasks whet I get data from foreach I get duplicated values
in example
in my path i have 2 tags PHP ,HTML5
PHP Has [PHP Task_1 , PHP_Task_2]
HTML5 Has [HTml5 Task_1,PHPTask_2]
because task has many tags so i will get to duplicated PHP_Task_2
i need to get every task just one time
My Controller
$posts2 = Path::with(['pathtags' => function ($q) use ($TagArray)
{$q->with(['Tasks' => function ($q) use ($TagArray) {$q->has('tasktags', '=', 2)
->with('tasktags');
}]);
}])->where('id', '=', 1)->get();
My Blade
#foreach ($posts2 as $item)
<h2> {{$item->name}}</h2>
#foreach ($item->pathtags as $Tag)
<li> Path Tag :: {{ $Tag->name }} </li>
#foreach ($Tag->Tasks as $Task)
<li> Task :: {{ $Task->task_name }} </li>
#foreach ($Task->tasktags as $TaskTag)
<li> Task Tags :: {{ $TaskTag->name }} </li>
#endforeach
#endforeach
#endforeach
#endforeach

Laravel 5.3 : Error while fetch data in view using foreach

i got error while i'm trying to fetch my data in view. This is my code :
controller
public function create()
{
$categories = Category::all();
$tag = Tag::groupBy('name')->pluck('name');
$tags = json_encode($tag);
return view('backend.articles.create', compact('categories', 'tags'));
}
view
fetch data categories
<div class="panel-body">
#foreach ($categories as $category)
<div class="checkbox">
<label>
{!! Form::checkbox('category_id[]', $category->id) !!} {{ $category->name }}
</label>
</div>
#endforeach
</div>
fetch data tags
jQuery(document).ready(function($) {
$('#tags').magicSuggest({
cls: 'form-control',
data: {!!$tags!!},
#if (count(old('tags')))
value: [
#foreach (old('tags') as $tag)
'{{$tag}}',
#endforeach
]
#endif
});
});
I dont know why i got this error message : Invalid argument supplied for foreach(). Could anybody to help me to fix this problem ?
view
<div class="panel-body">
#if($categories)
#foreach ($categories as $category)
<div class="checkbox">
<label>
{!! Form::checkbox('category_id[]', $category->id) !!} {{ $category->name }}
</label>
</div>
#endforeach
#endif
script
jQuery(document).ready(function($) {
$('#tags').magicSuggest({
cls: 'form-control',
data: {!!$tags!!},
#if (is_array(old('tags')))
value: [
#foreach (old('tags') as $tag)
'{{$tag}}',
#endforeach
]
#endif
});
});
Try to use is_array to make sure old('tags') returning array, I believe the problem because the old('tags') is not returning array.

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

#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

Returning checked checkboxes based on pivot table

This question is regarding Laravel and PHP.
I am building a form that allows users (players) to be assigned to / de-assigned from a team using checkboxes.
Many users can belong to many teams. I have a pivot table team_user to manage the many-to-many relationship.
I am having trouble populating the checkboxes based on whether the association exists in the pivot table.
So far I have them in two separate 'bits', which you can see below:
My Controller:
$users_checked = $team->users()->get();
$users = User::all();
return View::make('team/addplayers', compact('users', 'team', 'users_checked'));
My View:
#foreach ($users_checked as $user_checked)
<p>
{{ Form::checkbox('player[]', $user_checked->id, true) }}
{{ Form::label('email', $user_checked->email) }}
</p>
#endforeach
#foreach($users as $user)
<p>
{{ Form::checkbox('player[]', $user->id ) }}
{{ Form::label('email', $user->email) }}
</p>
#endforeach
What is the most sensible way to 'combine' these into one list, where the checkbox is ticked if the association exists in the pivot, or not ticked if it doesn't?
Many thanks for any help!
The answers put me on the right track, thank you. My actual solution is below:
In my controller:
public function showAddPlayer(Team $team)
{
$users = User::all();
return View::make('team/addplayers', compact('users', 'team'));
}
In my view:
#foreach ($users as $user)
<p>
{{ Form::checkbox('player[]', $user->id, $team->users->contains($user->id)) }}
{{ Form::label('email', $user->email) }}
</p>
#endforeach
I think you may try something like this:
$teams = Team::with('users')->get();
return View::make('team/addplayers', compact('teams'));
In your view:
#foreach ($teams as $team)
#foreach ($team->users as $user)
<p>
{{ Form::checkbox(
'player[]',
$user->id,
(in_array($user->id, $team->users->fetch('id')) ? 1 : 0)
)
}}
{{ Form::label('email', $user->email) }}
</p>
#endforeach
#endforeach
{{ Form::checkbox('player[]', $user->id,in_array($user->id, $users_checked->lists('id')) ? 1 : 0 ) }}

Categories