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

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

报告相同问题?