Creating and accessing a new page from blade - php

I am trying to create a new blade.php page, I need to access this page by pressing a button on my home page, but it isn't working properly.
How can I do it?
I tried doing
Route::get('/my-page', function () { return view('my-page'); });
inside web.php, it works, but when I press the card button, it doesn't work
`
<div class="col-lg-4 col-md-4 col-sm-4 my-1">
<a href="{{ route('course-page') }}">
<div class="card card-animated text-center py-5">
<span class="">
<img class="img-fluid" src="assets/image/courses.svg" >
</span>
<div class="card-body">
<p class="card-text">COURSES</p>
</div>
</div>
</a>
</div>

You are trying to get a route by route name while you did not define the route name. Add a name for your route:
Route::get('/my-page', function () { return view('my-page'); })->name('course-page');

Try this,
Change in web.php
Route::get('/my-page', function () { return view('my-page'); })->name('course-page');
Welcome in advance.

Related

Undefined variable $payments laravel 9.x

I am trying to print out the lists of payment methods using the foreach loop, however, i get an undefined variable.. error.
Here is my controller class
class HomeController extends Controller
{
//
public function index()
{
$payments = Payment::where('status', 1)->get();
return view('user.account.index', [
'payments' => $payments
]);
}
}
and here is my section in my view page(index) i want to loop through
#foreach ($payments as $payment)
<!-- {{ Cryptocap::getSingleAsset($payment->name) }} -->
<div class="card col-md-6 no-padding ">
<div class="card-body">
<div class="h1 text-muted text-right mb-4">
<i class="pe-7s-wallet"></i>
</div>
<div class="h4 mb-0">
<span class="">0.0012930403</span>
</div>
<small class="text-muted text-uppercase font-weight-bold">BTC</small>
<div class="progress progress-xs mt-3 mb-0 bg-flat-color-1" style="width: 40%; height: 5px;"></div>
</div>
</div>
#endforeach
The above approach works on other areas of my application, as this is the approach i have been using throughout the application. But it doesn't seem to work on this particular controller and view.
Can you give this a try
return View::make("user/regprofile", compact('students')); OR
return View::make("user/regprofile")->with(array('students'=>$students));
For more info visit this link
Passing data from controller to view in Laravel

How to do pagination in laravel 6 ? by exception in like above { Call to undefined method App\Clothes::links()}

When i try to paginate i got an exception Call to undefined method App\Clothes::links() (View: D:\work\Laravel\winter\resources\views\pages\home.blade.php).my data is view in the blade file but i am not been able to do pagination because of the above exception.please help
public function images(){
if(auth::check()){
$user = Auth::user();
$renimage = Clothes::orderBy('id','desc')->paginate(3);
$cart = cart::where('user_id',$user->id)->get();
$cart_added=count($cart);
return view('pages.home',['renimage'=>$renimage,'google_avater'=>$user,
'cart_added'=>$cart_added]);
}
}
In the blade file.
<div class="container" style="margin-top:50px;margin-bottom:50px">
<div>
#foreach ($renimage as $renimage)
<div class="col-lg-3 col-md-4 col-sm-6 col-xs-12">
<div class="hovereffect">
<img class="img-responsive" src="{{asset('upload/'.$renimage->img) }}" alt="">
</br>
<div class="overlay">
<h2>{{$renimage->name}}</h2>
<h3 style="color: white;padding-top: 70px;">{{'$'.$renimage->price}}</h2>
<a class="info" href="{{ route('product',['id'=>$renimage->id])}}" style="margin-top:
20px;">Buy Now</a>
</div>
</div>
</div>
#endforeach
{{$renimage->links()}}
you need to handle empty collections
#unless (count($renimage))
{{$renimage->links()}}
#endunless

Laravel 5.8 Passing parameter to view returned undefined

I want to pass the username to the url in the browser as in "/profile/{username that's logged in here}". But to test things out i tried to pass id first.
What's wrong with this is not when i press the actual link. The problem occurs when i tried to access just the welcome page.
I've called the route in the view and passed the correct param 'user' to it but it's still giving me an undefined variable error.
here's the route
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/profile/{user}', 'ProfilesController#index')->name('profile.show');
The ProfilesController:
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
class ProfilesController extends Controller
{
public function index($user)
{
$user = User::find($user);
return view('home', [
'user' => $user,
]);
}
}
The welcome blade:
<div class="flex-center position-ref full-height">
#if (Route::has('login'))
<div class="top-right links">
#auth
Home
#else
Login
#if (Route::has('register'))
Register
#endif
#endauth
</div>
#endif
<div class="content">
<div class="title m-b-md">
gramClone
</div>
<p class="content">
where IG pics get to be shittier.
</p>
</div>
</div>
the home blade it calls after pressing home link:
#section('content')
<div class="container">
<div class="row">
<div class="col-4 p-5">
<img src="/png/logo.png" alt="logo" style="height: 6rem; width: 6rem;" class="float-right rounded-circle">
</div>
<div class="col-8 pt-5">
<div><h1>{{$user->username}}</h1></div>
<div class="d-flex">
<div class="pr-5"><strong>number here</strong> posts</div>
<div class="pr-5"><strong>number here</strong> followers</div>
<div class="pr-5"><strong>number here</strong> following</div>
</div>
<div class="pt-4 font-weight-bold">Bio Summary Here</div>
<div>Bio Here</div>
<div>Link Here</div>
</div>
</div>
<div class="row pt-5">
<div class="col-4"><img src="https://images.unsplash.com/photo-1566624790190-511a09f6ddbd?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=634&q=80" alt="" class="w-100"></div>
<div class="col-4"><img src="https://images.unsplash.com/photo-1544127715-bafd09df7c52?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=634&q=80" alt="" class="w-100"></div>
<div class="col-4"><img src="https://images.unsplash.com/photo-1566592952746-15ea1f1a4133?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=653&q=80" alt="" class="w-100"></div>
</div>
</div>
#endsection
Some of my friends said it's cause i have to put the param at get'/' as well but when i tried it, it gave me the previous error of unpassed param again.
Please help me with this, i've been on overflow for 3 hours already and still got no actual answer to this.
Found out the problem after commenting out my route calling in the welcome blade, apparently it's the #auth making things freaky. After some quick googling i found out that apparently you can't just say route('profile.show', $user) if you have an #auth there.
You need to call it like this: auth()->user().

how to delete images from folder and from table also, if clicks on confirm on javascript box in laravel

I am making CRUD operation in laravel 5.4. I am deleteing an image from database and storage. I did it, but it deletes image name from database only. And do not delete from storage folder.
And I am doing one more thing, If I click on delete button then one confirmation box should be display. If click yes, then delete otherwise not delete. BUt it is also not working.
My web.php is:
Route::resource('todo','todocontroller');
My todocontroller.php is:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\todo;
class todocontroller extends Controller
{
public function index()
{
$todos = todo::paginate('3');
return view('todo.home',compact('todos'));
}
public function destroy($id)
{
$todos = todo::find($id);
if(\File::exists(public_path('public/storage/images/'.$todos->image))){
\File::delete(public_path('public/storage/images/'.$todos->image));
}
$todos->delete();
session()->flash('message','Deleted Successful');
return redirect('todo');
}
}
My home.blade.php is:
#extends('layout.app')
#section('body')
<br>
#if(session()->has('message'))
{{session()->get('message')}}
#endif
<div class="col-lg-4 col-lg-offset-4">Add new
<h1>Todo Lists</h1>
<ul class="list-group">
#foreach($todos as $todo)
<li class="list-group-item d-flex justify-content-between align-items-center">
<span><img src="{{asset('public/storage/images/'.$todo->image)}}" style="height: 100px;width: 100px;"></span>
{{$todo->title}}
{{$todo->body}}
<span>
<form class="delete" action="{{'todo/'.$todo->id}}" method="POST">
{{csrf_field()}}
{{method_field('DELETE')}}
<button type="submit">Delete</button>
</form>
</span>
</li>
#endforeach
{{$todos->links()}}
</ul>
</div>
<script>
$(".delete").on("submit", function(){
return confirm("Do you want to delete this item?");
});
</script>
#endsection
It seems you given wrong path to the images directory. that's why delete image is not working.
public function destroy($id)
{
$todos = todo::find($id);
if(\File::exists(public_path('storage/images/'.$todos->image))){
\File::delete(public_path('storage/images/'.$todos->image));
}
$todos->delete();
session()->flash('message','Deleted Successful');
return redirect('todo');
}
I think this will work.
Edited home.blade.php file
#extends('layout.app')
#section('body')
<br>
#if(session()->has('message'))
{{session()->get('message')}}
#endif
<div class="col-lg-4 col-lg-offset-4">Add new
<h1>Todo Lists</h1>
<ul class="list-group">
#foreach($todos as $todo)
<li class="list-group-item d-flex justify-content-between align-items-center">
<span>
<img src="{{asset('public/storage/images/'.$todo->image)}}" style="height: 100px;width: 100px;">
</span>
{{$todo->title}}
{{$todo->body}}
<span>
<form class="delete" action="{{'todo/'.$todo->id}}" method="POST" onsubmit="return confirm('Do you really want to submit the form?');">
{{csrf_field()}}
{{method_field('DELETE')}}
<button type="submit">Delete</button>
</form>
</span>
</li>
#endforeach
{{$todos->links()}}
</ul>
</div>
#endsection
you can use unlink function to delete file.
public function destroy($id)
{
$todos = todo::find($id);
if(\File::exists(public_path('storage/images/'.$todos->image))){
unlink(public_path('storage/images/'.$todos->image));
}
$todos->delete();
session()->flash('message','Deleted Successful');
return redirect('todo');
}
hope this will help.

Laravel pagination does not create relative path

I am using laravel pagination but url of pagination is not relative and it appends the new url to previous url for example if my current url is "http://localhost:8080/event-home/1" then when I click on 2nd page it generates link as "http://localhost:8080/event-home/1#http://localhost:8080/event-home/1?page=2"
As you can see it appends to the previous url and therefore it does not return the next page. Actually the URL should be like "http://localhost:8080/event-home/1?page=2" Following is the code for the same
if (Session::get('event_id') != null){
$members = Event::find(Session::get('event_id'))->users()->orderBy('name','asc')->paginate(5);
if ($request->ajax()) {
if($request->page) {
return view('event.members', ['members' => $members]);
}
else
return view('event.event-home', ['members' => $members]);
}
Here is my view code:
#foreach($members as $member)
<div class="col-lg-3 col-md-4 col-xs-6 member" id="memberId_{{$member->id}}">
<div class="caption">
<a href="/profile/{{$member->id}}" class="d-block mb-4 h-100">
<img class="img-fluid img-thumbnail" src="{{url('images/users/'.$member->id.'/profile_image/'.$member->avatar_url)}}" alt="">
</a>
<center> {{$member->name }} {{$member->profiles->last_name}}</center>
</div>
</div>
#endforeach
{{ $members->links() }}
Thanks!

Categories