How to write post response in Laravel? - laravel

How to convert this php code in to laravel?
While add this code in laravel blade getting error?
<?php
$status=$_POST["status"];
echo "<h3>Thank You. Your order status is ". $status .".</h3>";
?>

Laravel is an MVC framework, so the business logic remains in controller function like:
$status = Input::get('status');
and pass it to view like:
return view('view_name', ['status' => $status]);
and you can use this variable on view like:
<h3>Thank You. Your order status is {{ $status }}</h3>

Related

Redirect to another route with data Laravel 5.8

Good day everyone. I am encountering an error in my code. I am trying to redirect to another route and passing data with it.
Controller code:
return redirect()->route('customer.success', compact('data'));
Route:
Route::get('success-customer', 'CustomerController#showSuccess')->name('customer.success');
Blade:
Your assistant: {{Session::get($data['assistant'])}}
Now my error is, it shows the error of undefined data yet I used the compact function.
Answers and advices are highly appreciated!
In laravel 5.8 you can do the following:
return redirect('login')->with('data',$data);
in blade file The data will store in session not in variable.
{{ Session::get('data') }}
You can use this:
return redirect()->route('profile', ['id' => 1]);
To redirect to any controller, use this code
return redirect()->action('DefaultController#index');
If you want to send data using redirect, try to use this code
return Redirect::route('customer.success)->with( ['data' => $data] );
To read the data in blade, use this one
// in PHP
$id = session()->get( 'data' );
// in Blade
{{ session()->get( 'data' ) }}
Check here for more info
TRy this
return Redirect::route('customer.success')->with(['data'=>$data]);
In blade
Session::get('data');

data not displayed when passed from controller to view

I am trying to display data sent from Controller to view in Laravel 5.8 but when I do in my controller:
return view('index', ['user'=> 'mark']
and I try to display it in the view using:
{{$user}}
nothing is displayed, why and how to solve this please?
when I do dd($user) in view I get 'mark' but it is not displaying using previous method
I tried what is in Laravel 5.8 documentations
you can try this:
//controller
$data['user'] = 'mark';
return view('index', $data);
//your view
{{ $user }}
In your controller, put:
$user = 'mark';
return view('index', compact('user'));
In your view, you can now access the user through Blade templating:
{{ $user }}

New to Laravel - How to pass model data to a blade view?

Ok, I am totally re-writing this question, now that I am a bit more familiar with larval.
Here is my situation: I have a guitar lessons site based on larval 5.2.36, where each lesson belongs to a category, and within a lesson are several exercises. An exercise table does not have a category id as it is linked to a lesson which has a category.
Goal What I am trying to figure out is how to pass the category of the currently displayed lesson or exercise to a menu sidebar view that displays the categories, so that the category of the lesson or exercise is highlighted. For this, I need to understand how to do such a task in laravel.
From what I gathered, this is often done via controllers. However, there is no menu controller, but rather a menu composer. It contains a function
class MenuComposer
{
public function compose(View $view)
{
$minutes = 6 * 60;
$value = Cache::remember('menu-categories', $minutes, function() {
return \App\Category::with('parent')->with('children')->get();
});
$view->with('categories', $value);
}
}
Then in the menu blade file we have
#foreach ($categories as $category)
<?php $category = $category->present(); ?>
#if ($category->parent == null)
<li>{{ $category->title }}</li>
#foreach ($category->children as $child)
<?php $child = $child->present() ?>
<li class="level1">{{ $child->title }}</li>
<?php
/*
#foreach ($child->children as $grandChild)
<?php $grandChild = $grandChild->present() ?>
<li class="level2">{{ $grandChild->title }}</li>
#endforeach
*/
?>
#endforeach
#endif
#endforeach
So this is clear. I see that I can use the menu composer to pass additional data with a $view->with() call.
The question is how do I get the current category? For exercises and lessons, the routes don't have category data. They are of form
lesson/lessonid/lessontitle
or
exercise/exid/extitle
So I know I could do some sort of query of the model. But seems that wouldn't make sense, since I know there are other places in the process flow where the current cat is being passed. For instance, on an exercise page, the view is retrieving category as
$exercise->lesson->category->title
It is being passed this in exercise controller as
public function index($id, $name = null)
{
//$this->hit($id);
$exercise = $this->apiController->get($id);
$authorized = $this->isUserAuthorized();
return view('exercise/index', [
'exercise' => $exercise->present(),
'authorized' => $authorized,
]);
}
Similarly, a lesson controller passes $lesson object to lesson view as
public function index($id, $name = null)
{
//$this->hit($id);
$lesson = $this->apiController->get($id);
$subscribed = $this->request->user() && $this->request->user()->subscribed('premium');
return view('lesson/index', [
'lesson' => $lesson->present(),
'subscribed' => $subscribed,
]);
}
Based on above, seems I could modify the return statements in the lesson and exercise controller to pass the category to the menu view, but I don't see in the documentation how to do that, and I suspect the menu view is rendered before the lesson and exercise controller are called...
Also read about using service providers. middleware, etc, here: How to pass data to all views in Laravel 5?
But all these approaches seem overkill. I don't need every view to have the data. Seems to me, I need to do this somehow in the menu composer. But I don't know what method to use from the menu composer to retrieve the current lesson or exercise category. In the menu composer after debugging in phpstorm I see that the $view object for a lesson has $view->$data->$lesson->$entity.
So what I did was edited the menu composer to pass category to view:
$d=$view->getdata();
$s=array_key_exists ('lesson' , $d );
if ($s ==1) $attr = collect($d)->get('lesson');
$cat=$attr->cat();
This works since in the LessonPresenter I added function
public function cat()
{
$cat = $this->entity->category['attributes']['title'];
return $cat;
}
This works, but I feel like it is a hack. And I will have to do this for the Exercise Presenter as well. Being new to larval I suspect there has to be a more elegant way to do this. So can someone please explain how this should be done?
thanks,
Brian
You can use Facades of Laravel directly in blade templates.
Just use {! !} syntax to try and echo it. e.g: {!! Route::current() !!}
There are also similar functions of Route facade you can use.
Then, you can check your category with #if() ... #endif blocks and add something like class name within it.
Note: Don't put lots of logic in your blade files. Do it in your controller file (even in your other service classes) and pass simplest variables (e.g $isCurrentCategory) as an array to your template files using View::make() function's 2nd parameter.
Maybe this can help you
<a href="#" class="{{ (\Request::route()->getName() == 'routename') ? 'active' : '' }}">
You can also get the route prefix for example, you can check this out here:
Laravel API Docs Routing

Laravel - Passing Parameters/ undefined variable

I'm struggling with passing parameters through in Laravel, I can access it via URL but I don't want that. I'm getting Undefined variable: user in the master.blade error.
Any help is appreciated
AuthController.php
function viewUserDetails($userId)
{
$user = User::find($userId);
return view('user/userdetails',['user' => $user]);
}
Web.php
Route::get('userdetails/{userId}', 'AuthController#viewUserDetails');
Master.Blade
<li>Your Details</li>
Other guys are telling you about userdetails view, but your code is totally fine. What you need to do is pass $user variable to the master view too:
return view('master', ['user' => $user]);
Try to pass like this
return view('user/userdetails')->with('user' => $user);
Controller::
function viewUserDetails($userId)
{
$user = User::find($userId);
return view('user.userdetails')-with('user',$user);
}
Then Used In blade File..
#foreach ($user as $data){
<h1> {{ $data['_id'] }} </h1>
<h1> {{ $data['name'] }} </h1>
#endforeach
I assume that you have extended master.blade in user/userdetails.blade. You need use array as second parameter to url function.
<li>Your Details</li>
It is best practice to use named route in laravel.
Change your route like this :
Route::get('userdetails/{userId}', 'AuthController#viewUserDetails')->named('user_details');
In your blade you can use route function to get the path.
<li>Your Details</li>
You can update your code like:
AuthController.php
function viewUserDetails($userId)
{
$user = User::find($userId);
return view('user.userdetails',compact('user'));
}
Web.php
Route::get('userdetails/{userId}', 'AuthController#viewUserDetails');
Master.Blade
<li>Your Details</li>

Pass sql result from controller to views in laravel

I'm having a problem to get my data from controller into my view, and I was hoping someone could help me.
Here is the code for my controller:
$Info = new Info();
$Info = DB::select('SELECT * FROM 'tblinfo');
and I only echo it in controller because my value results doesn't pass into the view, here's the additional code for my controller:
foreach ($Info as $Infos)
{
echo $Infos->fname;
echo $Infos->gender;
}
$data['result'] = $Infos->fname;
$data['result'] = $Infos->gender;
$this->view = View::make(App::make('web')->makeDeviceVersionPath('.').'.infofolder.infofile', $data);
Here is the code for my views:
#foreach $result as $info
{{ $info->name }}
{{ $info->gender }}
#endforeach
I hope someone could help me to pass my sql result to my views. Thank you! :)
if u have model u dont need to type raw sql code
just do like this
$info= Info::all();
return View('Index.index')->with('info',$info); // use ->with('*',$*) to send data to view
// Index is View folder
// .index is view name
Now in view grab $info data with for each
#foreach($info as $infodata)
<h3>{{ $infodata->name }}</h3>
<h3>{{ $infodata->gender}}</h3>
#endforeach
Adnan's answer is ok.
Op you should really check Eloquent and Response documentation from laravels website.
http://laravel.com/docs/4.2/eloquent
http://laravel.com/docs/4.2/responses

Resources