【问题标题】:How can I get data from database in laravel?如何从 laravel 的数据库中获取数据?
【发布时间】:2019-06-21 11:05:05
【问题描述】:

我希望能够从数据库中获取数据。

我需要名为role_users 的表,列role_id 和表functions,列function

这是我的观点:

@foreach($contact_users as $contact)

<a   href="{{ url('') }}/{{ $contact->username }}">{{ $contact->username  }}</a></h3>
        <p>
            <strong><i class="icon-real-estate-020 u-line-icon-pro"></i> : </strong><a  href="#">{{ $contact->country }}</a><br>
        <strong><i class="icon-screen-tablet fa-" aria-hidden="true"></i> : </strong><a href="#">{{ $contact->industry }}</a><br>
        <strong><i class="icon-frame fa-" aria-hidden="true"></i> : </strong><a href="#">{{ $contact->organization_type }}</a><br>

@endforeach

这是我的控制器:

$data['contact_users'] = DB::table('contacts')
             ->join('users' , 'users.id', '=', 'contacts.contact_id')
             ->join('industries' , 'industries.id', '=', 'users.industry_id')
             ->join('countries' , 'countries.id', '=', 'users.country_id')
             ->join('organization_types' , 'organization_types.id', '=', 'users.organization_type_id')
                    ->select('users.*','industries.industry','countries.country','organization_types.organization_type')
             ->where('contacts.contact_id','!=',$id)
             ->where('users.deleted_at','=',NULL)
             ->whereIn('contacts.user_id', $contact_id)
             ->whereNotIn('contacts.contact_id', $contact_id)
             ->whereNotIn('contacts.contact_id', $inviter_id)
             ->groupBy('contact_id')
             ->take(4)
             ->get();

我想在organization_type下面添加功能。谢谢。

【问题讨论】:

    标签: php database laravel


    【解决方案1】:

    为什么要使用数据库查询连接?

    您需要先定义模型,然后才能使用 Eloquent 进行更清晰的查询。请查看文档了解更多信息。

    (https://laravel.com/docs/5.8/eloquent)https://laravel.com/docs/5.8/eloquent

    【讨论】:

      猜你喜欢
      • 2021-03-24
      • 1970-01-01
      • 1970-01-01
      • 2019-11-12
      • 1970-01-01
      • 1970-01-01
      • 2018-10-24
      • 2014-06-05
      • 2019-08-15
      相关资源
      最近更新 更多