Laravel ::all->where returns everything - laravel

I'm trying to get only rows which have column approved filled with value 1
$reviews = Review::all()->where('approved', '1');
return view('items.show', compact('reviews')
I also tried
$reviews = Review::where('approved', '1');
return view('items.show', compact('reviews')
and then I'm fetching them with
#foreach ($item->reviews->sortByDesc('created_at') as $review)
But this return every row from Review table from database no matter what the approved value. What am I doing wrong? Thanks in advance!

You must be use
$reviews = Review::where('approved', '1')->latest()->get();
return view('items.show', compact('reviews');
in blade
#foreach ($reviews as $review)

First thing is there is no reason to get "all" the records from the database then filter them when you can ask the database to only give you the records you want filtered.
$reviews = Review::where('approved', 1)->orderBy('created_at', 'desc')->get();
Second, as mentioned in the comments you should be iterating $reviews:
#foreach ($reviews as $review)

In the first example, you're adding a where condition after the query has executed. In the second you're not executing the query at all.
Instead do $reviews = Review::where('approved', '1')->get();

Related

Laravel Eloquent Only Get One Row

So I want to show id from order table where user id is the same as currently login user id, then later used to show orders have been made by the user
$orderId = Order::select('id')->firstWhere('user_id', auth()->id())->id;
$orders = SubOrder::where('order_id', $orderId)->orderBy('created_at', 'desc')->get();
it works but it only shows the first record, after some digging later I found out that the problem is on the $orderId, it only shows the first record. but I want it to be all the records. if I change the id to get(), it shows nothing since it give the result like "id = 1" instead of the number only. also have tried to change the firstWhere into where and got error like "Property [id] does not exist on the Eloquent builder instance."
please help, thanks
If you are going to use the other Orders associated with the User soon after you get the first Order, return all the relevant Orders and then just grab the first one when you need it.
$orders = Order::where('user_id', auth()->user()->id)->get();
$firstOrder = $orders->first();
$subOrders = SubOrder::whereIn('order_id', $orders->pluck('id'))->get();
Alternatively, you could use a subOrders relationship defined on your Order model.
class Order extends Model
{
public function subOrders()
{
return $this->hasMany(SubOrder::class);
}
}
$orders = Order::where('user_id', auth()->user()->id)->get();
$firstOrder = $orders->first();
$firstOrderSubOrders = $firstOrder->subOrders;
If you're confident you're going to be working with SubOrder records, you can use eager loading on your Order to improve performance.
$orders = Order::where('user_id', auth()->user()->id)
->with('subOrders')
->get();
$firstOrder = $orders->first();
$firstOrderSubOrders = $firstOrder->subOrders;
first() will return the first id queried and stop execution.
$firstOrder = $orders->first();

Only display selected fields to the view using Laravel Query Builder

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

Using Laravel 5.4 query builder with an array (json serialized) attribute

I'm stumped on this one. I can search an array of ids but I want to search the reverse. I have models with lists of ids as "with_ids" attribute and want to search similar to mongo db where id is in that array of ids.
For example
db.conversations.find( { with_ids: { $in: [id] } } )
How do I do that with Laravel and mysql/Eloquent?
$conversations = Conversation::with('messages.user')->where('with_ids', $id)->orWhere('created_by', $id)->get();
it's the where('with_ids', $id) I can't figure out... Any suggestions??
To clarify further:
I need to find if the user is participating in other conversations as well as the ones he created. The with_ids is a json serialized array f.ex [1,2,23,12] how do i search inside the array attribute?
Not sure I understand. Did you try with whereIn();
$conversations = Conversation::with('messages.user')
->whereIn('with_ids', [$id])
->orWhere('created_by', $id)
->get();
Edit
$conversations = Conversation::with('messages.user', function($query) {
$query->where('id', $id); // user_id ?
})
->orWhere('created_by', $id)
->get();
After MUCH digging I finally found a solution. FIND_IN_SET in a whereRaw query did the trick. In case anyone else has come upon this issue, hope it helps.
it's not pretty because for some reason quotes need to be stripped out
Conversation::where('created_by', $id)->orWhereRaw("FIND_IN_SET(?, REPLACE(REPLACE(REPLACE(with_ids, '\"', ''), '[', ''), ']','')) > 0", $id)->get()
That's the final query to get aggregated conversations where user either created or is part of.

Laravel conditional on relation: return first()

I have a Laravel 5.2 one-to-many relation and I want to return the model and put a condition to relation.
I've tried this:
$categories = Category::with(['descriptions' => function($d) use ($default_language) {
$d->where('language_id', $default_language->id);
}])->get();
It work fine, I just want something else: the relation should not be a collection or array, just a simple object. I want to do something like
$d->where('language_id', $default_language->id)->first();
, just in this case first() is not working. Any ideas?
EDIT
Actually first() is not working properly, it returns first description just for the first object returned, for others it return nothing.
Try this:
$categories = \Jobinja\CMS\Blog\Models\Category::with([
'descriptions' => function ($q) use ($defaultLanguage) {
return $q->where('language_id', $defaultLanguage->id)->take(1);
}
])
->get()
->map(function ($item) {
if ($item->descriptions->isEmpty() === false) {
$item->description = $item->descriptions->first();
}
return $item;
});
and get to description:
foreach ($categories as $category) {
$description = $category->description;
}
You can't do that but you can use first() on a collection later, for example in a view:
#foreach ($categories as $category)
{{ $category->descriptions->first()->name }}
#endforeach
I can say to use instead of first() find() and give it the language_id or $default_language->id and this will try to find in the table first the column id and assign the value. If you have different id column name give it to the find like find(['your_column_name' => '<your_value']).
If you want array to something like ->toArray(). You can test different scenarios in tinker. php artisan tinker
Here is a link to document this -> https://laravel.com/docs/5.3/eloquent#retrieving-single-models

Laravel Eloquent Pagination orderBy first

I have a simple pagination from my model User:
$users = User::paginate(15);
Unfortunately, it returns the results ordered by id or something.
I want to order the results ASC first and then paginate on them.
Can't find any way to get that working in combination with the paginate() method, like:
$users = User::orderBy('username', 'asc')->paginate(15);
Any idea?
Thanks
I already test it like your code. It works. Can you give the error.
Route::get('/view-tags', function()
{
$tag = App\Tag::orderBy('name', 'asc')->paginate(5);
dd($tag);
});

Resources