I am newbie to jsTree, I want to use it in my Laravel project.I have department and user table .A department has many users.A user belongs to one department.This is the foreach loop that i tried to show users and departments,but its not with jstree.I coudn't find any information that could help me with this in Laravel.I would appreciate a lot if someone could help me with some information about this.Thanks in advance
#foreach($employees as $employee)
<tr>
<td>{{ $employee->name }}</td>
<td>{{ $employee->department->name}}</td>
</tr>
#endforeach
department.php
{
return $this->hasMany(Employee::class);
}
employee.php
public function department()
{
return $this->belongsTo(Department::class);
}
Related
I have two models
Item:
protected $fillable = [
'code',
'name',
'uom'
];
public function recipe()
{
return $this->belongsTo('App\Recipe');
}
Recipe:
protected $fillable = [
'recipecode',
'itemcode',
'qty'
];
public function item()
{
return $this->hasMany('App\Item');
}
In migration I have:
public function up()
{
Schema::table('recipes', function (Blueprint $table) {
$table->foreign('itemcode')->references('code')->on('items');
});
}
In RecipesController I have:
public function index()
{
$recipes = Recipe::all()->sortBy('recipecode');
$items = Item::all();
return view ('recipe.index', compact('recipes', 'items'));
}
I want to see in the view recipecode, itemcode and ItemName, ItemQty etc.
#foreach($recipes as $recipe)
<tr>
<td>{{ $recipe->recipecode }}</td> // this works
<td>{{ $recipe->itemcode }}</td> // this works
<td>{{ $recipe->item->name }}</td> // this doesn't work
<td>{{ $recipe->item->uom }}</td> // this doesn't work
<td>{{ $recipe->item->qty }}</td> // this doesn't work
</tr>
#endforeach
What should I do in order to see 'name' and 'uom' columns from Items table? I think there is a problem with relations...
First, $recipe->item->name does not work as you expected because $recipe->item is a collection of App\Item, not an App\Item model instance. That's because you have set the relationship to be "hasMany"
Second, please read more about eager loading. What you're doing now are multiple queries. That's the N+1, for each recipe you are querying the database to get the items. You do not need to do that, as it's not performant at all. Use Recipe::with('item')->sortBy('recipecode')->get().
Now, keep in mind that $recipe->item is not a single item, but a collection of items that belong to that Recipe. You'll probably have to work a bit on your table to display multiple items for one Recipe.
Since you are using hasMany you must follow naming convention in naming function
public function items()
{
return $this->hasMany('App\Item');
}
In RecipesController you can eager load.
$recipes = Recipe::with('items')->sortBy('recipecode')->get();
Read documentation here: https://laravel.com/docs/master/eloquent-relationships
Also to show it in your blade
#foreach($recipes as $recipe)
<tr>
<td>{{ $recipe->recipecode }}</td> // this works
<td>{{ $recipe->itemcode }}</td> // this works
#foreach($recipe->items as $item)
<td>{{ $item->name }}</td>
#endforeach
</tr>
#endforeach
Other answers share why you can't directly use $recipe->item. However, I think there is a bigger mistake in database design.
The database that you have designed have a one to many relation. Like, one recipe has a lot of items. However, the reverse one that one item has only one recipe seems wrong. You can cook many recipes with one items. I.E. you can make chicken fry and chicken curry with chicken.
The database design should be something like this,
recipes: code, name, uom
items: code, name
item_recipe: item_code, recipe_code, qty
I created one to one relationship where user belongs to town, so in User model I have:
public function town()
{
return $this->belongsTo('App\Town');
}
and in users table I have town_id column. So, when user post something it says that user is from that town. That works.
Now, I created Settlement model which in same way belongs to App\Town (many users can come with one town), so again when settlement is created with some user it displays town of user and settlement which also contain town_id as that user table. (I hope I'm not confusing)
Question: When user is logged in, how to display settlements from his town only?
This is my code until now:
AdminSettlementsController:
public function index()
{
$settlements = Settlement::paginate(10);
$towns = Auth::user()->town->id;
$town[$towns] = Auth::user()->town->name;
$reon = Reon::pluck('name', 'id')->all();
return view('admin.settlements.index', compact('settlements', 'town', 'reon'));
}
User model has this:
public function town()
{
return $this->belongsTo('App\Town');
}
and index.blade.php in settlements folder has this table:
#if ($settlements)
#foreach ($settlements as $settlement)
<tr>
<td>{{ $settlement->id }}</td>
<td>{{ $settlement->town->name }}</td>
<td>{{ $settlement->reon->name }}</td>
<td>{{ $settlement->name }}</td>
</tr>
#endforeach
#endif
I presume that in Town model I need to have settlements() method where that method hasMany App\Settlement but I don't know how to go from there.
I hope my english is not too bad. Thanks
I'm trying to display in my view data from an eloquent relationship but i seem to be doing a tiny bit wrong. dd shows the relation in the collection but i just can't call the data correctly in my view. Below is what i have done
Employee model
public function task()
{
return $this->hasMany(Task::class);
}
Task model
public function employee()
{
return $this->belongsTo(Employee::class);
}
TaskController
public function index()
{
$alltask = Task::with('employee')->get();
dd($alltask);
/*return view('task.task', compact('alltask', 'empwithtask'));*/
}
task view
#foreach ($alltask as $task)
<tr>
<td>{{ $task->priority }}</td>
<td>{{ $task->firstname }}</td>
/* this is meant to be the employee.firstname */
<td>{{ $task->title }}</td>
<td>{{ $task->begin }}</td>
</tr>
#endforeach
I'm not able to display $task->firstname, firstname is from the employees table. Below is a snapshot of the result of dd
How do I show the employee firstname?
$task->employee->firstname
How can i show the classes only for student who is enrolled in a class and i have 2 tables and a pivot table?
Table 1: Home_Students : home_id , home_studid ....
Table 2: Hw_Classes :class_id , class_desc , class_date ....
Pivot table:Hw_StudentClasses :Stclass_id, Stclass_classid, Stclass_studid
So i made a model MySeminarClasses to communicate with the table Hw_StudentClasses and a Controller MySeminarClassesController
I made relations in the model of the other tables belongsToMany
public function Users(){
return $this->belongsToMany('User','home_id');
}
public function SeminarClass(){
return $this->belongsToMany('SeminarClass','class_id');
}
Also in the Controller i did this which im not so sure if its right but i did this from the instructions of the laravel 4.2 documentation
$myclasses = DB::table('Hw_StudentClasses')
->join('Hw_Classes','Hw_StudentClasses.Stclass_classid','=','Hw_Classes.Class_id')
->join('Home_Students','Hw_StudentClasses.Stclass_studid','=','Home_Students.home_studid')
->orderBy('class_date',strtotime('-4 month'))
->get();
Finally in the blade
<tbody>
#foreach($myclasses as $i=>$myclass)
<tr>
<td>{{ $i+1 }}</td>
**<td>{{ link_to_route('class.show',$myclass->Class_desc,$myclass->Class_id) }}</td>**
<td class="text-center">{{ date('j-n-Y G:i',strtotime($myclass->class_date)) }}</td>
<td class="text-center">{{ UserEnroll::where('Stclass_classid',$myclass->Class_id)->count() }}</td>
</tr>
#endforeach
</tbody>
After a 16 hours of struggle ......i aswered my own question ! This query with Auth::user helped me. Show the classes only for every user who is logged in
$id = Auth::user()->home_studid;
$date = date('m-d-Y', strtotime('-4 month'));
$seminars = Seminar::where('Package_Display', 1)
->orWhere('Package_Display', 2)
->orderBy('Package_Name', 'asc')
->get();
$myclasses = DB::table('Hw_StudentClasses')
->join('Hw_Classes','Hw_StudentClasses.Stclass_classid','=','Hw_Classes.Class_id')
->join('Home_Students','Hw_StudentClasses.Stclass_studid','=','Home_Students.home_studid')
->where('Home_Students.home_studid','=',$id)
->orderBy('class_date',strtotime('-4 month'))
->get();
HOPE THIS HELPS SOMEONE ! ^_^
I have a pretty basic relationship wherein searches are logged and stored along with the ID of the user who performed the search.
I'm trying to output this log and display the username instead of the id.
Search
public function user()
{
return $this->belongsTo('User');
}
User
public function searches()
{
return $this->hasMany('Search');
}
If I then pass Search::all(); to the view, how can I echo the username? Among many other things, I've tried:
#foreach($searches as $search)
<tr>
<td>{{ $search->user->username }}</td>
<td>{{ $search->description }}</td>
<td>{{ $search->product }}</td>
<td>{{ $search->group }}</td>
</tr>
#endforeach
Any help is much appreciated. Thanks!
Currently, Your loop has N + 1 problem.
Your loop will execute 1 query to retrieve all of the Searches on the table, then another query for each search to retrieve the user. So, if you have 20 searches, this loop would run 21 queries.
<td>{{ $search->user->username }}</td>
to avoid N + 1 problem, use the eager loading to load the relationship.
Search::with('user')->all()
More info about N+1 problem:
http://laravel.com/docs/eloquent#eager-loading