Laravel not passing query result from controller to view - php

Laravel version 5.2
My controller file:
<?php
namespace App\Http\Controllers;
use App\questions;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Http\Requests;
class Dashboard extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function fetchQuestions() {
//
}
public function index(){
$listQuestions=Auth::user()->questions()->pluck('question')->all();
//dd($listQuestions);
return view('forms.question',compact('listQuestions'));
}
}
My views file
<ul>
#foreach ($listQuestions as $q)
<li>{{ $q }}</li>
#endforeach
</ul>
$listQuestions isnt getting passed to the view.
But on dd($listQuestions) from view & controller file, I get this
array:2 [▼
0 => "some question"
1 => "another question"
]
It shows empty view page, no errors or output
What am I missing here?

pluck() method retrieves values by keys. You want to get a collection:
$listQuestions = Auth::user()->questions()->get();
$q is an object, so you need to get a property, i.e. question:
#foreach ($listQuestions as $q)
<li>{{ $q->question }}</li>
#endforeach

You can also pass a variable to a view like this
return view('forms.question')
->with('listQuestions' , $listQuestions)
Now i believe you're just passing the string 'listQuestions' over to the view, so it can't iterate over it. Hence the empty view.

Change your
return view('forms.question',compact('listQuestions'));
To
return view('forms.question',['listQuestions'=> $listQuestions]);
That should work

Related

Why I cannot get the data from UserController to my user.blade.php file in Laravel 9?

Hello I need some help I am learning Laravel 9 and I have a problem, in the UserController I declare a public function show with a parameter $id and return it in user.blade.php file and call using template literals, my problem is I cannot get the data inside my function show.
This is the codein UserController file
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function show($id){
$data = array([
"id" => $id,
"name" => "John Doe",
"age" => 22,
"email" => "jhondoe#gmail.com"
]);
return view("user", $data);
}
}
web.php file
use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Route;
Route::get('/show/{id}', [UserController::class, 'show']);
user.blade.php file
{{ $data }}
it says
"ErrorException Undefined variable $data"
enter image description here
The view function expects an associative array as second parameter (see docs). Each key of the array will have an equivalent variable in your blade view.
So you should respond either be :
return view("user", ['data' => $data]);
Or use PHP's compact helper
return view("user", compact('data'));
Or change your view so that it uses
{{ $id }}
{{ $name }}
{{ $age }}
{{ $email }}
Do whatever makes more sense to your use case

Object type variable inside the component is not rendering component inside blade - Laravel

I am unable to pass the object type variables inside the component.
This is code inside the blade to show the component in laravel.
resources\views\includes\header.blade.php
<ul class="dropcart__list">
#foreach ((array) session('cart') as $id => $details)
// <li>Name: {{$details['product']['name'] }}</li>
// <li>Color: {{$details['productVariation']['color'] }}</li>
// --- Data is populating inside the `$details['product']` and
// --- `$details['productVariation']` absoultly right, I've verified it as above
// Below line is not showing any thing
<x-widgets.dropCartItem product={{$details['product']}}, productVariation={{$details['productVariation']}} />
#endforeach
</ul>
Code inside the app view component
app\View\Components\widgets\dropCartItem.php
<?php
namespace App\View\Components\widgets;
use App\Models\Product;
use App\Models\ProductVariation;
use Illuminate\View\Component;
class DropCartItem extends Component
{
public Product $product;
public productVariationiation $productVariation;
public function __construct($product, $productVariation)
{
$this->product = $product;
$this->productVariation = $productVariation;
}
public function render()
{
return view('components.widgets.dropCartItem');
}
}
Code inside the resource view component resources\views\components\widgets\dropCartItem.blade.php
<li>Name: {{$product['name']}}</li>
<li>Color: {{$product['productVariation']}}</li>

Laravel - Property [comment] does not exist on this collection instance

i have a problem with laravel model relationship. The codes are as follows:
Route:
Route::get('/customers','Back\CustomerController#index')->name('admin.customer');
Models\Customer.php:
class Customer extends Model{
public function getData(){
return $this->hasMany('App\Models\Datapanel','name','name');
}
}
Models\Datapanel.php:
class Datapanel extends Model{
//
}
CustomerController.php:
class CustomerController extends Controller{
public function index(){
$customers=Customer::orderBy('created_at','desc')->get();
return view('back.customer',compact('customers'));
}
}
customer.blade.php:
#foreach ($customers as $customer)
<div class="tab-pane" id="{{$customer->name}}" role="tabpanel">
{{$customer->getData}} //this line working and calling all data of the same name, but in array format.
{{$customer->getData->comment}} //this line is what I want, but it doesn't work and gives the error Property [comment] does not exist on this collection instance
{{$customer->getData->first()->comment}} //this line working, but calling first data and i want all the data of the same name.
{{$customer->getData->all()->comment}} //I wish there was something like but it donesn't work.
</div>
#endforeach
Since getData is returning a HasMany relationship it will be a collection and not a single record. So need to loop over the collection to display data. See below
#foreach ($customers as $customer)
<div class="tab-pane" id="{{$customer->name}}" role="tabpanel">
<!-- Loop over each record in $customer->getData -->
#foreach($customer->getData as $data)
{{ $data->comment }}
#endforeach
</div>
#endforeach

How to pass parameter in Laravel Relationship Function within blade?

Student.php
It has many fees
class student extends Model{
public function fees($round){
return $this->hasMany(fee::class)->where('payment_round','=',$round);
}
}
Fee.blade.php
#foreach($student->fees("parameter") as $fee)
{{$fee->myproperty}}
#endforeach
How I can pass a parameter to $student->fees("parameter")?
You can keep relationship as simple as possible and add a second method which uses that to get the data you want :
<?php
class student extends Model{
public function fees(){
return $this->hasMany(fee::class);
}
public function getFeeByRound($round)
{
return $this->fees()->where('payment_round','=',$round)->get();
}
}
And then you can use this as :
#foreach($student->getFeeByRound($parameter) as $fee)
{{ $fee->myproperty }}
#endforeach
Try something like that:
#foreach($student->fees($parameter)->get() as $fee)
{{ $fee->myproperty }}
#endforeach
I think you had confused the collection of related models fees with the query constructor fees().

Unable to print data with laravel (php)

I tried, but don't work: i don't know why...
Page.php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class takeData extends Controller
{
public function selectdata()
{
$data_users = DB::select('select * from persons where id = :id', ['id' => 1]);
return view('page', ['persons' => $data_users]);
}
}
....
page.blade.php
....
#foreach ($persons as $person)
<p>$person</p>
#endforeach
routes.php
Route::get('takeData', 'Page#selectdata');
Error:
Whoops, looks like something went wrong.
If I remove '#foreach ($ persons as $ person) $ person #endforeach' in page.blade.php everything works correctly, why is it wrong?
Thanks a lot!!!
On your view, change <p>$person</p> to this:
<p>{{ $person }}</p>
The {{ }} is used to output data as specified in the Blade Documentation.

Categories