I am using Laravel.i want to show only the Name in my View Part. But facing problem.
function index() {
$data=array(
['id'=>'1','Name'=>'Debasais','sirname'=>'Acharya'],
['id'=>'2','Name'=>'prashant','sirname'=>'Mohaptra'],
['id'=>'3','Name'=>'Silu','sirname'=>'Mohaptra']);
return view("welcome")->withdata($data);
}
To show just the name in your view, just loop through $data and select only name.
#foreach($data as $names)
{{$names['Name']}}
#endforeach
I am pretty sure you are already looping through $data just make sure to output the name only
Related
I have quotes I am displaying to the screen one at a time upon each page refresh. The data is displaying fine from the DB; the issue is that I am getting the (id) display on the screen as well when I only need the quote and author name. How do I "exclude" the id field from displaying in my view?
Controller
public function index()
{
$quotes = DB::table('quotes')->inRandomOrder()->first();
return view('home', compact('quotes'));
}
Blade/View
#foreach($quotes as $quote)
<p>{{$quote}}</p>
#endforeach
Edit based on the comment of #devk:
Generally answers OPs question, but the code here (and in OPs post)
doesn't exactly make sense. The ->first() will return null or a single
quote, but the #foreach(..) is used like it's a collection of quotes
you can do the query like this:
$quote = DB::table('quotes')->select('quote', 'author')->inRandomOrder()->first();
replacing 'quote' and 'author' by the fields names in your table.
And return te quote:
return view('home', compact('quote'));
And in blade show the object:
<p>{{$quote}}</p>
or show the fields:
#isset($quote)
// $quote is defined and is not null...
<p>{{$quote->quote}}</p>
<small>{{$quote->author}}</small>
#endisset
If you want to show multiple quotes, do the query like this:
$quotes = DB::table('quotes')->select('quote', 'author')->inRandomOrder()->get();
or
$quotes = DB::table('quotes')->select('quote', 'author')->inRandomOrder()->take(5)->get();
And in blade you can loop through the collection:
#foreach($quotes as $quote)
<p>{{$quote}}</p>
#endforeach
or
#foreach($quotes as $quote)
<p>{{$quote->quote}}</p>
<small>{{$quote->author}}</small>
#endforeach
I use encrypt. I can display all table data. However I can'T get one record. I don't encrypt 'id'. I tried another column but same.
Here is my error
Trying to get property 'id' of non-object
Here is my code
blade file (I click this link first )
{{ $val->id }}
blade file (display page I got error at this page)
#foreach ($data as $val)
{{ $val->id }}
#endforeach
Trying to get property 'id' of non-object
web.php
Route::get('/one','MailController#onerecord');
Route::post('/one','MailController#onerecord');
Controller
public function onerecord(Request $request)
{
$id = $request['id'];
$data = Contact::where('id',$id)->get();
return view('mail.one', ['data' => $data]);
}
Could you teach me what is wrong my code please?
Use dd($data) after retrieving your Contact with Contact::where('id',$id)->get(); in order to investigate the content of $data. Most likely an object is returned and not an array, so looping through $data inside your blade #foreach will loop through the object's properties. Therefore $val->id isn't valid but the direct access of $data->id would be, without the need of looping through $data.
i want to display data from my database
controller
public function generatePDF(Request $request)
{
$id = Auth::user()->id;
$name = User::select("NAME")->where("id", $id)->get();
$pdf = PDF::loadView('generatePDF', ['name'=>$name]);
return $pdf->stream('generatePDF.pdf');
}
blade
<h2>{{name}}</h2>
result
[{"NAME":"name_from_database"}]
can i display without [{"name":}], so just the value of data (name_from_database) ?
Simple use find like this
$name = User::find($id)->name;
Or direct from auth
$name = \Auth::user()->name;
To get logged in user id you can use \Auth::id()
you can use:
$user_id = User::findOrFail($id)->first();
$name=$user_id->name;
test the laravel log file:
\Log::info($name);
Use first() instead of get(), because get() will return an array and first() will return the object.
DRY Code line no.1 and no.2. simple use:
$name = auth()->user()->name;
to get the name of the currently authenticated user.
Send $name to you view is fine
$pdf = PDF::loadView('generatePDF', ['name' => $name]);
Correct your code in blade file
{{ name }} -> {{ $name }}
I hope this might help you. :)
Hello Aldi Rostiawan,
$nameGet = User::select("NAME")->where("id", $id)->get();
$nameFirst = User::select("NAME")->where("id", $id)->first();
Here in both line of code the difference is only Get and First.
Get method returns an Array of objects. So for example if the query apply on many records then the code will give you many objects in an array. Or if query will be true for only one record then also it will return an array with one object.
If you know that id is primary key of he table then only one record will be fetched then you can use First function instead if Get method.
First function always return an Object. In case if query apply on many records then also first method will return you only first record as an Object.
And it seems that you have required an Object only.
You should try this:
Controller
public function generatePDF(Request $request)
{
$id = Auth::user()->id;
$rsltUser = User::where("id", $id)->first();
$pdf = PDF::loadView('generatePDF', ['rsltUser'=>$rsltUser]);
return $pdf->stream('generatePDF.pdf');
}
blade
<h2>{{$rsltUser->name}}</h2>
use VALUE() to display name only.
in your case:
<h2>{{$rsltUser->value('name')}}</h2>
your name variable is an array containing a single object with a NAME attribute .. so to diplay that value, you should change your script to
<h2>{{name[0]['NAME']}}</h2>
I've found many question realated to my problem but couldn't found an answer yet. It's about my foreach loop in my blade.
I want to print all product-names in my blade but I couln't figure out how to do that.
thats how I'm getting the products:
--- current code:
// controller
$id_array = Input::get('id');
$products= Products::whereIn('id', $id_array)->get();
$product_name = [];
foreach($products as $arr)
{
$product_name= $arr->lists('name');
}
returning $product_name gives me this as a output:
["football","cola","idontknow","freshunicorn","dummy-data"]
In my blade is just a simple:
#foreach($products as $product)
{{ $product}}
#endforeach
Error: htmlentities() expects parameter 1 to be string, array given
Thanks for your help and time.
It seems you are getting an object in an array in an array.
Like this:
array(
array(
object
)
)
It happens because you use the get() function to retrieve you model. The get() function always "wants" to retrieve multiple models. Instead you will have to use the first() function.
Like this:
foreach($id_array as $arr)
{
$want2editarray[] = Product::where('id', $arr)->first();
}
Hope it helps :)
Edit after #Wellno comment
That's probably because Product::where('id', $arr)->first(); returns null because it did not find anything.
I forgot to add a check after the retrieving of the product.
This can be done like this:
foreach($id_array as $arr)
{
// First try to get model from database
$product = Product::where('id', $arr)->first();
// If $product insert into array
if ($product) $want2editarray[] = $product;
}
Why do you use loop with IDs? You can find all products by IDs:
$products = Product::whereIn('id', $id_array)->get();
And then use $products in the blade template
#foreach($products as $product)
{{ $product->name }}
#endforeach
try to use Model/Eloquent to fetch data.
View should only display the data and not fetching directly from DB or do heavy calculations.
Im having a bit of trouble learning to get my data into my view, and i was hoping someone could help me.
I have the following function in my model
public function getPrivateMessages()
{
$userId = Auth::user()->id;
$messages = DB::table('pm_conversations')
->where(function($query) use ($userId) {
$query->where('user_one', $userId)
->where('user_one_archived', 0);
})
->orWhere(function($query) use ($userId) {
$query->where('user_two', $userId)
->where('user_two_archived', 0)
})
->get();
}
How would i pass it to my controller, then into my view?
Im a bit lost.
Thanks
Assuming that this is your Conversation model, you need to return those messages you queried:
public function getPrivateMessages()
{
...
return $messages;
}
Use it in your controller to pass to your View:
class HomeController extends Controller
{
public function index()
{
$conversation = Conversation::find(1);
return View::make('index')->with('privateMessages', $conversation->getPrivateMessages());
}
}
And in your view show whatever you need to:
<html><body>
#foreach($privateMessages as $privateMessage)
{{$privateMessage->text}}
#endforeach
</body></html>
In your controller, you would call this in one of your actions:
$pms = MyModel->getPrivateMessages();
return View::make('layout')
->with('pms', $pms);
Note that MyModel should be replaced with the actual name of your model. The ->with('pms',$pms) bit says, pass the contents of the variable $pms to the 'layout' view and assign it to a variable named 'pms' in that view. Feel free to customize the name of the view to match whatever view you want to use and pick different variable names if you are so inclined.
Then, in your view you would use it like this:
#foreach($pms as $pm)
<p>From: {{ $pm->user_one}}</p>
<p>{{ $pm->message }}</p>
#endforeach
Here, we're just looping over each of the private messages and outputting a few fields (user_one and message, you'd want to use the names of whatever columns you have in the database).
For more info see these sections of the docs:
Views
Basic controllers
inside your view
<?php $var=DB::table('tablename')->get(); ?>
#foreach($var as $variable)
{{ $variable->tablefield }}
#endforeach
here we are accessing the table named 'tablename' from our database (abbrievated as DB) and then accessing all the columns of the table through get method. Then we are storing them in a random variable (say var so the that we can loop it easily). And then we are simply looping through to print our column data(as in the case above)