laravel 4 - two routes on same controller function - php

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');
}

Related

Laravel 5 return redirect is not working as it should

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'));
}

Laravel - Callable returning blank screen

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));
});
}

Can't get hasmany tables in laravel 5.1 eloquent

I am having problem in getting the hasmany relationship data in Laravel eloquent.
AModel hasmany BModel
AModel hasmany CModel
AModel has function:
Public function bmodels(){
return hasMany('BModel');
}
Public function cmodels(){
return hasMany('CModel');
}
BModel has function:
Public function bmodels(){
return belongsTo('AModel', amodel_id);
}
CModel has function:
Public function cmodels(){
return belongsTo('AModel',amodel_id);
}
Now i am trying to get it like this
$amodels = AModel::with('bmodels','cmodels')
->where('status_id','2200')
->whereIn('eventstatus_id',['1','2'])
->get();
and now i want to test this in the for loop.
foreach($amodels as $amodel){
$bmodels = $model->bmodels();
if ($amodel && $amodel->end < Carbon::now()){
foreach ($bmodels as $bmodel){
$cmodels = $amodel->cmodels();
foreach($cmodels as $cmodel){
if ($bmodel->id !== $cmodel->reservation_id || $cmodel->reservation_id != null ){
array_push($reservation_needed_to_enter,$bmodel->id);
}
}
}
}
}
You should make changes in your Models, use $this to use belongsTo and hasMany,like
public function bmodels(){
return $this->belongsTo('App\AModel','amodel_id');
}
public function bmodels(){
return $this->hasMany('App\BModel');
}
Just Edited your foreach loop, use $amodel->bmodels instead of $amodel->bmodels()
foreach($amodels as $amodel){
$bmodels = $amodel->bmodels;
if ($amodel && $amodel->end < Carbon::now()){
foreach ($bmodels as $bmodel){
$cmodels = $amodel->cmodels;
foreach($cmodels as $cmodel){
if ($bmodel->id !== $cmodel->reservation_id || $cmodel->reservation_id != null ){
array_push($reservation_needed_to_enter,$bmodel->id);
}
}
}
}
}
replace this two functions in your AModel
Public function bmodels(){
return hasMany('BModel');
}
Public function cmodels(){
return hasMany('CModel');
}
with
public function bmodels(){
return $this->hasMany('App\BModel');
}
public function cmodels(){
return $this->hasMany('App\CModel');
}
and these functions in BMOdel and CModel as
public function bmodels(){
return $this->belongsTo('App\AModel','amodel_id');
}
public function cmodels(){
return $this->belongsTo('App\AModel','amodel_id');
}
If your models are stored at the default directory you should probably do this instead:
public function bmodels()
{
return $this->hasMany('App\BModel');
}
Do you have a function in AModel named cmodels if not this could be the problem. Because you are trying to get AModel::with('bmodels','cmodels') but if one of those isn't there it is not going to work.
And this if ($amodel && $amodel->end < Carbon::now()){ doesn't make sense to me. Because $amodel will alwasy be an instance of Eloquent\Model and never false or whatever you are trying to check there. But maybe it has a purpose..
How to accces your BModel in your .blade file:
#foreach($aModel->bmodels() as $bModel)
{{$bModel->id}}
#endforeach()

Laravel shows blank page as the view

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.

In Laravel how can I replace a controller/method/id with just /slug in the URL?

What is the best way I could replace /controller/method/id in the URL with just /slug?
For example: trips/1 would become /honduras-trip
This is what I am doing but couldn't their be a better way?
My routes:
Route::get('/{slug}', 'HomeController#show');
My Controller:
public function show($slug)
{
$class = Slug::where('name', '=', $slug)->firstOrFail();
if($class->slugable_type == 'Trip')
{
$trip = Trip::find($class->slugable_id);
return $trip;
}
if($class->slugable_type == 'Project')
{
$project = Project::find($class->slugable_id);
return $project;
}
if($class->slugable_type == 'User')
{
$user = User::find($class->slugable_id);
return $user;
}
}
My Slug Model:
class Slug extends Eloquent {
public function slugable()
{
return $this->morphTo();
}
}
The other models all have this method:
public function slugs()
{
return $this->morphMany('Slug', 'slugable');
}
In your routes.php just give
Route::get('slug', array('uses' => 'HomeController#show'));
In your controller, write show() function
public function show() {
return View::make('welcome');
}
In your view give,
<li>slug</li>

Categories