1

I want to get data from a controller in my view to use in one of the div's

My TagController is like:

 class TagController extends Controller
{
//
    public function index(){
        $tags = Tag::all();
        return $tags;
    }
}

My view includes the following code to access this

{{ $tags = new App\Http\Controllers\TagController,
                        $tags->index()
}}

->can't use the keyword use because eloquent don't allow that ->Tried to make the function static and used TagController::index() , gave error can't make function static -> can't use this controller to route and redirect with the function because the view is already linked to another controller and i don't want to reload How can I use this function from controller. Sorry for this silly thing, Please help out, I am new to laravel

AshFixed
  • 70
  • 1
  • 10
  • `{{ }}` echo's with `htmlspecialchars` (therefore it expects a string), yet you are passing `Tag::all()` which is returning an object of all the tags therefore it is failing. What are you actually trying to do? https://stackoverflow.com/questions/34587457/difference-between-eloquent-modelget-and-all – Script47 Oct 11 '18 at 08:10
  • 1
    You could just `return view('tags.index', ['tags' => $tags]);` and access `$tags` in your view. – brombeer Oct 11 '18 at 08:12
  • I know {{ }} is for echo, All i want is to access my TagController's index function that is bringing data from database. Then use that data as value in forms – AshFixed Oct 11 '18 at 08:12
  • 1
    @AshFixed if you know it is for `echo` then why would you try to echo an object? Secondly, if you want to assign variables in blade you use the `@php @endphp` tags and put your PHP within them. Nevertheless, to achieve what you want you can do what kerbholz suggested, that would be the cleanest. – Script47 Oct 11 '18 at 08:13
  • @kerbholz my controller is already connected with controller(FactController), this is just to get data from another controller(TagController) for a div – AshFixed Oct 11 '18 at 08:14
  • 1
    I find it a strange concept of accessing Controller methods in your view, that method looks like it belongs in your model. Personally I would add `$tags = Tag::all();` to your `FactController` method and pass it to your view. – brombeer Oct 11 '18 at 08:20
  • @kerbholz Actually i am making something like a create post page that on the basis of choice of dropdown chooses controller – AshFixed Oct 11 '18 at 08:38

1 Answers1

2

As mentioned in the comments, to actually set a variable within your template file, you need to use the correct blade tags:

@php
     $tags = new App\Http\Controllers\TagController;

     $tags->index()
@endphp

As it currently stands, you are trying to set a variable within echo.

As per the documentation:

In some situations, it's useful to embed PHP code into your views. You can use the Blade @php directive to execute a block of plain PHP within your template.

However...

In all honesty, this is not the ideal way of doing this. You should not be accessing another controller within another view, you should in fact be passing the variable from the respective controller of the view as suggested by @kerbholz in their comment.

Script47
  • 14,230
  • 4
  • 45
  • 66