unable to print table data in laravel - php

need print column data in my laravel app. My controller is TaskController.php
public function index()
{
$tasks = Task::all();
return view('tasks.index')->with('tasks', $tasks);
}
index.blade.php file is
#if(isset($tasks))
#foreach ($tasks as $task)
<h1>{{ $task->task_name }}</h1>
#endforeach
#endif
I am going include this index view file in show.blade.php file as following
#include('tasks.index')
but unable to print table data no any errors occurred. how can I print task_name in show view file?

If you are including the index blade inside the show blade, then you need to change your controller and pass data to the final blade (show instead of index) like this
return view('tasks.show')->with('tasks', $tasks);

If you want to show data on task.index then use this code.
public function index()
{
$tasks = Task::all();
//this line will pass $task data to this view
return view('tasks.index',compact('tasks'));
}

Related

Cannot pass full array from controller in laravel to a view using redirect()

I am unable to solve passing of array issue
below is my function in controller
public function fetchData($id)
{
$id=base64_decode(urldecode($id));
prod_detail=ProductDetail::select('prod_id','supplier_id','price','open_stock','discount_rate','min_order_level')->where('prod_id','=',$id)->get();
return redirect()->route('prod_d_view', compact($prod_detail));
}
below is my route
Route::get('/product_view', function(){
return view('/admin/product_d_mgt');
})->name('prod_d_view');
below is my error
Undefined variable: prod_detail (View: \admin\product_d_mgt.blade.php)
I am unable to pass the full array from one controller using redirect()->route() to another view
Maybe you can use something like this:
In your controller function:
...
return Redirect::to('product_view')->with('prod_detail', $prod_detail);
And in your product_view.blade.php file (in resources/view directory):
#if(Session::has('prod_detail'))
#foreach (Session::get('prod_detail')as $key => $value)
{{ $value->ColumnName }}
{{ $value->ColumnName2 }}
#endforeach
#endif
It has typo. Missing $ symbol before variable name prod_detail.
correct version:
public function fetchData($id)
{
$id = base64_decode(urldecode($id));
$prod_detail=ProductDetail::select('prod_id','supplier_id','price','open_stock','discount_rate','min_order_level')->where('prod_id','=',$id)->get();
return redirect()->route('prod_d_view', compact($prod_detail));
}

Grabbing the ID number in a blade

I have a route that takes me to my item page.
Route::get('/items/{item}', 'ItemsController#show');
In this page I have a form if the form is not filled out properly it will redirect to the forms method which currently is
action="/items"
How can I access that same {item} id number while in the blade?
Edit: Here is the Controller code
public function index()
{
$item = Post::get();
$price = $item->price;
return view('item');
}
public function show(Item $item)
{
return view('item', compact('item'));
}
You're returning compact('item') which gives you $item in your blade template.
You can use it within blade syntax, just like this:
{{ $item->id }}

How do I fetch data based on user id using Laravel 5.4

I have models User and Diares set up with the hasMany and BelongTo relstionship. I'm trying to fetch data from the Diaries table associated with a particular user. I tried auth but it didnt work for me. How can i achieve that and display it in the blade view as well. Here is my current code:
public function index(User $id)
{
$user = User::find($id);
$record = $user->diary;
return view('home')->with('record', $record);
}
The blade file it should display to:
#foreach ($record as $diary)
{{ $diary->error }}
{{ $diary->fix }}
#endforeach
In your index function the $id is not integer - this is a User instance, so you can try use this:
public function index(User $user)
{
$record = $user->diary;
return view('home')->with('record', $record);
}

Calling function from the same controller to return the view with flash message

When the user save the object in the blade view, a function into my controller execute with post request. If the save was success I want to reload the page with the changes.
So I need to call index() again to get the refreshed data. Also, I want to show a success message. So the steps are:
index() function execute so the user can show his items.
the user choose an item and save it
save(Request $request) is called
everything save fine
save function call index() with sending a parameter with the flash success message?
I have this structure in my controller:
public function index(){
...
return view('agenda')->with(['agenda'=>$response]);
}
public function save(Request $request){
...
// here where I need to return index function with success message
}
How can I solve my problem?
Assuming the save action was called from the index page, simply redirect back
public function save(Request $request){
...
return redirect()->back()->with('message', 'Item saved');
}
If it was called from a different page, redirect to index page. Assuming your index() function matches a route /:
return redirect()->to('/')->with('message', 'Item saved');
Then display the flash message in your index view.
#if(Session::get('message'))
{{ Session::get('message') }}
#endif
For styling, if you are using bootstrap you can do
#if(Session::get('message'))
<div class="alert alert-success">
{{ Session::get('message') }}
</div>
#endif

