I'm creating a Laravel 4 webapp and got the following route:
Route::get('products/{whateverId}', 'ProductController#index');
This is my index-function in ProductController:
public function index($whateverId)
{
$products = Product::all();
$data['whateverId'] = $whateverId;
return View::make('products', compact('products'), $data);
}
In my view, this returns the following error:
<p>Product: {{ $data['product'] }}</p>
ErrorException
Undefined variable: data (View: /Users/myuser/webapp/app/views/products.blade.php)
return View::make('products', compact('products'), "data"=>$data);
(or compact('data'))
Try passing it as:
$data['whateverId'] = $whateverId;
$data['products'] = Product::all();;
return View::make('products', $data);
And you'll have acces to it as
{{ foreach($products as ...) }}
and
{{ $whateverId }}
Or you can
$products = Product::all();
$data['whateverId'] = $whateverId;
return View::make('products')
->with('products', $products)
->with('whateverId', $whateverId);
Related
I have this in my controller:
public function detail($id) {
$data = DB::table('data_api')->where('id', $id)->get();
$carousel = DB::table('data_carousel')->where('data_api_id', $id)->get();
return view('detail', ['data_api' => $data]);
return view('detail', ['data_carousel' => $carousel]);
}
But when I try to echo-ing, $carousel by {{ $carousel }}, it says not found. But $data work perfectly. Any idea?
Undefined variable: carousel (View:
/mylaravelproject/resources/views/detail.blade.php)
you need to change the double return statement to a single return
return view('detail', ['data_api' => $data]);
return view('detail', ['data_carousel' => $carousel]);
to
return view('detail', ['data_api' => $data, 'data_carousel' => $carousel]);
you returning view two times that's why only $data_api is available in view,
try this
public function detail($id) {
$data = DB::table('data_api')->where('id', $id)->get();
$carousel = DB::table('data_carousel')->where('data_api_id', $id)->get();
return view('detail', ['data_api' => $data, 'data_carousel' => $carousel]);
}
Update:
public function detail($id) {
$data = DB::table('data_api')->where('id', $id)->get();
$carousel = DB::table('data_carousel')->where('data_api_id', $id)->get();
return view('detail', ['data_carousel' => $carousel,'data_api' => $data]);
}
You are returning two views from the same controller. After the first return execution of code is halt and it will not return the second view. That's why you are unable to get the second view parameters
You cannot return two times from a function and expect both to actually return something. After the first return, execution of the function is stopped.
Try returning both variables at once instead:
return view('detail', [
'data_api' => $api,
'data_carousel' => $carousel
]);
Replace your code with following:
public function detail($id) {
$data = DB::table('data_api')->where('id', $id)->get();
$carousel = DB::table('data_carousel')->where('data_api_id', $id)->get();
return view('detail')->with('data_api', $data)->with('data_carousel', $carousel);
}
You need to return view like below
public function detail($id) {
$data = DB::table('data_api')->where('id', $id)->get();
$carousel = DB::table('data_carousel')->where('data_api_id', $id)->get();
return view('detail', compact('data','carousel'));
}
is that really working now? You tell us you are getting
Undefined variable: carousel (View: /mylaravelproject/resources/views/detail.blade.php)
And you will get that because you are not passing the variable carousel to your view, you are naming your variables as data_api and data_carousell
Second, you should pass your variables as an asociative array in only one sentence not two view calls like this
return view('detail', ['carousel' => $carousel,'data' => $data]);
in my case i use
#if(isset($users))
before my foreach like this example:
<div class="form-group" id="boardAdminUserIdCon">
<p><span class="glyphicon glyphicon-briefcase" aria-hidden="true"></span> مدیر بورد</p>
<select name="boardAdminUserId" id="boardAdminUserId" class="form-control" required="required">
<option value="">{{ __('auth.CHOOSEYOURADMIN') }}...</option>
#if(isset($users))
#foreach($users as $user)
<option value="{{ $user['id'] }}">{{ $user["name"] }}
</option>
#endforeach
#endif
</select>
</div>
I was trying to paginate some data with Query builder with the following query.
public function productSearch(Request $request)
{
$name = $request->name;
$products = DB::table('products')
->where('name', 'LIKE', "%$name%")
->paginate(4);
return view('product',compact('products');
}
But when I tried to show the pagination in the view page with the following line
{{ $products->links() }}
It shows
Method links does not exist
What could the possible error be since the pagination does not show?
2 types to print pagination link in laravel -
Try with -
use Illuminate\Support\Facades\DB;
public function productSearch(Request $request)
{
$name = $request->name;
$products = DB::table('products')
->where('name', 'LIKE', "%$name%")
->paginate(4);
return view('product',['products' => $products]);
}
In View -
<div class="container">
#foreach($products as $product)
<h4>{{ $product['name'] }}</h5> <br>
#endforeach
</div>
1st type to defined laravel pagination link -
{{ $products->links() }}
And 2nd type to defined laravel pagination link -
{{ $products->render(); }}
Note - Also you missed in your code -> return view('product',compact('products')); in return view('product',compact('products');
Here is your solution.
Just change your return view option
return view('product')->with('products', $products);
Hope this helps you.
Change to :
return view('product',compact('products'));
In View page try :
{{$products->render()}}
In your Controller:
return view('product',compact('products');
In your View:
#foreach($products as $product)
{{ $product->name }}
#endforach
{{ $products->links() }}
try with:
{{ $products->render() }}
I am using Laravel, and I got an error:
Undefined variable: getFormTest (View: C:\xampp\htdocs\survey\resources\views\tambahformtest.blade.php)
That error references to this view:
<input value="{{ $getFormTest[0]->ms_test }}">
I have put $getFormTest in my controller:
public function TambahFormTest()
{
$ms_id = FormTest::max('ms_id');
$getFormTest = FormTest::Select('ms_test')->where('ms_id', '=', $ms_id)->get();
return view('tambahformtest', $getFormTest);
}
When returning a view in laravel, you have to pass an array of params.
return view ('myView', ['param1' => $v1, 'param2', $v2]);
then in your view
#if(isset($param1)
{{ $params->property }}
#endif
You should take a use of compact method of php
public function TambahFormTest()
{
$ms_id = FormTest::max('ms_id');
$getFormTest = FormTest::Select('ms_test')->where('ms_id', '=', $ms_id)->get();
return view('tambahformtest', compact('getFormTest'));
}
This would be sent to view as - ['getFormTest' => $getFormTest]
Hope this helps
In laravel controller I have following code:
public function getAdmins(){
//$users = $this->user->all();
$search[] =array();
$search['name']= Input::get('name','');
$search['uname']= Input::get('uname','');
$search['role']= Input::get('role','');
$users = $this->user->findUsers($search);
$exceptSuperadmin = array();
foreach($users as $user){
if(!$user->isUser())
$staffs[] = $user;
}
$users = #$staffs;
return view('users::admins.list')->with('staffs',$users)->with('search',$search);
}
In Model I have:
public function findUsers($search)
{
return self::where('name','like','%'.$search['name'].'%')
->where('username','like','%'.$search['uname'].'%')
->where('role','like','%'.$search['role'].'%')
->paginate(5);
}
And In blade file I have:
#if($staffs)
#foreach($staffs as $staff)
<!-- Some code here to loop array -->
#endforeach
#else
No Staffs
#endif
{!! $staffs->render() !!} Error comes at this line
I am not geeting why this error comes....staffs is an array and render() a function to echo the pagination pages...but can't getting the error...Anybody to help.
By applying foreach the pager object and assign an array you lose the paging properties, so you will have an array rather than a pager object.
I recommend the following solution for your case:
Controller:
public function getAdmins(){
$search[] =array();
$search['name']= Input::get('name','');
$search['uname']= Input::get('uname','');
$search['role']= Input::get('role','');
$users = $this->user->findUsers($search);
return view('users::admins.list')->with('users',$users)->with('search',$search);
}
Blade file:
#if($users)
#foreach($users as $user)
#if(!$user->isUser())
<!-- Some code here to loop array -->
#endif
#endforeach
#else
No Staffs
#endif
{!! $users->render() !!}
NO, render() doesn't work on an object per se, neither on the array you are creating out of the required object for the pagination to work (LengthAwarePaginator)
Since you have a collection, and you need one, you could use one of the methods provided to do your filtering, such as filter.
Something like (untested but should work):
$staff = $users->filter(function ($value, $key) {
return !$value->isUser();
});
This is on my routes.php
Route::get('registration/verify/{confirmation}', ['as'=>'verify', 'uses'=>'HomeController#verify']);
Route::get('login', ['as'=>'login', 'uses'=>'HomeController#getLogin']);
and on my blade is this
then on my HomeController.php where the error belong
public function verify($confirmation)
{
$user = User::where('activation_code', '=', $confirmation)->first();
return Redirect::route('login')
->withInput(['email' => $user->email])
->with('fuck', 'wtf');
}
and I got error like this
Undefined variable: fuck (View: C:\Program Files (x86)\Ampps\www\tridg\local\app\views\auth\login.blade.php)
I don't know where I been wrong, I'm so confident that this is correct.
EDIT1:
I even tried this
public function verify($confirmation)
{
$user = User::where('activation_code', '=', $confirmation)->first();
// $user->active = 1;
// $user->save();
return Redirect::route('login', ['fuck'=>'wtf'])
->withInput(['email' => $user->email]);
}
Nevermind, The answer is {{ Session::get('var_name') }} not {{ $var_name }}