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
Related
here is my code:
$data = Courses::select('courses.id','courses.standid', 'courses.publisher', 'courses.coverpic', 'courses.course_sts', 'standards.standtitle AS stitle', 'subjects.subtitle AS btitle')
->join('subjects', 'subjects.id', '=', 'courses.subjectid')
->join('standards', 'standards.id', '=', 'courses.standid')
->get();
and blade codes are:
#foreach ($data as $row)
<p>{{ $row->stitle }}</p>
#endforeach
the query works fine when i check dd($data) under attributes, but the stitle and btitle columns never shows inside blade as {{ $row->stitle }} or {{ $row->btitle }}
what am I missing or how to collect the joining table column into the blade file?
cheers
Thanks for response. It was the redirect method that taking me back to history page rather then the post update status.
it was return redirect (the_view_name)->with(vars)
instead of return view (the_view_name)->with(vars)
my first experience to ask a question at stackoverflow and resolved by myself.
I have created a relationship between products and users using a foreign key called "previous_owner". I can see when I dump the value that I'm passing to the view that the data I'm after is available under:
$product->relations->product_owner->user->attributes->name
But how do I access it from within my view. I'm trying to loop through the products and then do something like:
{{ $product->product_owner->user->name }}
or
{{ $product->product_owner->name }}
But non of it works, keep getting:
" Trying to get property 'name' of non-object "
In your view code must be similar this
#foreach($products as $product)
{{ $product->product_owner->name }}
#endforeach
This code mean each product has product_owner. When one product does not have product_owner that case value of $product->product_owner is Null and then your code is null->name and you get error. For fix it you must be check relation is null or use ?? operator. Change your code to
#foreach($products as $product)
{{ $product->product_owner->name ?? ''}}
#endforeach
or
#foreach($products as $product)
#if($product->product_owner)
{{ $product->product_owner->name}}
#endif
#endforeach
If name is attribute and product_owner is relationship function then try the below:
{{ $product->product_owner()->name }}
Your product_owner isn't stdClass. It is array. So you can't access this key of this array with "->" operator. try
{{ $product->product_owner['name'] }}
Pretty new to Laravel and trying to figure out the ins and outs.
If I write one main query that selects everything in my table, can I write individual queries when selecting columns and rows from the table.
Example:
Main Query:
public function allTickets(){
$tickets = DB::table('tickets')->get();
return view('admin.index',compact('tickets'));
In my view:
//THIS WORKS. It gives me a count of all open and closed tickets
{{$tickets->where('status', '=', 'OPEN')->Count()}}
{{$tickets->where('status', '=', 'CLOSED')->Count()}}
However this does not work...
Error: Call to a member function where() on string (View: ...
#foreach ($tickets as $ts)
{{$ts->subject->where('status', '=', 'OPEN')}}
#endforeach
#foreach ($tickets as $ts)
{{$ts->subject->where('status', '=', 'CLOSED')}}
#endforeach
Is there a way to use that ONE main query and show all the subjects of the tickets that are open and closed, or will I need to write multiple queries in my controller to achieve this?
You can do like this with simple solution.
first write one and then another one with if statement
#foreach ($tickets as $ts)
#if($ts->status == 'OPEN')
{{$ts->subject}}
#endif
#endforeach
#foreach ($tickets as $ts)
#if($ts->status == 'CLOSED')
{{ $ts->subject }}
#endif
#endforeach
I use Laravel with foreach in a blade.php
{{ $users = App\User::where('user_id', $post->user_id)->get() }}
and I got the result of this which show on the page
[{"user_id":"123","email":"123#com","password":"1234","first_name":"Tony"}]
But I want to get "Tony" only, how can I call?
{{ ($users = App\User::where('user_id', $post->user_id)->first())->first_name }}
Or better in sense of performance, if you select posts with user:
{{ $post->user->first_name }}
But it is better to prepare necessary data in controller.
get() returns an array, you should use first()
{{ ($users = App\User::where('user_id', $post->user_id)->first())->first_name }}
Since you said you are using foreach in blade. Try something like this
#foreach($user as $u)
{{$u->first_name}}
#endforeach
This will give you first_name for all users
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