parse error when trying to display in laravel view - php

I get this error whenever I'm trying to display items from database in my views.
Undefined variable: manufacturer (View:.../index.blade.php)
in my controller:
public function index(Request $request){
$title = array('pageTitle' => Lang::get("website.Home"));
$result = array();
$manufacturers = DB::table('manufacturers')
->leftJoin('manufacturers_info','manufacturers_info.manufacturers_id', '=', 'manufacturers.manufacturers_id')
->select('manufacturers.manufacturers_id as id', 'manufacturers.manufacturers_image as image', 'manufacturers.manufacturers_name as name', 'manufacturers_info.manufacturers_url as url', 'manufacturers_info.url_clicked', 'manufacturers_info.date_last_click as clik_date')
->get();
$result['manufacturers'] = $manufacturers;
return view("index", $title)->with('result', $result); }
my views:
{{ $manufacturer->name }}
help would be much appreciated.
Thank You.

If you're putting the manufacturers array in the $result and passing the $result then you have to reference it by $result in your view.
$result['manufacturers'] = $manufacturers;
return view("index", $title)->with('result', $result);
Something like...
#foreach ($result['manufacturers'] as $manufacturer)
{{ $manufacturer->name }}
#endforeach

in the Controller:
$result['manufacturers'] = $manufacturers;
return view('index', [
$result => $result,
]);
in the view:
<ul>
#foreach ($result['manufacturers'] as $manufacturer)
<li>{{ $manufacturer->name }} </li>
#foreach
</ul>

Related

How to echo object name of an array in Laravel controller

how do I display an object name of an array that I fetched from the database in Laravel blade?
When I dump array in Laravel controller I get this "name" => "{"name":"Item 1"}". Now, how do extract Item 1 from array and display in a blade while looping using foreach.
Controller
$data = DB::table('items')
->select('name')
->where('id', $user->id)
->get();
$output = json_decode($data,true);
dd($output);
Results
"name" => "{"name":"Item 1"}
Kindly help.
You have to extract from each object separately.
Variant 1:
Controller:
$data = DB::table('items')->select('name')->where('id', $user->id)->get()
->map(function($item){
$item->name = json_decode($item->name, true);
return $item;
});
return view('some_view', ['data' => $data]);
View:
#foreach($data as $d)
<p>{{ is_array($d->name) ? $d->name['name'] : 'No Name' }}</p> {{-- Just Checking if Json is correctly decoded --}}
#endforeach
Variant 2:
Controller:
$data = DB::table('items')->select('name')->where('id', $user->id)->get();
View:
#foreach($data as $d)
#php($name = json_decode($d->name, true))
<p>{{ is_array($name) ? $name['name'] : 'No Name' }}</p> {{-- Just Checking if Json is correctly decoded --}}
#endforeach
Controller:
$dataes = DB::table('items')
->select('name')
->where('id', $user->id)
->get();
return view('viewName',compact('dataes'));
View:
#foreach($dataes as $data)
{{ $data->name }}
#endforeach

Call to undefined method Illuminate\Database\Eloquent\Builder::links() (View: D:\xampp\htdocs\mieaceh\resources\views\shop.blade.php)

I am trying to paginate my categories shop.blade.php but I get this error:
Call to undefined method Illuminate\Database\Eloquent\Builder::links()
(View: D:\xampp\htdocs\mieaceh\resources\views\shop.blade.php)
Here is my MenuController.blade.php
if (request()->category) {
$menu = Menu::with('categories')->whereHas('categories', function ($query) {
$query->where('slug', request()->category);
});
$categories = Category::all();
$categoryName = $categories->where('slug', request()->category)->first()->name;
} else {
$menu = Menu::inRandomOrder()->take(10)->paginate(8);
$categories = Category::all();
$categoryName = 'Featured';
}
return view('shop')->with([
'menu' => $menu,
'categories' => $categories,
'categoryName' => $categoryName,
]);
and my shop.blade.php
<div class="category text-center">
#foreach($categories as $category)
<a href="{{ route('menu.index', ['category' => $category->slug]) }}">
<div class="card-category" style="width: 10rem; height: 4rem;">
{{ $category->name }}
</div>
</a>
#endforeach
{{ $menu->links() }}
How to solve it?
Right now, if you have a request()->category, then you're not adding the paginate to the $menu query. Just add it
$menu = Menu::with('categories')->whereHas('categories', function ($query) {
$query->where('slug', request()->category);
})->paginate(8);
You are missing pagiante() method on your menu query:
$menu = Menu::with('categories')->whereHas('categories', function ($query) {
$query->where('slug', request()->category);
})->paginate(8);
The way you did your query, you were returning QueryBuilder class, which has no links() method (pagination). This way you will return collection of data which you will be able to paginate.

