Answer the question
In order to leave comments, you need to log in
How to display customers of the relevant definitions. categories?
There are two tables. client-categories (id,title) and clients(id,title,link,category). In Clients -> category is id from the desired category. I have a list of categories on the page. You need to display customers that match the category.
How to do it? How to withdraw these clients? I was told that hasMany() but it doesn't work (
class ClientCategories extends Model
{
public $timestamps = false;
protected $table = 'client-categories';
protected $fillable = ['title'];
public function client()
{
return $this->hasMany('App/Clients');
}
}
<div class="clients">
@foreach($categories as $category)
<div class="slider-title clients-row">{{ $category['title'] }} <span class="plus">+</span></div>
<div class="slider-content">
<ul>
<li><span>{{ $category->client->title }}</span></li>
</ul>
</div>
@endforeach
</div>
Answer the question
In order to leave comments, you need to log in
Decided like this:
<div class="clients">
@foreach($categories as $category)
<? $client = App\Clients::find($category['id'])?>
<div class="slider-title clients-row">{{ $category['title'] }} <span class="plus">+</span></div>
<div class="slider-content">
<ul>
<li><span>{{ $client['title'] }}</span></li>
</ul>
</div>
@endforeach
</div>
Read about relationships in Eloquent in the official documentation. This is too trivial a question.
You decide whether you want to refer to it as an object or as an array.
Otherwise, in your code, then {{ $category->client->title }} then {{ $category['title'] }}.
Look in config/database.php for a way to get the data. And use it.
But using both is definitely not the best option.
I advise you to sleep more or be distracted from work, otherwise you are stupid out of the blue.
You have several clients in each category, the hasMany connection, but in the view you work with it as if there is only one client and the hasOne connection, although you have a collection and should have an
@foreach($category->client as $client) {{ $client ->title}}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question