Unable to display relations data from eloquent relationship - laravel-5

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

Related

Jstree in laravel

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);
}

Laravel what relation to use in order to see data from another model and column

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

Laravel 5.4: One to Many with One to One

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

Laravel Pivot Relationship not unique table/alias

I have three Tables & corresponding models:
Table ------ Model
papers ------ Paper
paper_stocks ------ PaperStock
paper_stock_amounts ------ PaperStockAmount
Paper contains diff. types of paper.
PaperStock contains different places where these papers can be stored
PaperStockAmount contains the paper id, the stock id and the amount, to determine how many of paper A are in stock B.
Now I want to check outgoing from the paper how manyy are in each stock. Therefore Im doing this in my view:
#foreach($papers as $paper)
#foreach($paper->paperStockAmount as $ps)
<tr>
<td>{{ $ps->stock->name }}</td>
<td>{{ $ps->amount }}</td>
</tr>
#endforeach
#endforeach
This should show every type of paper and in which stocks along with the amount they are in.
However this is the error:
SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'paper_stock_amounts' (SQL: select `paper_stock_amounts`.*, `paper_stock_amounts`.`id` as `pivot_id`, `paper_stock_amounts`.`amount` as `pivot_amount` from `paper_stock_amounts` inner join `paper_stock_amounts` on `paper_stock_amounts`.`id` = `paper_stock_amounts`.`id` where `paper_stock_amounts`.`id` in (1, 2))
The relationships are defined like this:
Paper
class Paper extends Model
{
public function paperStockAmount()
{
return $this->belongsToMany('App\PaperStockAmount', 'paper_stock_amounts')->withPivot('amount');
}
}
PaperStockAmount
class PaperStockAmount extends Model
{
public function paper()
{
return $this->hasOne('App\Paper', 'id', 'paper_id');
}
public function stock()
{
return $this->hasOne('App\PaperStock', 'id', 'stock_id');
}
}
Why doesn't this work?
You've defined M-M Relationship between paper & paper_stock.
i.e. If you are fetching paperStockAmount from $paper, then you would get paper_stock results with the pivot column amount.
You should do it like this:
#foreach($papers as $paper)
#foreach($paper->paperStockAmount as $paper_stock)
<tr>
<td>{{ $paper_stock->name }}</td>
<td>{{ $paper_stock->pivot->amount }}</td>
</tr>
#endforeach
#endforeach
Use pivot to fetch the pivot column named - amount
Hope this helps!
I am not sure but it looks your relation from Paper to PaperStockAmount is has-many, so your relation should be as:
Paper
class Paper extends Model
{
public function paperStockAmount()
{
return $this->belongsToMany('App\PaperStockAmount');
}
}
Then your view code will work.
OR
If the relation between from Paper and PaperStock is many-to-many then your relation should be as:
Paper
class Paper extends Model
{
public function paperStockAmount()
{
return $this->hasMany('App\PaperStock', 'paper_stock_amounts')->withPivot('amount');
}
}
Then in your view, it can be accessed as: (taking from #saumya answer)
#foreach($papers as $paper)
#foreach($paper->paperStockAmount as $paper_stock)
<tr>
<td>{{ $paper_stock->name }}</td>
<td>{{ $paper_stock->pivot->amount }}</td>
</tr>
#endforeach
#endforeach

Laravel Count from database

I want to count the amount of workers a company has.
So it shows like: McDonalds - 200 Employees (example)
Model:
public function getTotalWorkers()
{
return $this->hasMany('App\User')->whereCompanyId($this->company_id)->count();
}
View:
#foreach($companies as $company)
<tr>
<td>{!! link_to_route('company.edit', $company->name, [$company->id] )!!}</td>
<td>{{ $getTotalWorkers }}</td>
<td>{{ ' /' }}</td>
<td>{{ $company->parent_id }}</td>
<td>{{ ' /' }}</td>
<td>{{ $company->active }}</td>
</tr>
#endforeach
Controller:
public function index()
{
$getTotalWorkers = $this->company->getTotalWorkers();
$companies = Company::get();
return view('company.index', ['companies' => $companies, 'getTotalWorkers' => $getTotalWorkers]);
But this gives me an error: Call to a member function getTotalWorkers() on a non-object
How can I fix this or what am I doing wrong?
it looks like you are calling getTotalWorkers() on $post, not $company on the fourth line of the view snippet you have posted.
How you are doing it currently fundamentally will not work for what you want it to do.
You are currently calling ->getTotalWorkers() on your Controller class since you are using:
$getTotalWorkers = $this->company->getTotalWorkers();
This wont work as the controller does not have this method (Your use of $this-> tells PHP to call it on the current class, it will work inside your model class but not on your controller. You have to call it on something, ie a company).
To get it to work how you are wanting it, remove that line from your controller, remove the "getTotalWorkers" => $getTotalWorkers, too.
In your view you can change:
<td>{{ $getTotalWorkers }}</td>
To:
<td>{{ $company->getTotalWorkers() }}</td>
Which will get it to count the number of workers for each company. Note this will run a new query for each different company you list.

Resources