Display nothing when insert third record laravel - 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.

Related

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

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.

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 not display duplicated data?

i want to show the categorie list without showing duplicated ones
my controller code :
public function showActualite(){
$actualites=DB::table('actualites')->distinct('categorie')->orderBy('created_at', 'DESC')->paginate(6);
return view ('AllActualite',['actualites' => $actualites]);
}
My view :
#foreach ($actualites as $act)
<span style="text-transform: capitalize;" > {{$act->categorie}} </span>
<span style="text-transform: capitalize;"> </span>
#endforeach
</div>
<div class="row">
#foreach($actualites as $act)
<div class="col-lg-6">
<div class="box wow fadeInLeft">
<div class="icon"><i class="fa fa-newspaper-o"></i></div>
<h4 class="title">{{$act->categorie}}</h4>
<p class="description">{{$act->titre}}</p>
</div>
</div>
#endforeach
I believe your code should work, but in general i would not use DB logic. A more Laravel version of this would be. Latest is a syntax sugar for the same order by logic. See of this solves the problem.
$actualites = Actualites::query()->distinct('categorie')->latest()->paginate(6);
All your answer can be found in this post
$actualites= Actuality::distinct()->get(['categorie']);
Eloquent is used.

Laravel paginate not working in certain query but works in other query

As I am now putting a lot of data in my database I needed to use laravel's paginate. However, it doesn't seem to work in my other query. Is there other way? or what did I do wrong?
My code is shown below
Paginate Working (ExamController)
$exams = Exam::with('users')->where('id',$userID)->paginate(5);
Paginate Not Working (Questions Controller)
$questions = Question::with(['exams','choices'])->where('exam_id', $examID)->paginate(5);
Code to display pagination
{{ $exams->links() }} ---- exams view
{{ $questions->links()}} ---- questions view
Error
Call to undefined method App\Question::links()
View
<div class="card">
<div class="card-body">
#foreach($questions as $index => $questions)
{{$questions}}
#endforeach
</div>
<div class="mt-3 d-flex justify-content-end text-right">
{{$questions->links()}}
</div>
</div>

Laravel 5.0 paginate jump to page section

I'm using Laravel 5.0 and paginate along with the render() function in my view. Rather than go back to the top of the screen after I select the next page I want it to jump to #msgs.
Here is some of my code:
<h1 class="centered" id="msgs">Announcements</h1>
#foreach($messages->getCollection()->all() as $message)
<div class="row">
<div class="row">
<div class="col-sm-9">
<h3>{{$message->title}}</h3>
</div>
<div class="col-sm-3">
<h4>From: {{$message->author_name}}</h4>
</div>
</div>
<div class="row">
<p>{{$message->text}}</p>
</div>
</div>
<hr class="featurette-divider">
#endforeach
<div class="centered">
{!! $messages->render() !!}
</div>
I'm still getting a grasp on the flow of things in laravel so I'm not really sure what to do. I tried to use append->('#msgs') but realized that its appending an array in the parameters, not appending a path to the url. I there a simple way to achieve this without overriding anything?
You might want to use fragment() method:
{!! $messages->fragment('msgs')->render() !!}
Here is the link to the documentation: Pagination - Laravel

Resources