Laravel 5.0 paginate jump to page section - laravel-5

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

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

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.

Rendering HTML from database in vuejs component in laravel

I'm trying to fetch data from my database and display it as HTML content but it only turns out as a string, I'm using vuejs component in laravel. This is the code
<div id="main-window" v-for="post in posts">
<div class="post">
<div class="content">
{{post.content}}
</div>
</div>
How can I use the post.content as HTML?
You need to use v-html binding. Docs
<div id="main-window" v-for="post in posts">
<div class="post">
<div class="content" v-html="post.content">
</div>
</div>

Bootstrap classes for laravel 5 form submit button

Below is the part of input field and submit button.
I want to increase the width of submit button as the input field using bootstrap.
Here is my code.
<div class="form-group col-md-6">
{!! Form::label('Event Photo') !!}
</div>
<div class="form-group col-md-6">
<input type='file' name='photo' id ='photo' class ='form-control'/>
</div>
<div class="form-group col-md-12">
{!! Form::submit('Add', array('class'=>'btn btn-primary')) !!}
</div>
But the button size can't get increased like that. Where am I wrong? How should I solve that?
The array() part of your form allows you to pass in any other parameters that you want.
Just as you are using it to define the class, you can use it to define the style:
<div class="form-group col-md-12">
{!! Form::submit('Add', array('class'=>'btn btn-primary', 'style' => 'width:50px;)) !!}
</div>
Just change 50px to whatever width you want to use.
Adding the class btn-lg will work.
You can find that in the documentation

Resources