My controller is
public function index()
{
$users = User::paginate(15);
return view('dash.users.index')->with(array('users' => $users));
}
in view i pass: {{ $item->roles }} and return all column as array, if i put {{ $item->roles->get('name') }}the table is blank, why?
I want to show user->roles->name foreach user
If it's an array, you should only simply use this:
{{ $item->roles['name'] }}
solved with nested foreach! #foreach($item->roles as $item) {{ $item['name'] }} #endforeach
inside my #foreach user
Thank you Mojtaba for the help!
Related
I have a database with 3 tables. A separate model is connected to each table, and there is a controller that accepts values from all models. On the site page, I will have 3 tables that will be populated from a mysql table.
When I connected 2 models, everything worked fine. But after connecting 3, I get an error
undefined variable: sec_3.
If you delete one of the variables, then everything will work fine. It seems to me that the problem is either with the controller or with the file blade.php but I do not know how to fix it so that everything works properly. How to fix it?
My code:
Controller:
class PreschoolInstitution3Controller extends Controller {
public function index(){
$context=['bbs' =>PreschoolInstitution3::latest()->get()];
$context_2=['s' =>PreschoolInstitution::latest()->get()];
$context_3=['sec_3' => TrainingPrograms::latest()->get()];
return view('our_employees', $context, $context_2, $context_3);
}
}
web.php:
Route::get('/OurEmployees',[PreschoolInstitution3Controller::class,'index'] )->name('OurEmployees');
blade.php:
#foreach ($s as $section_2) <tr> <td>{{$section_2->number}}<td> <td>{{$section_2->fullname }}<td> <td>{{$section_2->post }}<td> <td>{{$section_2->telephone }}</td> <td>{{$section_2->email }}</td>
#endforeach #foreach ($bbs as $section )
{{$section->number}} {{$section->full_name}} {{$section->post}} {{$section->education}} {{$section->category}} {{$section->teaching_experience}} {{$section->professional_development}}
#endforeach #foreach ($sec_3 as $section_3)
{{ $section_3->number }}
{{ $section_3->level }}
{{ $section_3->directions }}
{{ $section_3->type_of_educational_program }}
{{ $section_3->period_of_assimilation }}
{{ $section_3->number_of_students }}
#endforeach
You should pass an array of data to view:
class PreschoolInstitution3Controller extends Controller {
public function index(){
$context = [
'bbs' => PreschoolInstitution3::latest()->get(),
's' => PreschoolInstitution::latest()->get(),
'sec_3' => TrainingPrograms::latest()->get()
];
return view('our_employees', $context);
}
}
https://laravel.com/docs/9.x/views#passing-data-to-views
Another one is that add a second parameter of an array with name to view( ) .
class PreschoolInstitution3Controller extends Controller {
public function index(){
$bbs = PreschoolInstitution3::latest()->get();
$s = PreschoolInstitution::latest()->get();
$sec_3 = TrainingPrograms::latest()->get();
return view('our_employees', [
'bbs' => $bbs,
's' => $s,
'sec_3' => $sec_3
]);
}
}
This code is working fine
public function render(){
$this->products = ProductModel::get();
return view('livewire.product');
}
But when I am trying to paginate using laravel livewire, it gives me an error
public function render(){
return view('livewire.product', [
'products' => ProductModel::paginate(10)
]);
}
Blade File
#foreach ($products as $product)
{{ $product->name }}
{{ $product->price }}
#endforeach
#if(!empty($products))
{{ $products->links() }}
#endif
import this in Component
use Livewire\WithPagination;
class Product extends Component
{
use WithPagination;
....
}
and add in view
#if(!empty($products))
{{ $products->links() }}
#endif
Ohh I got it.Actually I have already use $products variable as a global variable
and when I change $products to other name it works.
Thanks alot...
In my blade component I am entering this:
{{ $post['tags']->pluck('slug') }}
but getting the following result:
["abc","pqr"]
$post['tags']->slug is giving an error
You need to use foreach :
#foreach($post['tags']->pluck('slug') as $key => $slug)
{{ $slug }}
#endforeach
I'm using the VentureCraft/revisionable-package and it shows me in the ReadMe how to show the Revisions of a Model that has revisions:
#foreach($account->revisionHistory as $history )
<li>
{{ $history->userResponsible()->first_name }}
changed {{ $history->fieldName() }}
from {{ $history->oldValue() }}
to {{ $history->newValue() }}
</li>
#endforeach
But I want a list of All revisions that are done by a Specific user; how to achieve that? So I can show a History of Revisions that are done by one specific user.
I have never used this package. But based on what I see, you should be able to add this in your User model
public function revisions()
{
return $this->hasMany(\Venturecraft\Revisionable\Revision::class)
}
then
#foreach($user->revisions as $history )
<li>
{{ $user->first_name }}
changed {{ $history->fieldName() }}
from {{ $history->oldValue() }}
to {{ $history->newValue() }}
</li>
#endforeach
As you asked in the comments :
But I'm missing the Entity that's changed in that list.
(optional) I would implement an interface to my revisionable models with something like :
<?php
namespace App\Contracts;
interface RevisionableContract {
public function entityName();
}
Then in all my models that use the RevisionableTrait :
<?php
namespace App\Models;
class MyModel extend Eloquent implements RevisionableContract {
use RevisionableTrait;
// (required)
public function entityName(){
return 'My Entity name';
}
}
Finally :
#foreach($user->revisions as $history )
<li>
{{ $user->first_name }}
changed {{ $history->fieldName() }}
from {{ $history->oldValue() }}
to {{ $history->newValue() }}
on the entity {{ $history->historyOf()->entityName() }}
</li>
#endforeach
historyOf() may return false
Do you also have an idea how I can make a list of all revisions in desc-order with that info of the user?
From the migrations file, I can see that it has created_at and updated_at timestamps.
You have two possibilities :
In your view, you can directly order them on your collection like this :
#foreach($user->revisions->sortByDesc('created_at') as $history )
When you get a lot of revisions for a user, you will probably have performance issues and you will have to paginate them. From your controller, you will have to sort them and paginate them in your query instead of the collection.
public function index()
{
$user = User::find(1);
$revisions = $user->revisions()->orderBy('created_at')->paginate(15);
return view('your.view', compact('user', 'revisions'));
}
I could not use that package but it seems quite easy to understand. If you can show history of a user you should add this to your "User" entity:
public function history()
{
return $this->hasMany(\Venturecraft\Revisionable\Revision::class, 'user_id', 'id');
}
Or if you want to filter a specific morphable entity you should do it like this:
public function historyForUser(User $user)
{
return $this->morphMany(\Venturecraft\Revisionable\Revision::class, 'revisionable')->where('user_id' , '=', $user->getKey())->getResults();
}
I think that answer is corresponded for what you want to do.
i am new to laravel and i work on eloquent i want to send some data to view and show them like laravel documentation says so in my controller i wrote
public function show(PhoneBook $phoneBook)
{
return view('admin.phonebook.show',compact('phoneBook',$phoneBook));
}
and in the phonebook show view i wrote
<strong>id</strong> {{ $phoneBook->id }}<br>
<strong>calldate</strong> {{ $phoneBook->calldate }}<br>
<strong>description</strong> {{ $phoneBook->description }}
any idea why i receive empty query with no result with no errors and just the titles with no result in front of them i thank you if you can help me with this
Controller:
public function show($id)
{
$phonebook = Phonebook::find($id);
return view('admin.phonebook.show',compact('phonebook'));
}
Blade:
<strong>id</strong> {{ $phonebook->id }}<br>
<strong>calldate</strong> {{ $phonebook->calldate }}<br>
<strong>description</strong> {{ $phonebook->description }}
Try this:-
return view('admin.phonebook.show', compact('phoneBook'));
You can try 2 ways below to send data from Controller to View:
return view('admin.phonebook.show', compact('phoneBook'));
OR
return view('admin.phonebook.show')->with('phoneBook', $phoneBook);