Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

 [HELP] Using JOIN table in laravel 5.4

views
     
TSMaster Oogway
post Mar 23 2017, 12:18 PM, updated 8y ago

Getting Started
**
Junior Member
68 posts

Joined: Mar 2016


route

CODE
$aaa = DB::table('products')->join('city', 'products.city_id', '=', 'city.id')->select('city.city_name')->get();


view

CODE
{{$aaa}}


it output the whole thing just like this

[{"city_name":"LUMUT"},{"city_name":"MENGLEMBU"},{"city_name":"PANGKOR"}]
TSMaster Oogway
post Mar 23 2017, 12:59 PM

Getting Started
**
Junior Member
68 posts

Joined: Mar 2016


anyone?
cubiclecarbonate
post Mar 23 2017, 01:18 PM

On my way
****
Junior Member
557 posts

Joined: Jul 2011


I dont see any error?? Thats how laravel return get(). A collection. Use @foreach to display the rows.
TSMaster Oogway
post Mar 23 2017, 01:56 PM

Getting Started
**
Junior Member
68 posts

Joined: Mar 2016


QUOTE(cubiclecarbonate @ Mar 23 2017, 01:18 PM)
I dont see any error?? Thats how laravel return get(). A collection. Use @foreach to display the rows.
*
how do i only display the city name?

e.g LUMUT
e.g MENGLEMBU
e.g PANGKOR
TSMaster Oogway
post Mar 23 2017, 01:58 PM

Getting Started
**
Junior Member
68 posts

Joined: Mar 2016


i tried this code below in my views, but it print/display whole data structure in my database

CODE
@if($city->where('city_id', '=', $product->city_id))
               @foreach($city as $cities)
                   {{ $cities->city_name }}
               @endforeach
           @endif

cubiclecarbonate
post Mar 23 2017, 02:00 PM

On my way
****
Junior Member
557 posts

Joined: Jul 2011


QUOTE(Master Oogway @ Mar 23 2017, 01:56 PM)
how do i only display the city name?

e.g LUMUT
e.g MENGLEMBU
e.g PANGKOR
*
CODE

.
.
.
.
<table>
@foreach ($aaa as $city)
<tr><td>{{ $city->city_name }}</td></tr>
@endforeach
</table>
.
.
.
.


try

This post has been edited by cubiclecarbonate: Mar 23 2017, 02:01 PM
TSMaster Oogway
post Mar 23 2017, 02:09 PM

Getting Started
**
Junior Member
68 posts

Joined: Mar 2016


QUOTE(cubiclecarbonate @ Mar 23 2017, 02:00 PM)
CODE

.
.
.
.
<table>
@foreach ($aaa as $city)
<tr><td>{{ $city->city_name }}</td></tr>
@endforeach
</table>
.
.
.
.


try
*
it is the same result
cubiclecarbonate
post Mar 23 2017, 02:15 PM

On my way
****
Junior Member
557 posts

Joined: Jul 2011


QUOTE(Master Oogway @ Mar 23 2017, 02:09 PM)
it is the same result
*
share your code and web screen
TSMaster Oogway
post Mar 23 2017, 02:44 PM

Getting Started
**
Junior Member
68 posts

Joined: Mar 2016


QUOTE(cubiclecarbonate @ Mar 23 2017, 02:15 PM)
share your code and web screen
*
controller

CODE
public function index(Request $request)
   {
       $products= Product::orderBy('id','DESC')->paginate(5);

       $test = DB::table('products')->join('city', 'products.city_id', '=', 'city.id')->select('city.city_name')->get();

       return view('ProductCRUD.index',compact('products', 'test'))->with('i', ($request->input('page', 1) - 1) * 5);
   }


view

CODE
@foreach ($products as $product)
   <tr>
       <td>{{ ++$i }}</td>
       <td>{{ $product->name}}</td>
       <td>{{ $product->details }} </td>
       <td>
           {{ $test }}
       </td>

       <td>
           <!--the buttons code-->
       </td>
   </tr>


products table
Attached Image

city table
Attached Image

result
Attached Image
cubiclecarbonate
post Mar 23 2017, 03:52 PM

On my way
****
Junior Member
557 posts

Joined: Jul 2011


QUOTE(Master Oogway @ Mar 23 2017, 02:44 PM)
controller

CODE
public function index(Request $request)
   {
       $products= Product::orderBy('id','DESC')->paginate(5);

       $test = DB::table('products')->join('city', 'products.city_id', '=', 'city.id')->select('city.city_name')->get();

       return view('ProductCRUD.index',compact('products', 'test'))->with('i', ($request->input('page', 1) - 1) * 5);
   }


view

CODE
@foreach ($products as $product)
   <tr>
       <td>{{ ++$i }}</td>
       <td>{{ $product->name}}</td>
       <td>{{ $product->details }} </td>
       <td>
           {{ $test }}
       </td>

       <td>
           <!--the buttons code-->
       </td>
   </tr>


products table
Attached Image

city table
Attached Image

result
Attached Image
*
Replace your $products loop with $test loop. Since u joined the tables under $test collection.

This post has been edited by cubiclecarbonate: Mar 23 2017, 03:53 PM
TSMaster Oogway
post Mar 23 2017, 03:54 PM

Getting Started
**
Junior Member
68 posts

Joined: Mar 2016


