从laravel 5.1中的多个表中获取多个行数据

dol*_*lar 2 php mysql laravel laravel-5 laravel-5.1

在使用laravel 5.1框架处理管理控制面板时,我陷入了项目开发的中期.

我无法从多行的多个表中获取数据.这是我的情景

请检查这张图片是我的桌子结构.

[1] Offer Table - saves all offers
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

[2] Restaurant Table - Saves all restaurant information
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

[3] OfferImage Table - Saves all offers images
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

[4] FoodItem Table - saves all food items
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我想要做的是,显示图1中显示的所有优惠,但这样做对我来说非常困难.

这是我的控制器代码

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'));
}
Run Code Online (Sandbox Code Playgroud)

这是我的代码

@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
Run Code Online (Sandbox Code Playgroud)

在上面的控制器中,我如何编写代码,以便我可以从offer表中获取所有行,以及来自其他表的数据.我搜索了Laravel 5.1文档和雄辩的关系,但我无法在任何关系中使用这种类型.

帮助真的很感激.

pat*_*cus 7

您需要将这些模型相互关联,以便按照您的需要进行操作.举个例子,让我们看一下Offer - Restaurant的关系.

我假设餐厅可以与许多优惠相关,但优惠只能与一家餐厅相关.在这种情况下,你会说餐厅有许多优惠,并提供belongsTo餐厅.

为了促进这种关系,您需要在Offer表中添加一个字段,以存储与其相关的餐馆的ID.从您的刀片代码中,您看起来有一个rest_id字段存储该优惠的餐馆ID.

将该字段添加到Offer表后,您需要在模型中设置关系,如下所示:

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');
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,通过正确设置关系,您可以从商品对象访问相关的餐馆信息:

$offer = Offer::first();
$restaurant = $offer->restaurant;
$restaurantName = $offer->restaurant->rest_name;
Run Code Online (Sandbox Code Playgroud)

使用优惠图片和食品项目做同样的事情,你应该设置.此外,考虑到您将访问数据的方式,最好是在上面的示例中急切加载相关模型,而不是延迟加载.例如:

$offer = Offer::with('restaurant', 'offerImage', 'foodItem')->first();
$restaurant = $offer->restaurant;
$restaurantName = $offer->restaurant->rest_name;
Run Code Online (Sandbox Code Playgroud)

希望这有助于您入门,但阅读关系文档是一个好主意.

编辑

由于评论而编辑

获取所有行的工作方式与获取一行相同.唯一的区别是你获得了所有行的模型集合,而不是一行的一个模型.注意使用该get()方法而不是all()方法.all()是模型上的一个快捷方法,它只是创建一个查询构建器对象和调用get().由于您正在修改要运行的查询(急切加载关系),因此您将获得一个查询构建器实例并调用get()它来获取记录.

所以,你的代码看起来像:

// 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;
}
Run Code Online (Sandbox Code Playgroud)