Undefined variable template_2 laravel 5.2

Getting error with following code
$result = User::where("device_token", "!=", "")->where("device_type", "!=", "")->get();
$template_2 = DB::table('notification_templates')->get();
return view('admin.push_notifications')
->withPage('push_notifications')
->with([
'result' => $result,
'template_2' => $template_2,
]);
view:
#if(($template_2))
#foreach($template_2 as $taken)
{{ $taken->title }
#endforeach
#endif
PHP v5.5.9
According to your question your code must work.. If not works you can try this code:-
$result = User::where("device_token", "!=", "")->where("device_type", "!=", "")->get();
$template = DB::table('notification_templates')->get();
return view('admin.push_notifications')->with(compact('result','template'));
View should be:-
#if(isset($template))
#foreach($template as $taken)
{{ $taken->title }
#endforeach
#endif
Hope it helps!

Laravel Paginate not working

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

Laravel 4 Pagination not working (ErrorException)

I've searched for all solutions to implement pagination successfully, but nothing I've found worked. I'm new to Laravel so I have maybe only one of those basic misunderstandings.
I have created it like it is described in the current documentation. And I get the first page successfully, exactly the content i need, but when i try to use the pagination, clicking on the "next"-arrow, I get an error message:
ErrorException
Undefined variable: reviews (View: D:\Xampp\htdocs\laravel\app\views\search.blade.php)
I know what this generally means but absolutely no idea how to solve this.
This is the form in the view file:
{{ Form::open(array('url' => '/search', 'class' => 'search', 'method' =>'GET')) }}
{{ Form::text('title') }}
{{ Form::submit('Suche')}}
{{ Form::close() }}
Here is my routes configuration:
Route::any('/search', array('uses' => 'HomeController#search'));
Here is my function in the HomeController:
public function search() {
$input = Input::get('title');
if($input && $input != ''){
$games = DB::table('games')->where('name', 'LIKE', "%$input%")->get();
foreach ($games as $game) {
$reviews = DB::table('reviews')->whereGame_id($game->id)->paginate(3);
}
}
return View::make('search', compact('reviews', 'input', 'games'));
}
Here is the view-code of the search.blade.php view, which displays the results:
#section('content')
<h2>Suchergebnisse</h2>
#foreach ($reviews as $review)
#foreach ($games as $game)
#if ($game->id == $review->game_id)
<h3> {{ $game->name }} </h3>
#endif
#endforeach
<p>{{ (preg_replace('/\n/i', '<br/>', $review->body)) }}</p>
#endforeach
{{ $reviews->appends(array('term' => $input))->links() }}
#stop
I thank you beforehand if someone knows why Laravel has a problem with this solution.
Edit:
I've changed the compact-statement and replaced it with an simple array, just like that:
return View::make('search', array('reviews' => $reviews, 'input' => $input, 'games' => $games));
Then I've added global $games; global $reviews; to the Controller, so the Error "Undefined variable: reviews" disappeared. Now I've got the following message, but only on page 2, page 1 works fine:
ErrorException
Invalid argument supplied for foreach() (View: D:\Xampp\htdocs\laravel\app\views\search.blade.php)
First of all you are doing this
foreach ($games as $game) {
// $reviews is being updated every time in loop
$reviews = DB::table('reviews')->whereGame_id($game->id)->paginate(3);
}
So, every time there is a new value is being set to $review but it should be an array and that's why you should declare an empty array before the loop like this
$reviews = []; // or array();
foreach ($games as $game) {
$reviews[] = DB::table('reviews')->whereGame_id($game->id)->paginate(3);
}
So, you'll get an array in the $review variable.
Regarding your error message Undefined variable: reviews, it's not defined and it could be because if($input && $input != '') condition doesn't match or $games got nothing, so, try to debug step by step your code, at first make sure that execution control is being entered inside if by making dd($input) statement inside if and then if it outputs anything then make sure $games is not null/false by making a dd($games) statement after the games query.
You can change the code snippet to this
$input = Input::get('title');
if($input) {
$games = DB::table('games')->where('name', 'LIKE', "%$input%")->get();
if($games) {
$reviews = []; // or array();
foreach ($games as $game) {
$reviews[] = DB::table('reviews')->whereGame_id($game->id)->paginate(3);
}
return View::make('search', compact('reviews', 'input', 'games'));
}
}
// return a 404 (not found) or something like this or whatever, when nothing found

Categories