Blank page on laravel 5 - php

I just try to create a profile page i create a Route like this:
Route::get('profile/{id}/{name}','ProfileController#index');
and the index function like this :
public function index($id,$name)
{
$user = \App\User::find($id);
return view('pages.profile',compact('user'));
}
profile view:
#extends('app')
#section('content')
<div class="container" style="margin-top: 50px;padding: 30px;">
<div class="row">
<div class="col-sm-12 col-md-12 col-lg-12">
<div class="proPic"><img src="{{user->image}}" class="img-responsive" alt="Image"></div>
</div>
</div>
</div>
#endsection
but when I lunch the link i get a blank page

If you have a default installation, you probably need to have:
#extends('layouts.app')
instead of
#extends('app')
and change
{{user->image}}
to
{{ $user->image }}

Your code looks clean, a blank page can occur when laravel doesn't displays errors. I'd recommand you to :
1- Check the server response (HTTP code, content...)
2- Check your logs, maybe you'll discover a problem of configuration

Related

Trying to Save Comments but the application redirects me to an empty page

I've made a blog and now I'm trying to implement a comment section. I want it so that when the user tries to post, it's saves the comment and redirects the user to the same page. But when I write a comment and try to post it, the application redirects me to a different page. I'm learning how to make a blog with laravel, so I don't know when to use url and when to use routes. Here's the code that I've written.
#auth
<div class="card ml-5 col-lg-8">
<ul class="list-group list-group-horizontal">
<h5 class="list-group-item active">
Comments
<h5>
<div class="card-body">
<form method="post" action="{{url('save-comment/'.Str::slug($blog->title).'/'.$blog->id)}}">
#csrf
<textarea name="comment" class="form-control py-5"></textarea>
<input type="submit" class="btn btn-primary mt-3">
</div>
</ul>
</div>
#endauth
<div class="card ml-5 col-lg-8">
<h5 class="card-header mb-4">Comments<span class="badge badge-info ml-2"> {{count($blog->comments)}}</span></h5>
<div class="card-body mt-3">
#if($blog->comments)
#foreach($blog->comments as $comment)
<blockquote class="blockquote">
<p class="mb-0">{{$comment->comment}}</p>
<footer class="blockquote-footer">Username</footer>
</blockquote>
<hr>
#endforeach
#endif
</div>
</div>
BlogController :
function save_comment(Request $request,$slug,$id)
{
$request->validate([
'comment'=>'required',
]);
$data = new Comment;
$data->user_id=$request->user()->id;
$data->post_id=$id;
$data->comment=$request->comment;
$data->save();
return back();
}
Routes :
Route::get('/blog/', [App\Http\Controllers\BlogController::class, 'index'])->name('blog');
Route::get('blogs/{slug}','App\Http\Controllers\BlogController#getArticles')->name('article.show');
Route::get('blog.update/{id}','App\Http\Controllers\BlogController#edit');
Route::put('blog.update/{id}','App\Http\Controllers\BlogController#update');
Route::post('save_comment/{slug}/{id}','App\Http\Controllers\BlogController#save_comment')->name('save_comment');
Route::get('/admin/blog', 'App\Http\Controllers\BlogController#getBlog')->name('admin.blog');
If there's someone willing to assist come up with a solution to this problem, please assist me. I think the problem lies where I've written the url lies. When I change the url to route, it gives me an error of route not defined.
Route::resource('/blog','App\Http\Controllers\BlogController');
It redirects you to an empty page because you made a mistake on the url of your route. In your web.php file, your route is :
Route::post('save_comment/{slug}/{id}', 'App\Http\Controllers\BlogController#save_comment')->name('save_comment');
While in your form you wrote save-comment/ :
<form method="post" action="{{url('save-comment/'.Str::slug($blog->title).'/'. $blog->id)}}">
The error is due to this. I therefore advise you to modify the action in your form like this save_comment/:
<form method="post" action="{{url('save_comment/'.Str::slug($blog->title).'/'. $blog->id)}}">
This should be fixed !
Please change your code like this and check...
action="{{route('save_comment', $blog->id])}}"
Route::post('save_comment/{id}','App\Http\Controllers\BlogController#save_comment')->name('save_comment');
**BlogController**
function save_comment($id, Request $request)
{
$request->validate([
'comment'=>'required',
]);
$data = new Comment;
$data->user_id=$request->user()->id;
$data->post_id=$id;
$data->comment=$request->comment;
$data->save();
return back();
}

