unable to print table data on blade file in laravel - php

need print Task table task_name column values in index.blade.php file
this is index.blade.php
#if(isset($tasks))
#foreach ($tasks as $task)
<h1>{{ $task->task_name }}</h1>
#endforeach
#endif
TasksController.php is
public function index()
{
$tasks = Task::all();
return view('tasks.index')->withTasks($tasks);
}
No any error message but do not print data...how to fix

Change:
return view('tasks.index')->withTasks($tasks);
to
return view('tasks.index')->with('tasks', $tasks);
or
return view('tasks.index', array('tasks' => $tasks));
and try again.
Explanation:
The second parameter of the view function is the the array that contains the data in it on different indexes. As an alternative to passing a complete array of data to the view helper function, you may use the with method to add individual pieces of data to the view.
Reference

Just replace
return view('tasks.index')->withTasks($tasks);
with
return view('tasks.index',['tasks'=>$tasks]);
And try again.

Related

Click Event open new blade page {{$data->title}}

I'm pulling data from database and i would like to make {{$data->title}} clickable.
“This for a new server. In the past, I’ve tried on existing servers.”
#foreach($ress as $key => $data)
{{$data->title}}
#endforeach
Define a route in your routes.php and call it in your template
{{ $data->title }}
This is the best way to do it. You can change the variable names to the ones you have used in your code.
Route.php
Route::get('/show/{id}','DataController#show')->name('showdata);
Link on say dataindex.blade.php
#foreach($datas as $data)
{{ $data->title }}
#endforeach
DataController.php
public function show(){
$datas = DB::table('table_name')->get();
return view('dataindex', ['datas'=>$datas]);
}

How to use count number of rows and print value in view file laravel 5.4?

app/http/controller/FirstController.php
public function delhi_property()
{
$sql = DB::table('property')->where('city', 'Delhi')->orWhere('city', 'New Delhi')->get();
return view('index',['results'=>$sql]);
}
resources/views/index.blade.php
<h3>({{ $results->count() }}) Properties</h3>
routes/web.php
Route::get('/','FirstController#delhi_property');
I am new in laravel 5.4 Here, what am I doing I simply run a query as I mention above in my Controller and want to print numbers of rows in my view file but when I check it shows an error i.e.
Undefined variable: result (View: D:\xampp\htdocs\real_estate\resources\views\index.blade.php)
So how can I solve this problem? Please help me.
You are returning the query result as "results", so in the view you need to access it as "$results".
In the error it says undefined variable result.
There is no "s" in it.
So refer to the code line that error returns and check whether variable naming is correct.
In controller:
public function delhi_property()
{
$data = DB::table('property')->where('city', 'Delhi')->orWhere('city', 'New Delhi')->get();
return view('index', compact('data'));
}
In blade file:
<h3>( {{ $data->count() }} ) Properties</h3>
OR
<h3>( {{ count($data) }} ) Properties</h3>
To count all rows in a query is a function for example:
count($results);
I am modifying your query a bit, will achieve the same result efficiently
public function delhi_property()
{
$cityResults = DB::table('property')->whereIn('city', ['Delhi','New Delhi'])->get();
return view('index',compact('cityResults'));
}
In your view you can access the count as following:
<h3>{{ $cityResults->count() }} Properties</h3>
In case you are using this to calculate and display the total count on a Bootstrap badge, try the following directly in the sidebar view:
<span class="badge badge-info right">{{ DB::table('property')->where('city', 'Delhi')->orWhere('city', 'New Delhi')->count() }}</span>
You can use larave's default function to count rows like example given below.
public function delhi_property()
{
$result = DB::table('property')->where('city', 'Delhi')->orWhere('city', 'New Delhi')->get();
$total_result = count($result);
return view('index',compact(['result','total_result']));
}
In view you can print number of rows in variable $result.

How To use Database as a variable with blade template in laravel?

i'm new in laravel, i have some problem to make database as variable to be shown on blade template,
example i want to get any data from database :
{{$get = DB::table('perangkat')->get()}}
then:
#foreach ($get as $got)
{{$got->jabatan}}
#endforeach
if the result of the $got->jabatan is kepala, But i want the result is Change To "Kepala Desa", How can i do ??
Please Help Mee ...
Write database related stuff in model or controller in a method and pass that value to view. It will be very neat and clear
public function getData(){
$get = DB::table('perangkat')->get();
return view('myblade', ['data' => $get]);
}
In your view
#foreach ($data as $got)
{{$got->jabatan}} Desa
#endforeach
Also can you update where you get Desa .so i can update answer according to that.
Updated
you keep different roles in array and do some think like this
<?php
$role=['a'=>'operator','b'=>'admin'] ; ?>
#foreach ($data as $got)
<?php $result = isset($role[$got->jabatan]) ? $role[$got->jabatan] : null; ?>
{{$result}}
#endforeach
company.php
function showdata{
$sql= "this syntax sql";
}
//show data
$company = new company();
#foreach ($company->showdata() as $got)
{{$got->jabatan}}
#endforeach

Unique collection in Laravel

I need to find out unique value. So I tried bellow code. It is through undefined variable error.
Controller:
$employee = Employee::all();
Return view ('page', compact('employee'));
Page view:
$uniqueEmpLoc = $employee->unique('location')->values()->list('location')->toArray();
#Foreach($uniqueEmpLoc as $empLoc)
{{ $empLoc }}
//this is select box used for search
#endforeach
//Display Entire data
#foreach($employee as #employee)
//Display all value
#endforeach
But I got an uniqueEmpLoc is undefined error. I'm using LARAVEL 5.1. Please help me to solve this problem.
There are some errors in your code:
I don't think compact(employee) would work. Shouldn't that suppose to be compact('employee') ?
In Blade, there is no need of putting curly braces at all. Remove them.
Try out the following:
$employees = Employee::unique('locations')->values()->list('location')->toArray();
return view('page', compact('employees'));
And then in your view:
#foreach($employees as $employee)
{{ $employee }}
#endforeach
Use this query in controller
$employees = Employee::distinct()->list('location')->toArray();
return view('page', compact('employees'));
In view
#foreach($employees as $employee)
{{ $employee }}
#endforeach
I agree with the other answers here, this code should be in the controller. You shouldn't be doing logic in the views.
In the controller do:
$uniqueEmpLoc = $employee->unique('location')->values()->list('location')->toArray();
$employee = Employee::all();
Return view ('page', compact('employee', 'uniqueEmpLoc'));
The reason your code isn't working is the line that defines $uniqueEmpLoc is interpreted by blade as text, not code.
If you really want to do that in the view, you need to wrap it in #php tags.
#php
$uniqueEmpLoc = $employee->unique('location')->values()->list('location')->toArray();
#endphp
#Foreach($uniqueEmpLoc as $empLoc)
{{ $empLoc }}
//this is select box used for search
#endforeach

Laravel 4 : trying to get variable from same page

I'm new to Laravel and I'm getting an error which I think has more to do with logic than anything else but I can't quite seem to grasp how to overcome it.
So, I have a page with a simple form to search for a particular string in my database. But I want to have the result show up on the same page.
Here's what I have so far:
This is my Route.php:
Route::get('/', 'HomeController#index');
Route::post('find', 'HomeController#find');
This is my HomeController:
public function index()
{
return View::make('index');
}
public function search()
{
return View::make('index');
}
public function find()
{
$match = Input::get('find');
if($match) {
$results = Book::where('title', 'like', '%'.$match.'%')
->orWhere('author', 'like', '%'.$match.'%')
->get();
return View::make('index', array('results', $results));
} else {
return Redirect::to('/')->with('flash_error', 'No string added!');
}
}
And my View (index.blade.php):
{{ Form::open(array('url' => 'find', 'method' => 'POST')) }}
{{ Form::text('find', '', array('class' => 'search-query', 'placeholder' => 'Search')) }}
{{ Form::submit('Submit', array('class' => 'btn btn-info')) }}
{{ Form::close() }}
#if (Session::has('flash_error'))
{{ Session::get('flash_error') }}
#endif
#foreach ($results as $result)
{{$result->title}}
#endforeach
(eventually the foreach will be replaced by some ajax loading to display each result)
And the error says "undefined variable: results" and shows the foreach.
I get why that error shows up since on the first pass to this page the results haven't been loaded yet but how can I overcome this? I really want the data to be shown on the same page without having to go to another page to display them.
Like I said, I think this is mostly logic related (although I'm very new to Laravel so it might be that too)
Any help would be greatly appreciated !
you need to pass an associative array as your second param of the make method
return View::make('index', array('results' => $results);
The problem here is that in your use of index.blade.php in multiple controllers, you forgot which controllers provide which variables (and as a result, which variables may be omitted).
When you request / (HomeController#index), index.blade.php is rendered, but since no $results are passed to the view, you see the Undefined Variable warning. This is not a problem in HomeController#find, because you define $results. To combat this, you'll need to do something along the lines of an isset() check on $results before you foreach over it. Like so:
#if(isset($results))
#foreach ($results as $result)
{{$result->title}}
#endforeach
#endif
Your logic may vary based on your page's layout (you might want to add an else and display some alternate placeholder content).
Also, if abstracting the call to View::make() with $results into index_gen() isn't keeping your code DRY, then I'd suggest replacing it in find() with the call to View::make().

Categories