Reputation: 483
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
[2] Restaurant Table - Saves all restaurant information
[3] OfferImage Table - Saves all offers images
[4] FoodItem Table - saves all food items
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.
Upvotes: 2
Views: 4616
Reputation: 62228
You need to relate these models to each other in order do to what you want. As an example, let's look at the Offer - Restaurant relationship.
I'm assuming that a Restaurant can be related to many Offers, but an Offer can only be related to one Restaurant. In this case, you would say that Restaurant hasMany Offers, and Offer belongsTo Restaurant.
In order to facilitate this relationship, you need to add a field to your Offer table to store the id of the Restaurant to which it is related. From your blade code, it looks like you have a rest_id
field which stores the restaurant id for the offer.
Once you have the field added to the Offer table, you need to setup the relationships in your models, like so:
class Offer extends Model {
// second parameter is the field on the offer table
// third parameter is the id field on the restaurant table
public function restaurant() {
return belongsTo('\App\Restaurant', 'rest_id', 'id');
}
}
class Restaurant extends Model {
// second parameter is the field on the offer table
// third parameter is the id field on the restaur
public function offers() {
return hasMany('\App\Offer', 'rest_id', 'id');
}
}
Now, with the relationships setup properly, you can access the related restaurant information from the offer object:
$offer = Offer::first();
$restaurant = $offer->restaurant;
$restaurantName = $offer->restaurant->rest_name;
Do this same thing with the Offer Images and the Food Items and you should be set. Also, considering the way you will be accessing the data, it would be a good idea to eager load the related models, instead of lazy loading in the example above. For example:
$offer = Offer::with('restaurant', 'offerImage', 'foodItem')->first();
$restaurant = $offer->restaurant;
$restaurantName = $offer->restaurant->rest_name;
Hope this helps get you started, but it would be a good idea to read up on the relationship documentation.
edit due to comments
Fetching all rows works the same as fetching one row. The only difference is that you get a Collection of Models for all the rows, instead of just the one Model for one row. Note the use of the get()
method instead of the all()
method. all()
is a shortcut method on the Model that just creates a query builder object and calls get()
. Since you're modifying the query to run (eager loading the relationships), you get a query builder instance and call get()
on it to get the records.
So, your code would look like:
// get all offers, with their relationships eager loaded
$offers = Offer::with('restaurant', 'offerImage', 'foodItem')->get();
// loop through each offer; do what you need to do.
foreach($offers as $offer) {
echo $offer->restaurant->rest_name;
}
Upvotes: 7