I have created a function in my HomeController.php beside the index method in Laravel 5.3. In the index method, there are some data retrieved from database, and the sent to the other method to be analyzed and sent to the home view to be shown.
My problem is that laravel displays a mere blank page instead of the actual home.blade.php view. Below is the code that I am using to retrieve data from database and send them to the view. Could you please tell me what am I doing wrong here?
public function index ()
{
$user_table_data = DB::table($table_name)->get();
$this->ChooseAction($user_table_data);
}
private function ChooseAction ($r_value)
{
foreach ($r_value[0] as $key => $value)
{
if ( $key !=='id' && $value !== '0' )
{
array_push($this->choose_action_result, $key);
}
}
if (!empty($this->choose_action_result))
{
return view('home', ['rvalue' => $this->choose_action_result]);
}else{
return view('home');
}
}
in your index method return the call of private value
public function index ()
{
$user_table_data = DB::table($table_name)->get();
return $this->ChooseAction($user_table_data);
}
mark the return keyword in the call statement if should return the view
This will be your code:
public function index ()
{
$user_table_data = DB::table($table_name)->get();
return $this->ChooseAction($user_table_data); //here you also need a return
}
private function ChooseAction ($r_value)
{
foreach ($r_value[0] as $key => $value)
{
if ( $key !=='id' && $value !== '0' )
{
array_push($this->choose_action_result, $key);
}
}
if (!empty($this->choose_action_result))
{
return view('home', ['rvalue' => $this->choose_action_result]);
}else{
return view('home');
}
}
You didn't return the view in your public function index() {} that you got from your private method.
Related
I am trying to display data through a query on Codeigniter, but return an error
"ATTEMPT TO READ PROPERTY "JENIS_PRODUK" ON STRING"
Here's my code on controller :
public function minimalist($id = 'minimalist')
{
$data["minimalis"] = $this->welcome_model->getMinimalis($id);
$this->load->view('frontend/minimalis.php', $data);
}
My code on Model :
public function getMinimalis($id)
{
return $this->db->get_where($this->_table, ["jenis_produk" => $id])->row();
}
On view I added foreach to display those data
Can you tell me what's wrong?
Please change this
public function getMinimalis($id)
{
return $this->db->get_where($this->_table, ["jenis_produk" => $id])->row();
}
To
public function getMinimalis($id)
{
return $this->db->get_where($this->_table, ["jenis_produk" => $id])->result();
}
I'm having a controller with some functions. In every function I get user data by sharing it from the Contoller.php
In controller.php
public function share_user_data() {
$user = Auth::user();
$this->checkValidation($user);
$this->user = $user;
View::share('user', $user);
}
public function checkValidation($user){
if($user->email_activated == 0){
var_dump($user->email_activated); // I get: int(0)
return redirect('/verifyEmail');
}
}
In the other controller
public function viewCategory(Category $category){
$this->share_user_data(); // here's the check
$this->get_site_lang();
$products = $category->products;
return view('category', compact('category','products'));
}
But I get the category view and not redirected to the verifyEmail route. How to fix this and why it's happening?
The controller function called by the route is the one responsible for the response. I guess it is viewCategory() in your example?
Your viewCategory() function is always returning view(). It must return redirect() instead. I think the main function should be responsible for picking the output of the request.
private function checkValidation($user) {
return $user->email_activated == 0;
}
public function viewCategory(Category $category) {
$user = Auth::user();
/* ... call to share_user_data() or whatever ... */
if ($this->checkValidation($user)) {
return redirect('/verifyEmail');
}
return view('category', compact('category','products'));
}
view
error
model:
function add_wliempmap($params)
{
foreach ($params as$row)
{
$this->db->insert('tbl_wli_peremp_map',$row);
$query= $this->db->insert_id();
// return$query->result();
}
}
controller:
public function submit()
{
$data['data']=array('wid' => $this->input->post('wid'),'eid' => $this->input->post('eid')
);
$this->Wliempmap_model->add_wliempmap($data);
// var_dump($_REQUEST);
// exit;
}
Above model $row variable have array value I want to store this variable value in database how to pass this variable in controller multiple value with example ?
Change your code as per bellow code:
Model:
public function add_wliempmap($params)
{
$this->db->insert('tbl_wli_peremp_map',$params);
return $this->db->insert_id();
}
Controller:
public function submit()
{
$data = array('wid' => $this->input->post('wid'),'eid' => $this->input->post('eid'));
$this->Wliempmap_model->add_wliempmap($data);
}
For details info:
https://www.codeigniter.com/user_guide/database/query_builder.html#inserting-data
I'm trying to create a callback to return my views based on data from my current logged-in user. If I do something basic like echoing 'hi' it works, is there any way accomplish this?
function checkUser($type,$callback){
if( is_callable($callback) ){
call_user_func($callback);
}
}
class FichaController extends Controller
{
public function contarFichas()
{
checkUser('particular',function(){
$currentUser = Auth::user();
$countFichas = Ficha::where('user_id',$currentUser->id)->count();
return view('particular.index', array('countFichas' => $countFichas));
});
}
}
Return the result from checkUser
if( is_callable($callback) ){
return $callback();
}
public function contarFichas()
{
return checkUser('particular',function(){
$currentUser = Auth::user();
$countFichas = Ficha::where('user_id',$currentUser->id)->count();
return view('particular.index', array('countFichas' => $countFichas));
});
}
I want to do something I don't know if it is feasible.
here's my routes.php
Route::get('mockups/user/skills/{skill}', 'MockupsController#user');
Route::get('mockups/user/tours/{tour}', 'MockupsController#user');
Route::get('mockups/user', 'MockupsController#user');
and my MockupsController#user function
public function user($skill=null,$tour=null){
if($tour ==null && $skill != null)
return View::make('demo/mockups/user/public',array('skill'=>$skill));
if($tour!=null && $skill ==null)
return View::make('demo/mockups/user/public',array('tour'=>$tour));
return View::make('demo/mockups/user/public');
}
if I get the /mockups/user/tours/tour1 url, the controller calls the demo/mockups/user/public view without sending the $tour variable. how to make it works?
EDIT
public function user($skill=null,$tour=null){
echo var_dump($skill);
echo var_dump($tour);
die();
if($tour ==null && $skill != null)
return View::make('demo/mockups/user/public',array('skill'=>$skill));
if($tour!=null && $skill ==null)
return View::make('demo/mockups/user/public',array('tour'=>$tour));
return View::make('demo/mockups/user/public');
}
displays
string(5) "tour1" NULL
I dont understand why you are making it so complicated. Why not just do
Route::get('mockups/user/skills/{skill}', 'MockupsController#skills');
Route::get('mockups/user/tours/{tour}', 'MockupsController#tours');
Route::get('mockups/user', 'MockupsController#user');
public function skills($skill)
{
return View::make('demo/mockups/user/public',array('skill'=>$skill));
}
public function tours($tour)
{
return View::make('demo/mockups/user/public',array('tour'=>$tour));
}
public function user()
{
return View::make('demo/mockups/user/public');
}