G
G
GaserV2016-05-31 11:10:15
Laravel
GaserV, 2016-05-31 11:10:15

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

I output like this:
<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>

And instead of title for clients, I get {"id":1,"title":"\u0418\u0441\u043a\u0443\u0441\u0441\u0442\u0432\u043e"}

Answer the question

In order to leave comments, you need to log in

4 answer(s)
G
GaserV, 2016-05-31
@GaserV

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>

S
Stanislav Pochepko, 2016-05-31
@DJZT

Read about relationships in Eloquent in the official documentation. This is too trivial a question.

D
Dinar Garipov, 2016-05-31
@gaaarfild

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.

V
Vyacheslav Plisko, 2016-06-01
@AmdY

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 question

Ask a Question

731 491 924 answers to any question