Laravel 5.4: One to Many with One to One - laravel

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

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

Make a sum() of column accessors Laravel

im improving my skills in laravel making a simple challenges web but i stuck on the next problem.
I have the users table with the id, name, mail, etc. The challenges table where i list all the challenges (with columns id, name, body, answer, score) and a third table called "Answers" where i post each try of the users solving a challenge with the columns id, user_id, challenge_id, success (boolean) and score.
Now i'am trying to make a ranking of users with their scores. The problem is to show a sum() of the column 'score' in the table answers where user_id.answers = users.id. So i can get easy the total score for each user.
For now i solved the problem doing this in blade:
#foreach($users as $user)
<tr>
<th>{{ $user->id }}</th>
<td>{{ $user->name }}</td>
<td>Team pindonga</td>
<td>{{ score = DB::table('answers')->where('user_id', $user->id)->sum('score') }} </td> </tr>
#endforeach
works perfectly, but i want to get them by desc score (for now i solved the desc with js but want to get desc order from the eloquent query) and have something like this in the blade:
#foreach($users as $user)
<tr>
<th>{{ $user->id }}</th>
<td>{{ $user->name }}</td>
<td>Team pindonga</td>
<td>{{ $team->score }} </td> </tr>
#endforeach
I was thinking about solve it with accessors doing something like this:
public function getScoreAttribute($value)
{
return $this->groupBy('user_id')->where('user_id', '=', $value)->sum('score');
}
also i have a relationship belongsTo in the answers modle to users and a hasMany in the users to answers. But i'm not sure of how pass the results to blade. Tried in tinker:
>>> \App\Answer::find(1)->score;
=> "3400"
>>> \App\Answer::find(2)->score;
=> "3400"
>>> \App\Answer::find(5)->score;
=> "3400"
allways getting the sum of the id = 1. And with $user->score; in the blade foreach i got a '0'.
Using the relationship with Blade would be {{ $user->answers->sum('score') }}. No accessor needed.
For sorting I would use the sortByDesc collection method.
$users = User::get();
$users = $users->sortByDesc(function ($user) {
return $user->answers->sum('score');
});
I think this is what you're looking for. It takes a while to get used to Eloquent ORM :-)
foreach($users as $user){
print $user->answers->sum('score');
}
https://laravel.com/docs/5.6/queries#aggregates

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

Dynamic properties with ManyToOne (Laravel4/Eloquent)

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

Resources