Passing data from controller to view in Laravel

I am new to Laravel and I have been trying to store all records of table 'student' to a variable and then pass that variable to a view so that I can display them.
I have a controller - ProfileController and inside that a function:
public function showstudents() {
$students = DB::table('student')->get();
return View::make("user/regprofile")->with('students',$students);
}
In my view, I have this code:
<html>
<head>
//---HTML Head Part
</head>
<body>
Hi {{ Auth::user()->fullname }}
#foreach ($students as $student)
{{ $student->name }}
#endforeach
#stop
</body>
</html>
I am receiving this error: Undefined variable: students (View:regprofile.blade.php)
Can you give this a try,
return View::make("user/regprofile", compact('students')); OR
return View::make("user/regprofile")->with(array('students'=>$students));
While, you can set multiple variables something like this,
$instructors="";
$instituitions="";
$compactData=array('students', 'instructors', 'instituitions');
$data=array('students'=>$students, 'instructors'=>$instructors, 'instituitions'=>$instituitions);
return View::make("user/regprofile", compact($compactData));
return View::make("user/regprofile")->with($data);
For Passing a single variable to view.
Inside Your controller create a method like:
function sleep()
{
return view('welcome')->with('title','My App');
}
In Your route
Route::get('/sleep', 'TestController#sleep');
In Your View Welcome.blade.php. You can echo your variable like {{ $title }}
For An Array(multiple values) change,sleep method to :
function sleep()
{
$data = array(
'title'=>'My App',
'Description'=>'This is New Application',
'author'=>'foo'
);
return view('welcome')->with($data);
}
You can access you variable like {{ $author }}.
The best and easy way to pass single or multiple variables to view from controller is to use compact() method.
For passing single variable to view,
return view("user/regprofile",compact('students'));
For passing multiple variable to view,
return view("user/regprofile",compact('students','teachers','others'));
And in view, you can easily loop through the variable,
#foreach($students as $student)
{{$student}}
#endforeach
You can try this as well:
public function showstudents(){
$students = DB::table('student')->get();
return view("user/regprofile", ['students'=>$students]);
}
Also, use this variable in your view.blade file to get students name and other columns:
{{$students['name']}}
Try with this code:
return View::make('user/regprofile', array
(
'students' => $students
)
);
Or if you want to pass more variables into view:
return View::make('user/regprofile', array
(
'students' => $students,
'variable_1' => $variable_1,
'variable_2' => $variable_2
)
);
In Laravel 5.6:
$variable = model_name::find($id);
return view('view')->with ('variable',$variable);
public function showstudents() {
$students = DB::table('student')->get();
return (View::make("user/regprofile", compact('student')));
}
try with this code :
Controller:
-----------------------------
$fromdate=date('Y-m-d',strtotime(Input::get('fromdate')));
$todate=date('Y-m-d',strtotime(Input::get('todate')));
$datas=array('fromdate'=>"From Date :".date('d-m-Y',strtotime($fromdate)), 'todate'=>"To
return view('inventoryreport/inventoryreportview', compact('datas'));
View Page :
#foreach($datas as $student)
{{$student}}
#endforeach
[Link here]
$books[] = [
'title' => 'Mytitle',
'author' => 'MyAuthor,
];
//pass data to other view
return view('myView.blade.php')->with('books');
or
return view('myView.blade.php','books');
or
return view('myView.blade.php',compact('books'));
----------------------------------------------------
//to use this on myView.blade.php
<script>
myVariable = {!! json_encode($books) !!};
console.log(myVariable);
</script>
In laravel 8 and above, You can do route binding this way.
public function showstudents() {
$students = DB::table('student')->get();
return view("user/regprofile",['students'=>$students]);
}
In the view file, you can access it like below.
#foreach($students as $student)
{{$student->name}}
#endforeach

Categories