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

时间:2017-11-13 08:53:31

标签: php laravel

我正在尝试检查变量是否从控制器传递但是不起作用,控制器函数如下所示:

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]);

    }

在客户端刀片中,我使用以下代码显示每个项目的项目:

<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

但我只想在有项目时展示它,如果不是我不想展示de。

我正在尝试使用此代码:

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

//code

@endif

1 个答案:

答案 0 :(得分:1)

直接传递变量

@if (!$client_projects)

//code

@endif

@if ($client_projects->count())

//code

@endif

more info