why in search-bar using laravel livewire doesn't work - laravel

i want to show the user but it doesn't work and i wrote
#livewireScripts and #livewireStyles
i dont know why i found search tutorial at livewire docs
and doing the same but don't work also and not showing up any name of users
here is my code
my test.blade.php
#extends('layouts.master')
#section('css')
#livewireStyles
#section('title')
empty
#stop
#endsection
#section('page-header')
<!-- breadcrumb -->
<div class="page-title">
<div class="row">
<div class="col-sm-6">
<h4 class="mb-0"> </h4>
</div>
<div class="col-sm-6">
<ol class="breadcrumb pt-0 pr-0 float-left float-sm-right ">
<li class="breadcrumb-item">Home</li>
<li class="breadcrumb-item active">Page Title</li>
</ol>
</div>
</div>
</div>
<!-- breadcrumb -->
#endsection
#section('content')
<!-- row -->
<div class="row">
<div class="col-md-12 mb-30">
<div class="card card-statistics h-100">
<div class="card-body">
#livewire('counter')
</div>
</div>
</div>
</div>
<!-- row closed -->
#endsection
#section('js')
#livewireScripts
#endsection
my counter.blade.php
<ul>
#foreach($users as $user)
<li>{{ $user->name }}</li>
#endforeach
</ul>
my route when i show the component
Route::view('test', 'test');
my Counter.php of livewire
<?php
namespace App\Http\Livewire;
use App\User;
use Livewire\Component;
class Counter extends Component
{
public $search = '';
public function render()
{
return view('livewire.counter', [
'users' => User::where('name', $this->search)->get(),
]);
}
}

You have two issues here, one is that you never update your search property, and the other is - because you never update the search-property - you always search for those users who have an empty name (which is not null).
You should be making three changes, one to create an input-element that is bound to the $search proeprty, one to make the search a "like" operator (meaning that if you search for an empty string, all results will be shown, and if you have a user called Foobar and you search for Foo, it will show up in the results) and finally - because your results can change when you change your search criteria - you should be putting a key the list-elements, so Livewire know which row is which. The key should be something that is unique across your page.
First, add a new input-element to your counter view. This will be the search-property in your component. Because Livewire requires you to have only one root element in the view, we wrap it all in a <div>. This is also where we add the key to the different results.
<div>
<input wire:model="search" placeholder="Search users.." />
<ul>
#foreach($users as $user)
<li wire:key="user-{{ $user->id }}">
{{ $user->name }}
</li>
#endforeach
</ul>
</div>
Then, we need to update your retrieval of the results, in order to implement the like functionality. We put SQL wildcards % around the search to make this happen.
return view('livewire.counter', [
'users' => User::where('name', 'like', '%'.$this->search.'%')->get(),
]);

Take a deep look into what you are doing in your Livewire Component. You have initialized $search=''
Next, you are calling Users::where('name', $this->search)->get(). But at this point $this->search is an empty string. So it's either not returning users from your database or retrieving users without a name.
Finally, you are trying to display {{$user->name}} which even if there were users in your database, so either you don't have any users in your database without a name or you won't see anything because those users don't have a name.
You are missing a Search Input whose value is bound to the $search variable.
Add the following to your counter.blade.php
<input type="text" wire:model="search" />
And when you type a name in that field it should begin displaying in your list.

Related

Can't get id from previous page Laravel

