drt5813 2019-02-12 03:14
浏览 45

使用Eloquent模型创建基本模型以在View中呈现

I have created a ViewModel which has two Eloquent models as properties. I want to use the ViewModel with two non-Eloquent models with the same properties as the table / eloquent models used to populate mock-up objects as properties of the ViewModel after instantiation.

The idea is that I can create this ViewModel "on-the-fly" and its properties aka Eloquent Models.

Is and How is it possible to create models that use the pre-defined eloquent models in my project/package without actually having any relationship to the table besides it's schema::table properties? The ImageItem and PageInfo Models are Eloquent models and are used as directed, but I want to use their class templates to create dynamic objects that are not relational to the database. I'm going to scrap these objects out once the page is rendered they're gone.

The problem I am having is the View doesnt identify the object in its current state

Here is an example of my code:

Basic view model that gets passed to view as compact('page')

class LandingPageViewModel extends Model
{
     ImageItem $img;
     PageInfo $info;

   __construct($img = NULL, $info = NULL){
             if(!$img && !$info){
                $img = ImageItem::where("key","=","notfound").first();
                $info = PageInfo::where("key","=","notfound").first();
             }
             else{
                  $this->img = $img;
                  $this->info = $info;
            }
    }
}

This is where is gets a little blurry for me..These are two Eloquent models but in this case I only want to use them as templates. Meaning I wil not save these objects to the database. Think of them as child ViewModels at this point similar to how ASP.NET MVC works

public ImageItem(){
   __construct(){
         parent::__construct($attributes);
    }
}

public PageInfo(){
   __construct(){
         parent::__construct($attributes);
    }

}

In the controller let's say I have a function that creates an error page based off error codes and I return the LandingPageViewModel to the shared view that contains non-eloquent objects

function getErrorPage($params){

//do some looping through params and assign value to properties based on key/column names of eloquent models stored in my db 
$mockImage = new ImageItem();
$mockInfo = new PageInfo();
foreach($params as $k => $v){
   if($k == `***Schema::getColumnListing('image_item')***`){ //image_item is table name
       $mockImage->column['name'] = $v;
    }
 etc....
 $page = new LandingViewModel($mockImage,$mockInfo);
return view("landing.index",compact('page');
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?
    • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
    • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
    • ¥15 cmd cl 0x000007b
    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
    • ¥500 火焰左右视图、视差(基于双目相机)
    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染