samedi 17 octobre 2015

fetch multiple row data from multiple tables in laravel 5.1

While working on admin control panel using laravel 5.1 framework, I am stuck in mid of project developement.

I am unable to fetch the data from multiple tables with multiple rows. Here is my scenario

Please check this images which is my table structure.

[1] Offer Table - saves all offers

enter image description here

[2] Restaurant Table - Saves all restaurant information

enter image description here

[3] OfferImage Table - Saves all offers images

enter image description here

[4] FoodItem Table - saves all food items

enter image description here

What I want to do is , display all offers as displayed in Image 1,but doing this is quite difficult for me.

Here is my controller code

public function index()
{
  $alldata=Offer::all(); //this line is correct 

  $offerimage=Offer_image::all(); //this 3 lines are logically incorrect
  $restaurant=Restaurant::all();
  $fooditem=Food_item::all();
  return view('admin.listoffer',
  compact('alldata','offerimage','restaurant','fooditem'));
}

This is my code in view

@foreach($alldata as $data)  
 <tr role="row" class="odd">
 <td class="sorting_1">{{ $data->offer_id }}</td>
 <td>{{ $offerimage->img_filename }} !!}</td>
 <td>{{ $restaurant->rest_name }}</td>
 <td>{{ $fooditem->item_name }}</td>
 <td>{{ $data->offer_code }}</td>
 <td>{{ $data->description }}</td>
 <td>{{ $data->total_price }}</td>
 <td>{{ $data->disc_value }}</td>
 <td>{{ $data->disc_percentage }}</td>
 <td>{{ $data->status }}</td>
 <td><a href="{{ Route('offer.edit',$data->rest_id) }}" class="btn btn-success">Edit</a><br> 

 {!! Form::open(array('route'=>['offer.destroy',$data->offer_id],'method'=>'delete')) !!}
 {!! Form::hidden('id',$data->offer_id) !!}
                    {!! Form::submit('Delete',$attributes=array('class'=>'btn btn-danger')) !!}
                {!! Form::close() !!}
  </td>
  </tr>
@endforeach

In above controller , how do i write code so that I can get all rows from offer table, as well as it's data from other tables also. I searched for Laravel 5.1 documentation, & eloquent relationship but I am unable to fit this type in any relations.

help is really appreciated.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire