vendredi 20 avril 2018

How to get country, region, town and area name for a user provided area_id migrates to user tables

I want to get the name of an area, a town, a region, and a country using the area_id which migrates to the user table that belongs to a particular user.

I have 5 tables which include,

A Country table with id, name, and description.

A Region table with id, name, description, and country_id

A Town table with id, name, description, and region_id.

An Area table with id, name, description, and town_id.

and I have a user table which the value of the area_id migrates to the user table and have attributes id, name, surname, email, phone, area_id.

I wish to know how can I get the name of the area, town, region, and country for a particular user, provided the area_id exists in the user table in the database using Laravel.

In my user Controller, I did something like this

 /**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */

public function accueil(Session $session)
{
    $recentJobbers =User::with('Area')
        ->limit(5)
         ->whereType('jobber')
        ->orderBy('updated_at', 'desc')
        ->get();

    return view('home', compact('recentJobbers','areas', 'categories'));
}

and in part of my blade view I did this

 <div class="col-lg-12">
                    <div class="heading">
                        <h2>Jobbeurs en vedette</h2>
                        <span>Principaux jobbers ayant déjà  été solicités pour leur travail.</span>
                    </div><!-- Heading -->
                    <div class="job-listings-sec">

                        @foreach($recentJobbers as $recentJobber)
                        <div class="job-listing">
                            <div class="job-title-sec">
                                <div class="c-logo"> <img src="" alt="Image" /> </div>
                                <h3><a href="" title="">  <span style="float:right; padding-left: 10px;">(a ete contacte 25 fois)</span></a></h3>
                                <span></span>
                            </div>
                            <span class="job-lctn"><i class="la la-map-marker"></i>, , </span>
                            <span class="fav-job"><i class="la la-heart-o"></i></span>
                            <a href="" class="job-is ft">Voir Profil</a>
                        </div><!-- Job -->
                        @endforeach
                    </div>
                </div>

and in my user model, I did something like this

  public function category() 
{
    return $this->belongsTo('App\Category');
}

public function service() 
{
    return $this->belongsTo('App\Service');
}

 public function region() 
{
    return $this->belongsTo('App\Region');
}

public function town() 
{
    return $this->belongsTo('App\Town');
}

 public function area() 
{
    return $this->belongsTo('App\Area');
}

But it only returns the area name. Does not return the town, region nor country name.

I wish to know what is the easiest way to achieve this. Any help will be welcome



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2Hh20OT
via IFTTT

Aucun commentaire:

Enregistrer un commentaire