I have a button to see detail about user using button in each row datatable, the button will call a route detail.show and open link with user_id with it (ex:http://127.0.0.1:8000/detail/7),
here is the button code
->addColumn('action', function ($post) {
$link = "route('detail.show',$post->user_id)";
$btn = ' <span class="fas fa-eye"></span>';
return $btn;
})
the problem is I cant get any data from it, is there something wrong?
here is my route
Route::resource('detail', DetailController::class);
Here is my controller
public function show($user_id)
{
$post = Post::where('user_id', $user_id)->latest()->get();
return view('detail', compact('post'));
}
and here is my view
#extends('layouts.app')
#section('content')
<div class="row mt-5 mb-5">
<div class="col-lg-12 margin-tb">
<div class="float-left">
<h2> Show Post</h2>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>User ID:</strong>
{{ $post->user_id }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Name:</strong>
{{ $post->name }}
</div>
</div>
</div>
#endsection
I cant show anything using code as shown above. Is there something wrong? thanks in advance
Problems like this can often be solved by structuring your logic to be cleaner, more straightforward and use constructs of the framework as intended (documented).
In this case I would treat a user as resource and not the "detail".
I would rather create a specific route for showing the specific details of the user or just use partial resource routes.
Anyway, if you would like to keep your current build:
If I remember correctly, parameter passed to the show function in the controller should match the parameter name in your route.
When you generate routes with
Route::resource('detail', DetailController::class);
it creates something like
/details/{detail}
for the show method.
As described in this part of the docs.
Try replacing
$user_id
with
$detail
in your show method.

How to change which template is extended based on user authentication

I am using Laravel 6.20.11, and I have published the Laravel blade error templates. What I need to have happen is that if the user is authenticated, the error message extends the internal-only layout, and if not, it extends the public layout like so:
#auth
#extends ('int-layouts.partials.wrapper')
#section ('error')
<div class="be-content">
<div class="main-content container-fluid">
<div class="container py-5">
<div class="row">
<div class="col-lg-8">
<div class="text-block">
<h1>404 - Not Found</h1>
<p class="text-muted text-uppercase mb-4">Sorry, we couldn't find the page you were looking for.
Please contact the administrator<br/>
Or go back to the <a class="navbar-brand py-1" href="/controlpanel/">Dashboard</a></p>
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
#endauth
#guest
#extends ('ext-layouts.master')
#section('error')
<div class="container py-5">
<div class="row">
<div class="col-lg-8">
<div class="text-block">
<h1>404</h1>
<p class="text-muted text-uppercase mb-4">Sorry,You are trying to reach the page that does not exist.<br>
Please contact the administrator <br/> Or go back to the <a class="navbar-brand py-1" href="/">Home Page</a></p>
</div>
</div>
</div>
</div>
#endsection
#endguest
The problem I am having is that when I test the 404 error using abort(404) or by trying to access a route that doesn't exist, the error message displays twice and the page inherits the int-layouts.partials.wrapper even when the user isn't authenticated.
Is this expected behavior or am I doing something wrong in my blade file?
You can try to use a conditional in one #extends directive:
#extends (auth()->check() ? 'int-layouts.partials.wrapper' : 'ext-layouts.master')
Then you can use a conditional after this inside your section.
#section('error')
#auth
#else
#endif
#endsection
The #extends directive is not actually returning output directly into the compiled result like most directives do. The compiled PHP for this is held in an array to be added later.
Everything inside the ( ) of the directive is being used as a literal to call make on the View Factory:
"<?php echo \$__env->make({$expression}, \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>"
If you found the compiled view for this Blade template you would see this:
<?php echo \$__env->make(auth()->check ? 'int-layouts.partials.wrapper' : 'ext-layouts.master', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>
The first argument to make ends up being your view name when this compiled view is executed.
In Controller function:
$layouts = Auth::user->admin ? 'view.admin' : 'view.student'; //
view.admin or view.student is the blade file which you want to render
dynamically.
return view('welcome',compact('layouts'));
Write in Blade File:
#extends($layouts)
#section('...')
...
#endsection

URL with two parameters

I want to generate URL with two parameters. In my web.php I created a route:
Route::get('/pojedinacni-turnir/{godina}/kolo/{kolo}', [
'uses' => 'Frontend\PojedinacniTurnirController#show',
'as' => 'pojedinacni.turnir',
]);
where are my two parameters are godina and kolo.
In my Controller I create show function with two parameters: id from godina and id from kolo.
Here is code from my controller:
<?php
namespace App\Http\Controllers\Frontend;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Sezona;
use App\TurnirPojedinacni;
class PojedinacniTurnirController extends Controller
{
public function show($id_sezona, $id_kolo)
{
$sezone = Sezona::orderBy('godina', 'desc')->get();
$sezona = Sezona::findOrFail($id_sezona);
$kola = TurnirPojedinacni::where('sezona_id', $id_sezona)->orderBy('id', 'asc')->get();
$kolo = TurnirPojedinacni::findOrFail($id_kolo);
return view('frontend.pojedinacni_turnir', compact('sezone', 'sezona', 'kola', 'kolo'))->render();
}
}
When I try to check my URL i get this message:
Missing required parameters for [Route: pojedinacni.turnir] [URI: pojedinacni-turnir/{godina}/kolo/{kolo}]. (View: C:\WebSites\TkPazin\TK_Pazin\resources\views\frontend\pojedinacni_turnir.blade.php)
I don't understand error because i try URL with two parameters. I created URL with id's that exist like this:
{{ route('pojedinacni.turnir', ['godina' => 1, 'kolo' => 1]) }}
and even then i get the same error message.
Update: added blade file
#extends('layouts.frontend')
#section('title', 'TK Pazin | Pojedinačni turnir')
#section('css')
<link href="/css/style_pojedinacni_turniri.css" rel="stylesheet"/>
#endsection
#section('content')
<!-- Page Content -->
<div class="container">
<!-- Page Heading -->
<h1 class="my-4" style="text-align:center; color: #ba3631;">Pojedinačni turniri {{ $sezona->godina }}</h1>
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
#foreach($sezone as $sezona)
<li class="breadcrumb-item">{{ $sezona->godina }}</li>
#endforeach
</ol>
</nav>
<div class="row mb-5">
<div class="col-12 col-md-2 mb-5">
<div class="list-group">
#foreach($kola as $kolo)
<button type="button" class="list-group-item list-group-item-action">{{ $kolo->naziv }}</button>
#endforeach
</div>
</div>
</div>
</div>
<!-- /.container -->
#endsection
You blade file shows that you have only added 1 parameter
#foreach($sezone as $sezona)
<li class="breadcrumb-item">{{ $sezona->godina }}</li>
#endforeach
Add the additional parameter
{{ $sezona->godina }}

Display nothing when insert third record laravel

I'm developing a web app project of my client where people can ask there questions, like stack-overflow, I inserted 1 record manually into database it showed on the page and then I inserted 2nd record, it showed two records on the page but when i inserted 3rd record into database, nothing is showing on the page I checked the console there is no error there then I inspected the page inside body tag this is showing only <!----> I viewed the page source there code is displaying fine, then I removed the 3rd record everything is fine after that I inserted third record again the content of page gone again, this is my code of page...
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-sm-8 offset-md-2">
#foreach ($questions as $question)
<div class="card mb-2">
<div class="card-header">
<h5 class="bold">{{ $question->question_title }}</h5>
</div>
<div class="card-body">
<p class="card-text">{{ (strlen($question->question_body) > 50) ? substr($question->question_body, 0, 50) : $question->question_body }}</p>
Continue Reading
<p class="text-muted float-right">{{ date('M j, Y h:ia', strtotime($question->created_at)) }}</p>
</div>
</div>
#endforeach
</div>
</div>
</div>
#endsection
and here is the QuestionController:
public function index()
{
$questions = Question::all();
return view('questions.index', compact('questions'));
}
To find out the issue, do this two things:
use dd() function in your controller and check the data
dd($questions)
check if there are any error in the Laravel log
storage/logs/laravel.log
Hope that help you find the problem and possible fix.
I think there is an issue with your html.Take your #foreach statement out and place it just before <div class="row">. If that works, you'll know that its in html.
Oh and adjust #endforeach accordingly.

Show a collection in laravel from controller on different columns in view

got this problem:
I want to show the info from a collection where a publication has a "featured" column in true.
$pub_destacadas = Publicaciones::take(3)->where('destacado', '=', 1)->get();
The problem is that my layout has 3 elements to fill with this information that have different sizes so i wasn't able to solve this using the chunk() method in the foreach in my blade template, so I tried the following:
$individual_destacada = $pub_destacadas->take(1);
$grupo_destacada = $pub_destacadas->take(2);
and show them in the view as follows:
<div class="publicaciones-destacadas">
<div class="container">
<h2>Publicaciones Destacadas</h2>
<div class="row">
#foreach($individual_destacada as $destacada)
<div class="col-md-6">
<div class="item-destacado size-2">
<img class="img-responsive" src="{{ asset('img/publicaciones/' . $destacada->imagen ) }}">
{{ $destacada->titulo }}
{{ $destacada->descripcion }}
</div>
</div>
#endforeach
<div class="col-md-6">
#foreach($grupo_destacada as $destacada)
<div class="row doble-publicacion">
<div class="col-md-12">
<div class="item-destacado size-1">
<img class="img-responsive" src="{{ asset('img/publicaciones/' . $destacada->imagen ) }}">
{{ $destacada->titulo }}
{{ $destacada->descripcion }}
</div>
</div>
</div>
#endforeach
</div>
</div>
</div>
</div>
So... now it shows it correctly in the layout but the first item that the query gets is shown 2 times in this layout. How can I exclude this first element in the second variable where I get the featured publications?
Hope I explained the problem correctly.
Here's the layout:
You can use splice.
$individual_destacada = $pub_destacadas->splice(1);
$grupo_destacada = $pub_destacadas->splice(2);

Resources