dongxiang3648 2019-04-29 10:38
浏览 78

如何修复laravel 5.7中未定义的变量ID错误

M working on a solution where by i need to pass data from controller to view, Based on the id.

I've tested each variable one by one for see if there is actual data contained in those variables.

one-by-one produces all the values required and as soon as i comment out the var_dumps(). Throws an Undefined index error.

Please See code below:

View

<td>
   <a href="view-campaign/{{$item->id}}" class="btn btn-success mb-2"
      data-toggle="tooltip" title="view campaign">                            
        <i class="fa fa-eye"></i>
   </a>
</td>

Controller

public function viewCampaign($id){

        //return var_dump($id);

        $img = null;

        //firebase configs and send to firebase
        $serviceAccount = ServiceAccount::fromJsonFile(__DIR__.'/serviceKey.json');
        $firebase = (new Factory)
            ->withServiceAccount($serviceAccount)
            ->withDatabaseUri('https://projectName.firebaseio.com/')
            ->create();

            $database = $firebase->getDatabase();

            $ref = $database->getReference('CampaignCollection')->getValue();

            foreach($ref as $key){
                $item = $key['id'];
                //return var_dump($item); 
                $poster = $key['Poster'];
                //return var_dump($poster); 
                if($item = $id){ 

                    //return '1';
                    $img = $poster; 
                    //return var_dump($poster);
                }else{
                    return '0';
                }
             }
        return view('view-campaign')->with('img',$img);
    }

Route

Route::get('view-campaign/{id}','CampaignController@viewCampaign');

View::Results

@extends('layouts.layout')

@section('content')
<div class="col-md-12">
        <div class="col-md-12 panel">
            <div class="col-md-12 panel-heading">
                <h4>View Campaign:</h4>
            </div>
            <div id="imgContainer" class="col-md-12 panel-body">
               <a href="/listCampaign" class="btn btn-danger mb-2" style="margin-bottom: 15px"><i class="fa fa-arrow-circle-left"></i></a>
               @if(isset($img))
                  <div  align="center">
                    <img src="{{($img)}}" />
                  </div>
                @else
                  no data 
                @endif

            </div>    
        </div>
</div>
@endsection

Goal is to get the base64 code to pass to the view.

  • 写回答

1条回答 默认 最新

  • duanjizi9443 2019-04-29 13:04
    关注

    Try replacing your foreach with the following code:

    foreach($ref as $k1 => $key){
                    $item = $key->id; //change over here
                    //return var_dump($item); 
                    $poster = $key->Poster;  //change over here
                    //return var_dump($poster); 
                    if($item == $id){ //change over here
    
                        //return '1';
                        $img = $poster; 
                        //return var_dump($poster);
                    }else{
                        return '0';
                    }
                 }
    

    I reckon, you would also have to update the function signature to look something like following:

    public function viewCampaign(Request $request , $id){
                //your code
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来