How to Share functions in same controller in one view - laravel

Hi i have a Controller called homeController and a view called home.blade.php (home). I have 2 functions in my homeController that i want to use in my view 'home'.
The function called index() , works fine, but how do i get the values from function userchart() to my view 'home'.
public function index(){
$value1 = '';
$value2 = '';
return view('home', compact('$value1', 'value2'));
}
public function userchart(){
$value3 = '';
$value4 = '';
return view('home', compact('$value3', 'value4'));
}
Now in my VIEW i can only access $value1 and $value2
foreach($value1 as $key){
echo $key->value;
}
How do i get access to the data '$value3 and $value4'

public function index(){
$data['value1'] = 'value1';
$data['value2'] = 'value2';
$data = array_merge($data, $this->userchart());
return view('home', $data);
}
public function userchart(){
$data['value3'] = 'value3';
$data['value4'] = 'value4';
return $data;
}

Related

Codeigniter: Display an image stored in DB

How do I get an image stored in a field of my database, and put it into a variable for using it in an email message like a signature?
Here is my code:
//Model user_model
function getImage(){
$this->db->select("image");
$this->db->where("$id", user_id);
$query = $this->db->get("users");
if($query->num_rows()>0){
return result();
}else{
return false;
}
}
//Controller user_controller
function image(){
this->load->model("user_model");
$id = $this->session->userdata('id');
$image = $this->user_model->getImage($id);
echo $image;
}
Here is a example
Model user_model
// Pass the id function
function getImage($id){
$this->db->where($id, 'user_id');
$query = $this->db->get("users");
if($query->num_rows()>0){
return $query->row_array();
}else{
return false;
}
}
Controller user_controller
function image(){
$this->load->model("user_model");
$somedata = $this->user_model->getImage($this->session->userdata('id'));
$data['image'] = $somedata['image'];
$this->load->view('someview', $data);
}
On view
<?php echo $image;?>

Laravel 5.4 Add a difference between views

