Jack The Baker
Jack The Baker

Reputation: 1883

Laravel use a controller in blade

I just start to learning Laravel, I stuck in a part, I want to get data from database and pass it to a blade (view) file then use this view in another view file, and want to pass one variable to a controller but I got this error:

"Class 'adsController' not found"

web.php

Route::get('seller/panel', 'sellerController@panel');

panel.blade.php

@include('seller\hook\ads')

sellerController.php

public function panel(){
    $ads = DB::table('ads')->get();
    return view('hook\ads', ['ads' => $ads]);
}

adsController.php

class adsController extends Controller {
  public function getStatus($p) {

    if ($p == 'something') {
      $status = 'yeah';
    } else {
      $status = 'blah blahe';
    }
    return view('hook\ads', $status);
  }
}

ads.blade.php

<div class="ads">
@foreach ($ads as $ad)
{{ $ad->name }}
{{ adsController::getStatus($ad->approved) }}
@endforeach
</div>

So, as you see I am tring to get data from database in sellerController.php then pass it to ads.blade.php then I want to use adsController.php 's function in ads.blade.php, but it can't find adsController

Sorry I am newbie to laravel

Upvotes: 0

Views: 10071

Answers (4)

Juha Vehnia
Juha Vehnia

Reputation: 1414

Most often when you get this error when your namespace declaration in the controller is wrong. Typical scenario is where you generate controller stub with Artisan and then move it somewhere else from the default location.

In some cases you need to run:

composer dumpautoload

To generate new optimized class loader map.

Upvotes: 0

Cl&#233;ment Baconnier
Cl&#233;ment Baconnier

Reputation: 6058

As everyone said, it's not recommended to call the controller from the view. Here's what I would do :

In your model :

<?php
namespace App;

use Illuminate\Database\Eloquent\Model;

class Ad extends Model{
    public function getStatus()
    {
        return $this->approved == 'something' ? 'yeah' : 'blah blaehe';
    }
}

In your view :

<div class="ads">
    @foreach ($ads as $ad)
        {{ $ad->name }}
        @include('hook.ads', ['status' => $ad->getStatus()])
    @endforeach
</div>

In your controller :

public function panel(){
    $ads = \App\Ad::all();
    return view('hook\ads', ['ads' => $ads]);
}

Upvotes: 1

HexaCrop
HexaCrop

Reputation: 4263

At the beginning of the blade file you could include the following

@php
    use App\Http\Controllers\adsController as adsController;
@endphp

then on your blade template you could use the controller as you have used here. But it is a really bad habit to use one directly.

Since you are a newbie to Laravel change that coding practice.

You could use a service provider of a sort if you want that data which needs to be shown on a particular blade view all the time.

Upvotes: 1

Achraf Khouadja
Achraf Khouadja

Reputation: 6279

You should try this despite I don't recommend the approach you are trying to achieve.

{!! app('App\Http\Controllers\adsController')->getStatus($ad->approved) !!}

It should work but that's very wrong.

Upvotes: 0

Related Questions