duanchen7036 2017-11-13 08:53
浏览 63
已采纳

Laravel:检查变量是否从控制器传递

I'm trying to check if variable is being passed from controller but isn't working, the controller function looks like here:

public function editClient($id)
    {
        $client = Client::find($id);
       // $client_project = DB::table('client_project')->where('client_id',$id)->first()->project_id;
        //$project = DB::table('projects')->where('id',$client_project)->first();
        $client_projects = Client::find($id)->projects;
        return view('cms.public.views.clients.editclient', ['client' => $client, 'client_projects' => $client_projects]);

    }

In the client blade I show the projects of each one with this code:

<h3 class="h3client h3clienteedit">Proyectos de {{$client->name}}</h3>

          @foreach ($client_projects as $project)

            <div class="col-md-3 col3projectofclient">
              <h4 class="h4traductiontitle"><a href="/admin/project/{{$project->id}}/edit" class="atraductiontitle">{{$project->title}}</a></h4>
              <a href="/admin/project/{{$project->id}}/edit"><img src="{{ asset('/storage/projects/'.$project->slug.'/header.jpg') }}" class="imgprojectoneditclient"></a>
            </div>

@endforeach

But I only want to show it when have projects if not i don't want to show de .

I'm trying to do it with this code:

@if ( !empty($client_projects['slug']))

//code

@endif
  • 写回答

1条回答 默认 最新

  • dougaicha5258 2017-11-13 08:57
    关注

    pass the variable directly

    @if (!$client_projects)
    
    //code
    
    @endif
    

    or

    @if ($client_projects->count())
    
    //code
    
    @endif
    

    more info

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测