I have the following function in the Controller:
public function index($id)
{
$client_name_tasks = Client::select('clients.name')
->join('projects', 'clients.id', '=', 'projects.client_id')
->join('tasks', 'projects.id', '=', 'tasks.project_id')
->where('tasks.project_id', $id)
->distinct()
->get();
return view('task.index', compact('client_name_tasks'));
}
where $client_name_tasks is collection.
Since the following view is called from different parts of my project, the title h1 is different based on where it is called (are 3 different).
<div class="col-12 col-md-10 d-flex justify-content-center">
#isset($client_name)
<h1>List of overall tasks of {{$client_name}}</h1>
#endisset
#isset($client_name_tasks)
#foreach($client_name_tasks as $client_name_task)
<h1>List of {{$client_name_task->name}} project tasks</h1>
#endforeach
#else
<h1>List all task</h1>
#endisset
</div>
I get error compact(): Error $client_name_tasks.
The problem is in the view, isset.
Can anyone kindly help me?
You can check the count value of that collection.
<div class="col-12 col-md-10 d-flex justify-content-center">
#isset($client_name)
<h1>List of overall tasks of {{$client_name}}</h1>
#endisset
#if(count($client_name_tasks) > 0)
#foreach($client_name_tasks as $client_name_task)
<h1>List of {{$client_name_task->name}} project tasks</h1>
#endforeach
#else
<h1>List all task</h1>
#endif
</div>
why don't you do a foreach in your controller in order to bring putting it in the compact back only the variable in the isset?
foreach ($client_name_tasks as $client_name_task){
$name_client=$client_name_task->name;
}
and in the view write this way (attention that name_client is different from client_name):
#if(isset($name_client))
<h1>List of {{$client_name_task->name}} project tasks</h1>
#elseif(isset($client_name))
<h1>List of overall tasks of {{$client_name}}</h1>
#else
<h1>All task</h1>
#endif
Related
I'm making a "teacher's" app, and I want to make a log-in page which changes depending if there's registered users in the database or not.
I want to make a redirection button to a create user page if there aren't auth users in database, and to make a select user view if the database have one or more users.
The problem is that I don't know how to exactly do this, 'cause the view always shows me the first statement (what I've got in the if), also if in the database are registered users. Can anyone help me with this please?
This is the blade file:
#if (empty(Auth::user()->id))
<div class="grid-item" id="grid-item5">
<div id="title">
<h1>Welcome</h1>
<p>We see there aren't users</p>
</div>
<div id="loginForm">
<button type="button" onclick="window.location='{{ url("/newUser") }}'">Button</button>
</div>
</div>
#else
<div class="grid-item" id="grid-item5">
<div id="title">
<h1>Select an user</h1>
</div>
<div id="loginForm"></div>
</div>
#endif
Here you have the controller index method:
public function index()
{
$users = User::all();
return view('/', compact('users'));
}
And finally here you have the page:
The following code is the sample for it, kindly replace code accordingly
#if(!$user)
//show button
#else
//dont show button
#endif
I think your question is you want to check if there is user in database.
So no need to check if the user authenticated but to check if there is user on the database.
In your controller
public function index() {
return view('/', ['users' => User::all()]);
}
and in your blade file
#if(!$users)
<div class="grid-item" id="grid-item5">
<div id="title">
<h1>Welcome</h1>
<p>We see there aren't users</p>
</div>
<div id="loginForm">
<button type="button" onclick="window.location='{{ url("/newUser") }}'">Button</button>
</div>
</div>
#else
<div class="grid-item" id="grid-item5">
<div id="title">
<h1>Select an user</h1>
</div>
<div id="loginForm"></div>
</div>
#endif
This function will get the current authenticated user: Auth::user(). I guess what you are trying to achieve is #if(empty($users)) where $users is the variable you are passing on controller.
If you want to verify if the user that accessed to that view is authenticated you can simply use #auth and #guest.
Also i would suggest you to change your button to an <a> tag and your href would be <a href="{{ route('route.name') }}" where route.name would be defined in your routes file.
in your controller:
you can create a folder inside views called users and then the index.blade.php (views/users/index.blade.php)
public function index()
{
$users = Users::all();
return view('users.index')->with('users', $users);
}
in your view:
#if(count($users) < 1)
...
#else
...
#endif
count is validating if the $users array length is less then 1 (in other words if the array is empty).
Alternative you can you isEmpty()
#if($users->isEmpty())
...
#else
...
#endif
I have a strange problem, I am working on a personal project in Laravel, a "Q&A" type site, so far everything is fine. When I want to post a question from the database, it will be displayed twice on the display page, even if I only put the question with a certain id.
the error that appears
Route:
Route::get('/viewUserQuestion/{post}', 'PostsController#viewUserQuestion')->name('viewQuestion');
Controllerr:
public function viewUserQuestion() {
if(Auth::check()) {
$posts = Post::latest()->get();
return view('viewQuestion', compact('posts'));
}
else {
return redirect('register');
}
}
Blade:
<div class="card-body p-0">
#foreach($posts as $post)
<div class="mailbox-read-info">
<h5 align="center"> {{ $post->title }}</h5>
<h6> From userID: {{ $post->user_id }}</h6>
<span class="mailbox-read-time" align="center">Created at: {{ $post->created_at }}</span></h6>
</div><div class="mailbox-read-message">
<p>{{ $post->content }}</p>
Route to the display page:
<td>{{ $post->id }}</td>
What do you think would be the exact problem? I would be grateful if you could help me, does it run twice or does it?
Basically I want to display the data from each id, but in different pages without overlapping. As it is here, you click on a question and display it not twice.
I'm making an attemp to write advanced Eloquent search query filters but when I return the collection to view, I get the following error
Call to undefined method Illuminate\Database\Query\Builder::links()
My Eloquent query builder:
$catalogos = Catalogo::query();
$catalogos->where('catalogo.subcategoria',$filtro)
->join('productos', 'productos.catalogo_id','=','catalogo.id')
->join('productos_precios','productos_precios.segment','=','productos.codigo_oracle')
->join('fotos_productos','fotos_productos.producto_id','=','productos.id')
->select('catalogo.descripcion', 'catalogo.descripcion_corta','catalogo.id','fotos_productos.nombre as ruta', 'productos_precios.precio')
->whereNotNull('fotos_productos.nombre')
->orderBy('precio', 'asc')
->groupBy('catalogo.id')
->paginate(24);
And yes, if I dd($catalogos), it returns me a Builder instance, but why?
My view code is as follows (just in case you're thinking I'm overwriting the value of the $catalogos variable):
#foreach($catalogos as $catalogo)
<div class="producto_tienda">
<a href="{{route('detalle-producto', [$catalogo->id,$catalogo->slug] ) }}">
<div class="foto_producto" style="background-image:url('{{ asset("public/images/tienda/".$catalogo->ruta) }}')">
#if($catalogo->stock>0 && $catalogo->stock<10)
<div class="etiquetas">
<p class="existencia">Ăšltimos productos</p>
</div>
#endif
#if($catalogo->stock==0)
<div class="etiquetas">
<p class="descuento">Agotado</p>
</div>
#endif
</div>
</a>
<a href="{{route('detalle-producto', [$catalogo->id,$catalogo->slug])}}">
<div class="descripcion_producto">{{ $catalogo->descripcion_corta }}</div>
</a>
<div class="precio_producto">{{ money_format( '%.2n' ,$catalogo->precio) }}</div>
</div>
#endforeach
<div class="text-center">
{{ $catalogos->links() }}
</div>
You are not assigning the return value of paginate to anything. It should be:
$catalogos = Catalogo::query()->where('catalogo.subcategoria',$filtro)
->join('productos', 'productos.catalogo_id','=','catalogo.id')
->join('productos_precios','productos_precios.segment','=','productos.codigo_oracle')
->join('fotos_productos','fotos_productos.producto_id','=','productos.id')
->select('catalogo.descripcion', 'catalogo.descripcion_corta','catalogo.id','fotos_productos.nombre as ruta', 'productos_precios.precio')
->whereNotNull('fotos_productos.nombre')
->orderBy('precio', 'asc')
->groupBy('catalogo.id')
->paginate(24);
Now it appears to be working with this:
$catalogos = Catalogo::query();
$catalogos = $catalogos->where('catalogo.subcategoria',$filtro)
->join('productos', 'productos.catalogo_id','=','catalogo.id')
->join('productos_precios','productos_precios.segment','=','productos.codigo_oracle')
->join('fotos_productos','fotos_productos.producto_id','=','productos.id')
->select('catalogo.descripcion', 'catalogo.descripcion_corta','catalogo.id','fotos_productos.nombre as ruta', 'productos_precios.precio')
->whereNotNull('fotos_productos.nombre')
->orderBy('precio', 'asc')
->groupBy('catalogo.id')
->paginate(24);
From the docs:
Currently, pagination operations that use a groupBy statement cannot be executed efficiently by Laravel. If you need to use a groupBy with a paginated result set, it is recommended that you query the database and create a paginator manually.
You should follow the documentation and create a manual paginator for your query.
https://laravel.com/docs/5.4/pagination#manually-creating-a-paginator
I have this query and i want to return the 5 top results in laravel but the take() and first() dont work at all it works only with get().The result are for each $id
The query
$user_lessons = DB::table('Home_LogStudents')
->join('LessonUnitSections','LessonUnitSections.leuns_ID','=','Home_LogStudents.LogSt_sectionID')
->join('LessonUnits','LessonUnits.leun_ID','=','LessonUnitSections.leuns_ID')
->join('Lessons','Lessons.less_ID','=','LessonUnits.leun_ID')
->where('Home_LogStudents.LogSt_action',101)
->where('Home_LogStudents.LogSt_studid',$id)
->orderBy('Home_LogStudents.LogSt_date','desc')
->get();
Blade
<div class="row">
<div class="col-md-12">
<div class="form-group">
#foreach($user_lessons as $ul)
{{ $user_lessons->less_Title }}
#endforeach
</div>
</div>
</div>
Any suggestions im doing this wrong ?
you could either add ->limit(5) to your query, either use ->take(5) you still have to end your query with ->get();.
Ok so I'm trying to setup a scope that will restrict which users are returned based on a column in a hasOne relation.
Here are the two methods from my User model:
public function scopeHasImage($query)
{
return $query->with(['profile' => function($query){
$query->where('avatar', '!=', '');
}]);
}
public function profile()
{
return $this->hasOne('App\Profile');
}
When I call it like so:
$users = User::HasImage()->simplePaginate(15);
All of that works ok untill I go to display the code in my blade template like so:
#foreach($users as $user)
<div class="col-xs-12 col-sm-3 col-md-2">
<a href="{{ route('user.profile.public', ['id' => $user->id]) }}">
<img class="img-responsive" src="{{ $user->profile->avatar }}" />
</a>
</div>
#endforeach
That results in this error:
Trying to get property of non-object
I have dumped the $user->profile and it is an object which has attributes listed and one of those attributes is avatar. So I'm not sure why this is not working.
Any thoughts would be greatly appreciated.
Almost certainly one of your users has no profile attached. Because you probably dumped only the the first $user->profile you didn't see that...
You can fix this by wrapping it in an if:
#foreach($users as $user)
#if($profile = $user->profile)
<div class="col-xs-12 col-sm-3 col-md-2">
<a href="{{ route('user.profile.public', ['id' => $user->id]) }}">
<img class="img-responsive" src="{{ $profile->avatar }}" />
</a>
</div>
#endif
#endforeach
Or exclude users without a profile in the first place (which is probably what you wanted all along)
public function scopeHasImage($query)
{
return $query->with('profile')->whereHas('profile', function($query){
$query->where('avatar', '!=', '');
});
}