Cannot find the way to make #if statement works in blade

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

How to embed forms to another website using laravel

I have a two projects using laravel framework. I have a form for project 1 that I want to embed on my project 2.
Here is my FormController in project 1
public function show(Form $form)
{
return view('formbuilder.render', compact('form'))->render();
}
View on project 2 where I want to embed the form on project 1
#extends('layouts.app')
#section('content')
div class="card rounded-0">
<div class="card-body">
<script type="text/javascript" src="https://route/for/first-project-form"></script>
</div>
</div>
#endsection
I want to imitate how jot form works. https://www.jotform.com/help/34-Embedding-a-Form-to-a-Web-Page.
How do I achieve this?
Using an iframe it's just this simple:
#extends('layouts.app')
#section('content')
div class="card rounded-0">
<div class="card-body">
<iframe src="https://route/for/first-project-form"></iframe>
</div>
</div>
#endsection
If you really want to use a javascript file to load the iframe (can't see any reason why, but let's do it):
Create a javascript file:
const container = document.getElementById('form-container')
const iframe = document.createElement("iframe");
iframe.setAttribute("src", "https://route/for/first-project-form");
iframe.style.width = "640px";
iframe.style.height = "480px";
container.appendChild(iframe);
In project 2:
#section('content')
<div class="card rounded-0">
<div class="card-body">
<div id="form-container"></div>
</div>
</div>
<script src="http//link/to/javascript/file"></script>
#endsection

How to make dynamic #yield in laravel?

I want to make #yield('dynamic') to load different pages like single page application in laravel.
Route:
Route::get(
'/front-office-setup/{setup_type}',
'AdmissionFrontOfficeSetupController#index'
)->name('frontofficesetupview');
Controller:
public function index($setup_type)
{
$data['setup_type'] = $setup_type;
return view('frontoffice::frontofficesetup.frontofficesetup', $data);
}
View:
<div class="row">
<div class="col-md-3">asdf</div>
<div class="col-md-4">asdf</div>
<div class="col-md-5"> #yield('{{$setup_type}}')</div>
</div>
Section:
#extends('frontoffice::frontofficesetup.frontofficesetup')
#section('visitor-purpose')
sdfasd
#endsection
But it doesn't render or show in #yield('{{$setup_type}}')
Is there any way to do this?
Edit part*
Also i have already included a #yield type of things in view file
#extends('backend.master.master')
#section('content')
<div class="row">
<div class="col-md-3">asdf</div>
<div class="col-md-4">asdf</div>
<div class="col-md-5"> #yield($setup_type)</div>
</div>
#endsection
#yield is already started PHP tag <?php. SO no need to mention braces again{{}}. Simply try #yield($setup_type) It will work perfectly.

Laravel 5 other routes show 404

i have been working with laravel 4 for a long time without problem and now i am depping in the laravel 5, so i found some frustrating bugs that i dont know how to fix, here the code.
route
Route::get('/', 'HomeController#index');// works
Route::get('home', 'HomeController#home');//dont work
//Route::get('/home', 'HomeController#home');//still doesn't work
Controller
public function index()
{
return view('home');//it works
}
public function home()
{
return view('home');// it does not work :(
}
the home.blade.php
#extends('app')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="panel panel-default">
<div class="panel-heading">Home</div>
<div class="panel-body">
You are logged in!
</div>
</div>
</div>
</div>
</div>
#endsection
At the browser
http://mailing_creator.dev/ << it open the welcome page normal
http://mailing_creator.dev/home <<< it show 404 not found :(
Any idea?
I found the solution !! I just have to uncomment the
"LoadModule setenvif_module modules/mod_setenvif.so"
on apache httpd.conf, i am using wamp server and the "rewrite module" is disabled i just have to enable it and it will works

Categories