0
votes

I want to fetch data from my database but I am stuck when I try to get data from another table.

This is what I want to do, when a user login to the web page, he would be able to see a list of people name. By clicking on their name, user will be redirected to another page which will show the person details such as name, address and email. But because there is so many of them (assuming around 200 people)

Questions:

Q1. how do I redirect the user to another page which is the name (eg: jack, user click on jack name, jack id = 1, how do I relate it back to the user_id = 1 which is the foreign key)

Q2. how do I do a loop to get their information instead of specifying it 1 by 1 (eg: I don't want to keep saying id=1, is there something like id++ which will help auto add)

This is how it looks like with my code: (picture I took from the internet but it something similar to this) enter image description here

home.blade.php

    <table class="table table-bordered">
      <tr>
        <th><strong><big>Name AS PER NRIC/PASSPORT: </big></strong></th>
      </tr>
      <td>
      <tr>
        @foreach($data as $value)
      <tr>    
      <th><a href="www.google.com"> {{$value->Name}}</a></th> --> I don't know how to redirect it to show user details              
      </tr>
        @endforeach
      </tr>
      </tr>
    </table>

Controller

public function index()
    {
        return view('home');
    }
    public function getData(){
        $data['data'] = DB::table('personal_infos')->get()->sortByDesc('upload_time');
        if(count($data)>0){
        return view('home',$data);
}else{
    return view('home');
}
}
}

Route

Route::get('/test','testController@getData');

personal_info model

class personal_info extends Eloquent
{
    protected $fillable = array('Email', 'Name', 'address');
    protected $table = 'personal_infos';
    protected $primaryKey = 'id';
    public function user_infos() {
        return $this->hasMany('App\user_info,'user_id');
    }
        public function user_info1s() {
        return $this->hasMany('App\user_info1','user_id');
    }
}

user_info and user_info1 model

class user_info1 extends Eloquent
{
    protected $fillable = array('hobby','sport','user_id');
    public function personal_infos() {
        return $this->belongsTo('App\personal_info', 'user_id', 'id');
    }
2
personal_infos::with('user_info1s')->get()? - madalinivascu
@madlinivascu I have tried that already but it only work abit, I wanted to get the data based on their user id from different table - blastme

2 Answers

0
votes

First add a route like

Route::get('/test/{id}','testController@getPerson')->name("showPerson");

Then setup a redirect

<a href="{{route('showPerson', $value)}}">

Use this line instead of your href="www.google.com"

Make getPerson() from testController return a new page, on that page, print out all the info of a person.

I would also suggest you google Laracast and watch all of the free videos. They're about 30 and they give you a good understanding of the laravel framework

0
votes

Alter the link of your page to redirect you to the detail page:

<th><a href="{{route('user.show',['id'=>$value->id])}}"> {{$value->Name}}</a></th>

route should be:

Route::get('user/show/{id}','testController@getInfo')->name("user.show");

controller should be:

public function getInfo($id) {
  $user_info1 = user_info1::where('user_id',$id)->get();
  return $user_info1;//change this with your view
}