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!
Related
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
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 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>
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'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