Return a variable to a view - php

I have a question about Laravel.
I have by example index.blade.php. In this file I want include the sidebar who is located on the file sidebar.blade.php.
But now I want to send a variable from my controller(SidebarController) to the sidebar view: sidebar.blade.php.
Like this:
class SidebarController extends Controller
{
public function news_widget() {
$posts = Post::take(5)->orderBy('updated_at', 'DESC')->take();
}
}
How can I do that?

You can use following
class SidebarController extends Controller
{
public function news_widget() {
$posts = Post::take(5)->orderBy('updated_at', 'DESC')->take();
return view('index', array('data'=>$posts));
}
}
And within index.blade.php include sidebar.blade.php
#include('sidebar',$data)

Try this
class SidebarController extends Controller
{
public function news_widget() {
$posts = Post::take(5)->orderBy('updated_at', 'DESC')->take();
return View::make('sidebar')->with('posts', $posts);
}
}

Related

How to pass two functions in the same route in Laravel 8?

i have this route:
web.php:
Route::get('main',[HomePageController::class,'show']);
that pass this function:
HomePageController.php
class HomePageController extends Controller
{
function show()
{
$data = Classes::all();
return view('index',['classes'=>$data]);
}
}
and i want to pass another function in the same route but i keep getting "Undefined variable"
whenever i try to do this:
web.php
Route::get('main',[HomePageController::class,'show']);
Route::get('main',[HomePageController::class,'showfeeds']);
HomePageController.php
class HomePageController extends Controller
{
function show()
{
$data = Classes::all();
return view('index',['classes'=>$data]);
}
function showfeeds()
{
$data = Feeds::all();
return view('index',['feeds'=>$data]);
}
}
what am i doing wrong here?
You could use single route but send Classes and Feeds to single view (as you intended):
Route:
Route::get('main',[HomePageController::class,'show']);
Controller:
class HomePageController extends Controller
{
function show()
{
$classes = Classes::all();
$feeds = Feeds::all();
return view('index', compact('classes', 'feeds'));
}
}

How to concatenate two variables in a controller class and pass it to all views of the class in laravel