I use the same view to show one post and random post
routes
Route::get('posts/{id}', 'PostsController#show')->name('posts.show');
Route::get('get-random-post', 'PostsController#getRandomPost');
methods in PostsController
public function show($id) {
$post = Post::findOrFail($id);
return view('posts.show', compact('post'));
}
public function getRandomPost() {
$post = Post::inRandomOrder()
->where('is_published', 1)->first();
return redirect()->route('posts.show', ["id" => $post->id]);
}
but now I need to add a small difference between two views. How can I do that?
UPD
I added variable $randomPost to methods in Controller
public function show($id) {
$randomPost = false;
$post = Post::findOrFail($id);
return view('posts.show', compact('post', 'randomPost'));
}
public function getRandomPost() {
$randomPost = true;
$post = Post::inRandomOrder()
->where('is_published', 1)->first();
return redirect()->route('posts.show', ["id" => $post->id]);
}
and added code below to show view
#if($randomPost)
some text
#endif
but I don't know how to pass variable from getRandomPost() to view?
UPD2
As result I used session, it works but I'm not sure about it
method
public function getRandomPost() {
$post = Post::inRandomOrder()
->where('is_published', 1)->first();
session()->flash('random_post', 'ok');
return redirect()->route('posts.show', ["id" => $post->id]);
}
view
#extends('layouts.app')
#section('content')
Home page
<h2>#{{$post->id}}</h2>
{!! nl2br(e($post->text)) !!}
<?php if(session()->has('random_post')){
echo '<div style="text-align: center">';
echo link_to_action('PostsController#getRandomPost', 'Random Post', $parameters = array(), $attributes = array());
echo '</div>';
}?>
#stop
You can use session flash, it lasts only on subsequent request:
// set
session()->flash('random_post', 'ok');
// check
if(session()->has('random_post')){
// is random
I guess the easiest way would be to call the function from the getRandomPost by passing a default variable through.
public function show($id, $randomPost = false) {
$post = Post::findOrFail($id);
return view('posts.show', compact('post', 'randomPost'));
}
public function getRandomPost() {
$post = Post::inRandomOrder()->where('is_published', 1)->first();
$this->show($post->id, true);
}

Codeigniter function_exists not working correctly

I am using php function_exists() function exist on my Welcome controller. But for some reason it keeps on throwing my show_error even though my slideshow function exists.
With in my foreach loop I get module function name from database which in the foreach loop is called $function = $module['code'];
Question is: How am I able to make sure function_exists checks
function exists correctly?
<?php
class Welcome extends CI_Controller {
public function index() {
$data['content_top'] = $this->content_top();
$this->load->view('home', $data);
}
public function content_top() {
$data['modules'] = array();
$modules = $this->get_module();
foreach ($modules as $module) {
$function = $module['code'];
if (function_exists($function)) {
$setting_info = array('test' => 'testing');
if ($setting_info) {
$data['modules'][] = $this->$function($setting_info);
}
} else {
show_error('This ' . $function . ' does not exist on ' . __CLASS__ . ' controller!');
}
}
return $this->load->view('content_top', $data, TRUE);
}
public function banner() {
}
public function slideshow($setting) {
$data['test'] = $setting['test'];
$this->load->view('module/slideshow', $data);
}
public function get_module() {
$query = $this->db->get('modules');
return $query->result_array();
}
}
function_exists() works on functions, but not class methods - these are different things. What you want is method_exists():
method_exists($this, $function);

Explain the functionality or meaning of this : $data['row']

I have this doubt.
// This is my controller
public function profile()
{
$session_data = $this->session->userdata('logged_in');
$uid= $session_data['id'];
$pic = $this->user_model->getImage($uid);
$data['profile_pic']= $pic ;
$data['row'] = $this->user_model->get_user_data($uid);
$test = $this->load->view('profile_view',$data,TRUE);
echo $test;exit;
}
Based on name it seems to be:
public function profile()
{
$session_data = $this->session->userdata('logged_in');//checking user login or not
$uid= $session_data['id'];//getting id from session
$pic = $this->user_model->getImage($uid);//geting user image
$data['profile_pic']= $pic ;//storing image to data array
$data['row'] = $this->user_model->get_user_data($uid);//passing $uid to model and store in data array what row model returns
$test = $this->load->view('profile_view',$data,TRUE);//passing data array to view
echo $test;exit;
}
//model
i am passing user id only
public function get_user_data($uid){
//echo $uid;exit;
$data = array();
$this->db->select('*');
$this->db->from('user');
$this->db->where('id',$uid);
$query = $this->db->get();//checking that $uid data in database
if($query->num_rows() > 0){
$get_user_data=$query->result();
//print_r($get_user_data);exit;
//print_r($items);exit;
return $get_user_data; // if find, return data to controller
}
else{
return FALSE;
}
//return $query->row();
}

CodeIgniter pass variables form controller to model

Ok I want to pass two variables from a controller to a model but I get some kind of error. Am I passing variables on right way? My syntax is:
Controller:
public function add_tag(){
if(isset($_POST['id_slike']) && isset($_POST['id_taga'])){
$slika = $_POST['id_slike'];
$tag = $_POST['id_taga'];
$this->load->model("Member_model");
$res = $this->Member_model->add_tags($slike, $tag);
foreach ($res->result() as $r){
echo $r->name;
}
}
else{
echo "";
}
}
Model:
public function add_tags(){
$data = array(
'tags_id' => $tag ,
'photos_id' => $slika
);
$check = $this->db->query("SELECT tags_id,photos_id FROM bridge WHERE bridge.tags_id='{$tag}' AND bridge.photos_id={$slika} ");
if($check->num_rows()==0){
$this->db->insert('bridge',$data);
$res = $this->db->query("SELECT name FROM tags where `tags`.`id`='{$tag}' ");
return $res;
}
}
you are passing variables correctly, but do not get them correctly in the model, which should look like this:
public function add_tags($slike, $tag){
//your other code
}
The following code write on the controller file:-
$data = array();
$this->load->model('dbmodel');
$data['item'] = $this->dbmodel->getData('*','catagory',array('cat_id'=>21));
$this->load->view('listing_view', $data);
The following code write on the dbmodel file:-
public function getData($cols, $table, $where=array()){
$this->db->select($cols);
$this->db->from($table);
$this->db->where($where);
$query = $this->db->get();
$result = $query->result();
return $result;}

Resources