Laravel 4 : trying to get variable from same page - php

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().

Related

how to fix Undefined variable: Products in view blade laravel 9.50.1 [duplicate]

Hey as i am passing a blade view which is having it own controller also i am including it into the view which does not have its own controller. it gives me an undefined variable error can any one help me how to it.
I have a view which does not have any controller only have Route like this Route::get('index', function () { return view('index'); }); in this view i am passing another view which having its own controller and also having some data from an array. but after using this view inside the view i get undefined variable error.
Two steps :
Declare & transfer $variable to View from Controller function.
public function index()
{
return view("index", [ "variable" => $variable ]);
}
Indicate where transferred $variable from Controller appear in view.blade.php.
{{ $variable }}
If you do not make sure, $variable is transferred or not
{{ isset($variable) ? $variable : '' }}
If this helps anyone, I was completely ignorant to the fact that my route was not hooked with the corresponding controller function and was returning the view directly instead, thereby causing this issue. Spent a good half hour banging my head till I realized the blunder.
Edit
Here again to highlight another blunder. Make sure you're passing your array correctly. I was doing ['key', 'value] instead of ['key' => 'value'] and getting this problem.
You can try this:
public function indexYourViews()
{
$test = "Test Views";
$secondViews = view('second',compact('test'));
return view('firstview',compact('secondViews'));
}
and after declare {{$secondViews}} in your main view file(firstview).
Hope this helps you.
public function returnTwoViews() {
$variable = 'foo bar';
$innerView = view('inner.view', ['variable' => $variable]);
return view('wrapper.view, ['innerView' => $innerView]);
}
This may be what you are looking for?
... inside your wrapper.view template:
{!! $innerView !!}
EDIT: to answer the question in the comment: In order to fetch each line you for do this inside your $innerView view:
#foreach($variable as $item)
{{ $item }}
#endforeach
... and in the wrapper view it will still be {!! $innerView !!}

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

Paginate Issue Laravel 5.6

Was wondering if someone could check this out and let me know what I'm doing wrong. I'm trying to use paginate on a database query and render it in my view.
Is my DB query incorrect? I was following the docs to create it. Also read that I needed to remove ->get() when using paginate.
This is what is giving the error on my view: {{$item->paginate(4)}}, same happens if I use {{$item->links(4)}}
Everything renders fine if I remove paginate from the query in the controller..?
Here is what I'm working with.
Controller:
public function index()
{
// $news = DB::table('news')->orderBy('id', 'DESC')->paginate(4);
$news = DB::table('news')->select('id','title','description','listing_image','created_at','updated_at')->orderBy('id', 'DESC')->paginate(4);
return view('news.index',compact('news'));
}
View: (Getting error: Call to undefined method stdClass::paginate())
#if ($news->count())
#foreach($news as $item)
...html
#if ($item->listing_image)
...more html
#else
#endif
... more html
#endforeach
{{$item->paginate(4)}}
#endif
change {{$item->paginate(4)}} to {{ $news->links() }}

view cannot access variable defined in controller in laravel

I could not think how to write question for this problem.
So this is my problem.
I have two views
store-side-menu.blade.php
×
#foreach($store_categories as $cats)
{{ $cats->category_of_store }}
#endforeach
employ.blade.php
#section('side-menu-section')
#include('fc.static.store-side-menu')
#endsection
In employ.blade.php is only includes the store-side-menu.blade.php. I have designed this structure due to need.
In controller I have
public function employment()
{
$search = "";
$store_categoies = Category::all();
return view('fc.employ.employ', compact('search', 'store_categories'));
}
Now I have included store_categories in compact but store-side-menu.blade.php cannot access it. How do I achieve with its solution.
Try the code below and you should be good to go:
#section('side-menu-section')
#include('fc.static.store-side-menu', ['store_categories' => $store_categories])
#endsection

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

Categories