I am using Laravel 5.3,
There is something wrong in the view below:
controller:
$user=\Auth::user();
$articles = $user->articles;
return view('articles.index', compact('articles'));
view:
#if ($articles!= null)
<p>Yes</p>
#else
<p>No</p>
#endif
Question:
When no articles return,it still show "Yes".
Is $articles!= null not right?
$user->articles is most likely a Collection which is why it doesn't pass your null check. $articles is probably returning an empty Collection which is not the same as null.
Instead you want to check for $articles->count() or $articles->isEmpty(). Your view would look like:
#if (!$articles->isEmpty())
<p>Yes</p>
#else
<p>No</p>
#endif
or
#if ($articles->count())
<p>Yes</p>
#else
<p>No</p>
#endif
See https://laravel.com/docs/5.2/collections#method-count or https://laravel.com/docs/5.2/collections#method-isempty
Related
I need to output the creation date, but sometimes there may not be a date, for this I did in blade.php #forelse, but it doesn't help
Controller
$dataBumps[] = DB::table('server_user_likes')
->where('server_id', $server->id)
->orderBy('created_at', 'DESC')
->first();
blade.php
#forelse ($dataBumps as $dataBump)
{{$dataBump->created_at}}
#empty
-
#endforelse
You are storing also null values into the array.
You can try to dd($dataBumps); to debug it and add a check in your blade file.
A simple example
#forelse ($dataBumps as $dataBump)
#if ($dataBump !== null)
{{$dataBump->created_at}}
#else
-
#endif
#empty
-
#endforelse
I'm trying to get all the users where a given method in User model meets. Please see my code below:
User.php
public function isPicker(){
return ($this->where('isPicker', 1)) ? true : false;
}
Now, I can use User::all();, but it returns all the users. What I want is to only return the users that meets the isPicker() method. What I'm trying to do in view is:
#foreach($users as $user)
#if($user->isPicker())
{{ $user->first_name }}
#endif
#endforeach
This is also working fine, but it is not that efficient to use. What if there's a lot of method to check? Any idea for this?
Just do:
$users = User::where('isPicker', 1)->get();
Or create a scope:
public function scopeIsPicker($query)
{
return $query->where('isPicker', 1);
}
// usage
$users = User::isPicker()->get();
Well you could change you code up a little to look like this.
#foreach($users->where('isPicker', 1)->all() as $user)
{{ $user->first_name }}
#endforeach
But this will only work if the users var is a collection.
Other wise just change you query on how your getting the users to something like this.
User::where('isPicker', 1)->get()
Instead of checking in model file, you can directly query in your controller try below code
if you want to display only isPicker=1 users.
Controller Code :
$users = User::where('isPicker', 1)->get();
Blade code :
#foreach($users as $user)
{{ $user->first_name }}
#endforeach
OR
if you want to display all users including isPicker 0&1.
Controller Code :
$users = User::all();
Blade code :
#foreach($users as $user)
#if($user->isPicker == 1)
{{ $user->first_name }}
#else
<p>Picker is 0</p>
#endif
#endforeach
Note : Remove your isPicker function from user model file because its unuse.
in my laravel5 controller I take some rows from DB:
$order = Order::select('ordered_by')->where('column1','=','1')->get();
then in my view, where I render a table,
#foreach($users as $user)
#if(strpos($order, $user->id) == true) match! #endif
#endforeach
I get result
[{"ordered_by":"309"},{"ordered_by":"524"},{"ordered_by":"541"},{"ordered_by":"545"}]
Why strpos not working? (Actually it gives me "match!" in table only for user #546)
Tried this:
#if (in_array($user->id,$order) ==true ) ok #endif
but it shows me error in_array() expects parameter 2 to be array, object given
How can I check if $user->id exists in $order output?
Try this code:
$order = Order::where('column1','=','1')->get();
$orders = array_pluck(collect($order)->toArray(), 'ordered_by');
#foreach($users as $user)
#if(in_array($user->id, $orders))
ok
#endif
#endforeach
strpos — Find the position of the first occurrence of a substring in a string
You can got it using in_array
Try given script
$order = Order::select('ordered_by')->where('column1','=','1')->get()->toArray();
#foreach($users as $user)
#if (in_array($user->id,$order)) ok #endif
#endforeach
This is very strange my subscription site was working yesterday but today it is giving me the error - Call to a member function subscribed() on null
I have checked my edited code in the last day and nothing obvious stands out.
My code is...
#if (!$user->subscribed())
<h1>You are not currently subscribed!</h1>
SUBSCRIBE
#elseif(Auth::guest())
<h1>You need to be a member to view todays tips!</h1>
LOGIN
REGISTER
#else
#forelse($todaystips as $tip)
<h3>{{ $tip->league }}</h3>
<h2>{{ $tip->home_team }} V {{ $tip->away_team }}</h2>
<h3><b>{{ $tip->tip }}</b> {{ $tip->odds }}</h3>
#empty<h1>There are no tips today! Check back tomorrow!</h1>
#endforelse
#endif
and the controller code is
public function home()
{
$user = Auth::user();
return view('home')->with(['user' => $user,]);
}
You need to check Auth::check() before using Auth::user()
Something like this:
#if (Auth::check() && !$user->subscribed())
I'm trying to get the total comments the user have..
Controller:
public function index()
{
$setting = Setting::findOrFail(1);
$comments = Comment::where('published', '=', '1')->get();
$users = User::all();
return view('page.index', compact('setting', 'comments', 'users'));
}
View:
#foreach($comments as $comment)
{{ count($users->where('user_id', '=', $comment->user_id)) }}
#endforeach
The problem is that it only returns 0 and i have 2 comments there.. even using the user id to instead of "$comment->user_id" it doesnt work. still display 0.
$users is a collection, not a query. Treat it as such:
#foreach ($comments as $comment)
{{ $users->where('user_id', $comment->user_id)->count() }}
#endforeach
The collection's where method does not take an operator.
From the wording in your question it seems you actually want it the other way around:
$comments = Comment::wherePublished(1)->get()->groupBy('user_id');
Then in your view:
#foreach ($users as $user)
{{ $comments->has($user->id) ? count($comments[$user->id]) : 0 }}
#endforeach
I'm late to answer your question and Joseph already showed you the problem but you may also do it differently. You can group comments using Collection::groupBy, for example:
$comments = Comment::all()->groupBy('user_id');
Then in your view you may try this:
#foreach($comments as $user => $userComments)
// count($userComments)
// Or
#foreach($userComments as $comment)
// {{ $comment->title }}
#endforeach
#endforeach
In the first loop (#foreach($comments as $user => $userComments)), the $user is the user_id and it's value would be all comments (an array) by this user but in group.