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

Php 从laravel 5.1中的多个表中获取多行数据,php,mysql,laravel,laravel-5,laravel-5.1,Php,Mysql,Laravel,Laravel 5,Laravel 5.1,在使用Laravel5.1框架处理管理控制面板时,我被困在项目开发的中间 我无法从包含多行的多个表中获取数据。 这是我的设想 请检查这张图片,这是我的表格结构 [1] Offer Table - saves all offers 我想做的是,按照图中所示显示所有报价,但这对我来说相当困难 这是我的控制器代码 public function index() { $alldata=Offer::all(); //this line is correct $offerimage=

在使用Laravel5.1框架处理管理控制面板时,我被困在项目开发的中间

我无法从包含多行的多个表中获取数据。 这是我的设想

请检查这张图片,这是我的表格结构

[1] Offer Table - saves all offers

我想做的是,按照图中所示显示所有报价,但这对我来说相当困难

这是我的控制器代码

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'));
}
这是我的代码视图

@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
@foreach($alldata作为$data)
{{$data->offer_id}
{{$offerimage->img_filename}}!!}
{{$restaurant->rest_name}
{{$fooditem->item_name}
{{$data->offer_code}
{{$data->description}
{{$data->总价}
{{$data->disc_value}
{{$data->disc_percentage}
{{$data->status}

{!!Form::open(数组('route'=>['offer.destroy',$data->offer\u id],'method'=>'delete')) {!!Form::hidden('id',$data->offer\u id) {!!Form::submit('Delete',$attributes=array('class'=>'btn-btn-danger')) {!!Form::close()!!} @endforeach
在上面的控制器中,我如何编写代码,以便能够从offer表中获取所有行,以及从其他表中获取数据。我搜索了Laravel5.1文档和雄辩的关系,但我无法在任何关系中使用这种类型


非常感谢你的帮助

您需要将这些模型相互关联,以便根据需要进行操作。作为一个例子,让我们看一下报价与餐厅的关系

我假设一家餐厅可以与多个报价相关,但一个报价只能与一家餐厅相关。在这种情况下,你会说餐馆有很多优惠,而优惠属于餐馆

为了促进这种关系,您需要在报价表中添加一个字段,以存储与其相关的餐厅的id。从刀片代码看,似乎有一个
rest\u id
字段,用于存储报价的餐厅id

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

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');
    }
}
现在,在关系设置正确后,您可以从报价对象访问相关的餐厅信息:

$offer = Offer::first();
$restaurant = $offer->restaurant;
$restaurantName = $offer->restaurant->rest_name;
对提供的图片和食物做同样的事情,你应该被设置好。另外,考虑到访问数据的方式,最好是立即加载相关模型,而不是上面示例中的延迟加载。例如:

$offer = Offer::with('restaurant', 'offerImage', 'foodItem')->first();
$restaurant = $offer->restaurant;
$restaurantName = $offer->restaurant->rest_name;
希望这有助于你开始,但这将是一个好主意,阅读了

编辑 因评论而编辑

获取所有行的工作原理与获取一行的工作原理相同。唯一的区别是,所有行都有一个模型集合,而不是一行只有一个模型。注意使用了
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;
}

正如您所说的,我尝试了,但在上面的代码中,您正在获取表的第一行,在这里,我尝试从表中获取每一行。所以我有这样的代码$offer=offer::all();如何获取$offer=offer::all()将获取的每一行的剩余名称;你像上帝一样帮助了我。我喜欢它…谢谢你,一切都很好&我用你提供的解决方案轻松地学习了雄辩的关系概念。。。。
$offer = Offer::first();
$restaurant = $offer->restaurant;
$restaurantName = $offer->restaurant->rest_name;
$offer = Offer::with('restaurant', 'offerImage', 'foodItem')->first();
$restaurant = $offer->restaurant;
$restaurantName = $offer->restaurant->rest_name;
// 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;
}