QUOTE(cubiclecarbonate @ Mar 23 2017, 03:52 PM)
Replace your $products loop with $test loop. Since u joined the tables under $test collection.
*
Attached Image

EDIT

CODE
@foreach ($test as $product)
   <tr>
       <td>{{ ++$i }}</td>
       <td>{{ $product->name}}</td>
       <td>{{ $product->details }} </td>
       <td>
           {{ $product->city_name }}
       </td>


Attached Image

This post has been edited by Master Oogway: Mar 23 2017, 03:57 PM
Moshpit94
post Mar 23 2017, 10:18 PM

Casual
***
Junior Member
341 posts

Joined: Feb 2011
From: Earth
For this function:

CODE
public function index(Request $request)
  {
      $products= Product::orderBy('id','DESC')->paginate(5);

      $test = DB::table('products')->join('city', 'products.city_id', '=', 'city.id')->select('city.city_name')->get();

      return view('ProductCRUD.index',compact('products', 'test'))->with('i', ($request->input('page', 1) - 1) * 5);
  }



I think this is where your problem is but I can't rectify where is the true source of problem since I'm using iPad right now.. maybe if this unsolved tomorrow I'll try take a look...

CODE
$test = DB::table('products')->join('city', 'products.city_id', '=', 'city.id')->select('city.city_name')->get();

TSMaster Oogway
post Mar 24 2017, 08:32 AM

Getting Started
**
Junior Member
68 posts

Joined: Mar 2016


QUOTE(Moshpit94 @ Mar 23 2017, 10:18 PM)
For this function:

CODE
public function index(Request $request)
  {
      $products= Product::orderBy('id','DESC')->paginate(5);

      $test = DB::table('products')->join('city', 'products.city_id', '=', 'city.id')->select('city.city_name')->get();

      return view('ProductCRUD.index',compact('products', 'test'))->with('i', ($request->input('page', 1) - 1) * 5);
  }

I think this is where your problem is but I can't rectify where is the true source of problem since I'm using iPad right now.. maybe if this unsolved tomorrow I'll try take a look...

CODE
$test = DB::table('products')->join('city', 'products.city_id', '=', 'city.id')->select('city.city_name')->get();

*
hope you could help me smile.gif
cubiclecarbonate
post Mar 24 2017, 10:59 AM

On my way
****
Junior Member
557 posts

Joined: Jul 2011


QUOTE(Master Oogway @ Mar 24 2017, 08:32 AM)
hope you could help me  smile.gif
*
this only return city_name field.
$test = DB::table('products')->join('city', 'products.city_id', '=', 'city.id')->select('city.city_name')->get();

this will return city_name field and all fields in products
$test = DB::table('products')->join('city', 'products.city_id', '=', 'city.id')->select('city.city_name', 'products.*')->get();

this is the view
@foreach ($test as $product)
<tr>
<td>{{ ++$i }}</td>
<td>{{ $product->name}}</td>
<td>{{ $product->details }} </td>
<td>
{{ $product->city_name }}
</td>

@endforeach

try again.
cubiclecarbonate
post Mar 24 2017, 11:00 AM

On my way
****
Junior Member
557 posts

Joined: Jul 2011


QUOTE(Master Oogway @ Mar 24 2017, 08:32 AM)
hope you could help me  smile.gif
*
controller
$test = DB::table('products')->join('city', 'products.city_id', '=', 'city.id')->select('city.city_name', 'products.*')->get();

view
@foreach ($test as $product)
<tr>
<td>{{ ++$i }}</td>
<td>{{ $product->name}}</td>
<td>{{ $product->details }} </td>
<td>
{{ $product->city_name }}
</td>
</tr>
@endforeach

try again.
TSMaster Oogway
post Mar 24 2017, 02:24 PM

Getting Started
**
Junior Member
68 posts

Joined: Mar 2016


QUOTE(cubiclecarbonate @ Mar 24 2017, 11:00 AM)
controller
$test = DB::table('products')->join('city', 'products.city_id', '=', 'city.id')->select('city.city_name', 'products.*')->get();

view
@foreach ($test as $product)
  <tr>
      <td>{{ ++$i }}</td>
      <td>{{ $product->name}}</td>
      <td>{{ $product->details }} </td>
      <td>
          {{ $product->city_name }}
      </td>
  </tr>
@endforeach

try again.
*
tried everything u posted, but it shows nothing as shown pic below

Attached Image
TSMaster Oogway
post Mar 24 2017, 02:27 PM

Getting Started
**
Junior Member
68 posts

Joined: Mar 2016


QUOTE(cubiclecarbonate @ Mar 24 2017, 11:00 AM)
controller
$test = DB::table('products')->join('city', 'products.city_id', '=', 'city.id')->select('city.city_name', 'products.*')->get();

view
@foreach ($test as $product)
  <tr>
      <td>{{ ++$i }}</td>
      <td>{{ $product->name}}</td>
      <td>{{ $product->details }} </td>
      <td>
          {{ $product->city_name }}
      </td>
  </tr>
@endforeach

try again.
*
okay its solved already, my mistake is that I didnt put $test in my looping

thank you very much cubiclecarbonate, u are the saviour of laravel rclxms.gif

 

Change to:
| Lo-Fi Version
0.0167sec    0.38    6 queries    GZIP Disabled
Time is now: 29th March 2024 - 08:48 AM