I'm trying to concatenate two class spesific variables in controller and pass it to all views without repeating the same variable in every controller methods.
Example code:
class ProductsController extends Controller
{
private $global_path; //Comes from .env
private $sub_folder = '/products_folder';
public function __construct()
{
//Frontend Image Path - to pass into all views
$frontend_path = $this->global_path.$this->sub_folder;
}
}
I want to pass '$frontend_path' to all blade views created in the controller without repeating it in every single method like
return view('example_view', compact('frontend_path');
I tried View::share... but couldn't do it.
The '$sub_folder' variable doesn't have the same value in all controllers.
Is there a way to make it possible?
For your code, I think you can change it to
class ProductsController extends Controller
{
public $frontend_path;
public function __construct() {
$this->frontend_path = env('GLOBAL_PATH') . '/products_folder';
}
public function index()
{
$x = $this->frontend_path;
return view('index', compact('x'));
}
}
and directly use it like $this->frontend_path or like below SELF::$frontend_path
class ProductsController extends Controller
{
public static $frontend_path;
public function __construct() {
SELF::$frontend_path = env('GLOBAL_PATH') . '/products_folder';
}
public function index()
{
$x = SELF::$frontend_path;
return view('index', compact('x'));
}
}
or
class ProductsController extends Controller
{
public static $frontend_path;
public function __construct() {
SELF::$frontend_path = env('GLOBAL_PATH') . '/products_folder';
view()->share('frontend_path', SELF::$frontend_path);
}
public function index()
{
return view('index');
}
}
in view
{{ $frontend_path }}

Laravel, is there a way to include codes from resource/view path into controller?

My goal is to include codes from another sources which is located in resources/views. I have tried using resource_path('views/myfiles.php') but it does nothing.
Controller
class MyController extends Controller
{
public function test(Request $request)
{
if($request->input('name') == "chair")
{
$theFilesLocation = "resources.views" . $request->input('name');
#include($theFilesLocation) //something like this
}
}
}
myfiles.php
<?php
dump("if this shows up, then the code works")
?>
Try bellow code but I think it is not a good way.
class MyController extends Controller
{
require_one(resource_path('views/myfile');
}
Or with Laravel File facade
class MyController extends Controller
{
\File::requireOnce(resource_path('views/myfile');
}
You should create a class and put your code there then call it from the controller is a better solution.
What you are looking for is a trait. This allows the easy sharing of code and functionality without having to inherit from a specific base class causing an inheritance hell.
namespace MyCode\Traits;
trait SharedCodeForThing {
public function blaTheBla() {
dump("if this shows up, then the code works");
}
}
and then in your controller
use MyCode\Traits\SharedCodeForThing ;
class MyController extends Controller
{
use SharedCodeForThing;
}
Now if you wish to just render the contents of the view which it seems you're after:
public function test(Request $request)
{
if($request->input('name') == "chair")
{
$view = view('resources.views' . $request->input('name'));
return $view->render();//or echo $view->render(); whatever you like
}
}

codeigniter global array for a model

In my controller I want to declare a global variable which will always fetch all areas from my db
currently I'm passing $data['dropdowns']
in all methods in my class
{
$data['dropdowns']=loading some other model method
$this->load->view('commons/header',$data);
}
{
$data['dropdowns']=loading some other model metod
$this->load->view('commons/header',$data);
}
{
$data['dropdowns']=loading some other model metod
$this->load->view('commons/header',$data);
}
{
$data['dropdowns']=loading some other model metod
$this->load->view('commons/header',$data);
}
the thing is I want to now send $data['area'] to all the views without having to declare it again and again in each method
$data['area']= $this->area_model->get_all_locations();
You want to add global variable , but as per my suggest to use global function to use any where to using to send parameter, so please check below my code.
Note : please load your model in application/config/autoload.php file
This is simple demo :
controller
{
$data['dropdowns']=$this->your_model_name->get_records('table_name','select field like id, name');
$this->load->view('commons/header',$data);
}
{
$data['dropdowns']=$this->your_model_name->get_records('table_name','select field like id, name,user_name');
$this->load->view('commons/header',$data);
}
Your model
function get_records($table_name,$field_name)
{
$this->db->select("$field_name");
$this->db->from("$table_name");
$query=$this->db->get();
return $query->result_array();
}
create a base_controller and placed in application/core
class base_controller extends CI_Controller
{
public $area = array();
function __construct()
{
// Call the Controller constructor
parent::__construct();
$this->get_area();
}
public function get_area() {
$this->load->model('area_model');
$this->area= $this->area_model->get_all_locations();
}
}
now $this->area is available in all controller which extends base_controller and all common functionality you can put here
class homepage extends base_controller{
function __construct()
{
// Call the Controller constructor
parent::__construct();
}
public function index()
{
$data = $this->area; // call this wherever u need
$this->load->view('commons/header',$data);
}
}
importantly $this->area; one can use directly inside view
Create a helper for your custom functions
Eg: custom_helper.php then load your custom helper in autoload.php
In custom_helper.php, create a function for getting area.
if (!function_exists('get_area')) {
function get_area() {
$CI = & get_instance();
$area= $CI->area_model->get_all_locations();
return $area;
}
}
You can call get_area() in your views without declaring in controllers..

How to access a controller method from view in CodeIgniter?

I want to do something like this in CodeIgniter:
Controller:
class Home extends CI_Controller {
public function index() {
$this->load->view('home');
}
public function user($user) {
return $user;
}
}
View (home):
Hi <?php echo user('User') ?> // call a controller method
How can i do this?
One way to get it is , returning it from the routes.php ,
Like $route['user'] = "home/user"; And this will execute the user method from the home controller.

Categories