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
Related
I have a controller method which retrieves data from a Queue, the queue can have a relationship to either a Guest or a Customer model.
In my blade template I iterate over the Queue and need to display either Guest.Name or Customer.Name depending on which column is populated.
Controller
$queue = Queue::where('business_id', '=', $business_id);
$customer_bag = $queue->pluck('user_id');
$guest_bag = $queue->pluck('guest_id');
$customers = User::whereIn('id', $customer_bag)->get();
$guests = Guest::whereIn('id', $guest_bag)->get();
return view('myqueue', compact(['queue', 'customers', 'guests']));
Blade Template
#foreach($queue as $quee)
{{ $customers->find($quee->user_id) ? $customers->find($quee->user_id)->name : $guests->find($quee->guest_id)->name }}
#endforeach
When I use this and the guest_id is blank I get an error stating "Trying to get property 'name' of non-object". How can I correctly detect which one to use?
I think so many problems with your code.
Firstly, $queue is findOrFail() which returns a single object data(not array or collection that can be used for foreach.) I think you need to change to get()
Also $customers and $guests are a collection, so you can not use find() method.
Then you didn't define $quee in the #foreach, so it's will give error result.
I think this code will return as you expected if you fix above problems:
#foreach($queue as $quee)
{{ \App\Customer::find($quee->user_id)->name ?? \App\Guest::find($quee->guest_id)->name ?? "no name" }}
#endforeach
But a better approach is using relation from your queue model:
public function customer()
{
return $this->belongsTo(Customer::class);
}
public function guest()
{
return $this->belongsTo(Guest::class);
}
public function getNameAttribute()
{
return $this->customer?$this->customer->name:($this->guest->name??"no name");
}
and in the blade view
#foreach($queue as $quee)
{{ $quee->name }}
#endforeach
The issue turned out to be that one of my test data records didn't have a value in the guest_id when it should have, this caused the blade template to fail render.
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
I have two collections on a single page which should be both paginated. But pagination generates the same Parameter for both (?page=X).
How can I solve that kind of an issue?
You can change the param of either pagination by
Paginator::setPageName('someparam');
Read more about Pagination here In the section Customizing The Paginator URI
Note : You should do this before paginator is done i.e.,
$yourCollection = Model::paginate(10);
Example :
I assume you have two pagination like this
Paginator::setPageName('yourFirstParam');
$firstCollection = FirstModel::paginate(10);
Paginator::setPageName('yourSecondParam');
$secondCollection = SecondModel::paginate(10);
Where you use this to get in your view
Paginator::setPageName('yourFirstParam');
$firstCollection->links();
Paginator::setPageName('yourSecondParam');
$secondCollection->links();
There is a way to "automatically" set the page name (in a sense), which I'll get to in a bit.
First, if we go over the paginate method, you'll see that it accepts a pageName argument as its 3rd parameter:
public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)
Lets say you have a User and Post model. You can then do something like this in your controller:
$users = User::paginate(10, ['*'], 'users');
$posts = Post::paginate(10, ['*'], 'posts');
return view('example', compact('users', 'posts'));
It works like your normal pagination except the second argument specifies the columns you want to select and the third argument specifies the page name.
In your view, when you render your pagination links, you might run into a problem when you do this:
{!! $users->render() !!}
{!! $posts->render() !!}
While the pagination links will be rendered, when you click on a link to a posts page, the users query string parameter is gone. Therefore, the users are back to page one and vice versa.
To fix this, you can use the appends method to keep the query parameters for both models:
{!! $users->appends(['posts' => Request::query('posts')])->render() !!}
{!! $posts->appends(['users' => Request::query('users')])->render() !!}
All this works, but it's a bit ugly so how can we clean this up? You can create your own method to "automate" this process. In your model, you can add your own paginate method:
// Name it whatever you want, but I called it superPaginate lol
protected function superPaginate($perPage)
{
return $this->newQuery()->paginate(10, ['*'], $this->getTable());
}
This will automatically set the pagination name to the model's table name. So for the User model, the page name will be "users". For the Post model, the page name will be "posts".
There's still the problem with rendering links. You don't want to call appends all the time and specify the query parameters. To fix that, we can improve the superPaginate method into this:
protected function superPaginate($perPage, $columns = ['*'], $page = null)
{
$params = \Request::query();
return $this->newQuery()->paginate(10, $columns, $this->getTable(), $page)->appends($params);
}
Now, all you need to do is Model::superPaginate(10); and $models->render(). Everything should work properly.
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)
I am new to Laravel and have been banging my head on this issue for a while. I am merely trying to get a single row from a table and hand it off to a view.
The table has two fields that should match up with this query, the first of course is the id and the second is user_id which matches the logged in user. When its done I plan on responding with a failure message (no record found that matches id: X).
public function show($id)
{
$data = Partners::where('id', $id);->where('user_id', Auth::user()->id)->get();
return View::make('partners.showone')
->with('data', $data)
->with('title', 'View Record')
->with('breadcrumb', 'View Partner');
}
In my view:
{{ $data->firstName }}
This configuration gives me an error of:
Undefined property: Illuminate\Database\Eloquent\Builder::$firstName
Typo...you have a ; in the middle there....it should be....
$data = Partners::where('id', $id)->where('user_id', Auth::user()->id)->get();
but your query really should be this..
$data = Partners::find($id)->where('user_id', Auth::user()->id)->first();
if you just want one row
EDIT (based on your comments below)
For your phones in your Partner model (Im working with Laravel 3 btw)
function phone(){
return $this->hasMany('Phone');
}
Then with the above $data query, you can get the phone by...
$data->phone();