so i have a problem in my laravel application when i access to the route where i posted the json i get the 500 error so there is the source :
the route :
web.php
Route::get('loadmore', 'MoviesController#loadmore');
The controller :
public function loadmore(Request $request)
{
$movies_list = new Movies;
if ($request->has('page')) {
$movies_list = Movies::where('status', 1)->orderBy('id', 'DESC')->paginate(4);
}
$returnHTML = view('pages.ajax_movie', compact('movies_list'))->render();
return response()->json([
'html' => $returnHTML,
'page' => $movies_list->currentPage(),
'hasMorePages' => $movies_list->hasMorePages(),
])
}
and finally the view :
location : pages/ajax_movies.blade.php
#foreach($movies_list as $movies_data)
<a href="{{ URL::to('movies/'.$movies_data->video_slug.'/'.$movies_data->id) }}" class="movie">
<span class="r"><i class="i-fav rating"><i>{{$movies_data->imdb_rating}}</i></i></span>
<img src="{{URL::to('upload/source/'.$movies_data->video_image_thumb)}}" alt="" title="">
<span class="title">{{$movies_data->video_title}}</span>
<span class="ribbon ">
<span>{{$movies_data->video_access}}</span>
</span>
</a>
#endforeach
everyone can help me ?
thank you in advance
Related
I'm creating a shop, with an upload image in Laravel 9.
Everything is on my database, and my image is sent on file storage/app/public/nameofthefile/nameoftheimage.png.
But my image does not appear on my website like this :
As you can see on the console, the image has the correct direction I give you the code of my controller, my html page :
My CONTROLLER :
public function CreateArticleAction(Request $request) {
$request->validate([
'nom' => ['required'],
'description' => ['required'],
'prix' => ['required'],
'quantité' => ['required'],
'img' => ["required", "image", "mimes:jpg,jpeg,png,gif,svg,webp", "max:2048"],
]);
$path = $request->file('img')->store('boutique-storage', 'public');
$iduser = Session::get('iduser');
$article = new Boutique();
$article->iduser = $iduser;
$article->nom = request('nom');
$article->description = request('description');
$article->prix = request('prix');
$article->quantité = request('quantité');
$article->img = $path;
$article->save();
return redirect('/boutique');
}
My HTML page :
#foreach ($boutiques as $boutique)
<div class="produit">
<img src="{{ asset('storage/app/public/' . $boutique->img) }}" alt="Produit" class="img-produit" />
<p>{{ $boutique->nom }}</p>
<p>{{ $boutique->description }}</p>
<p>{{ $boutique->prix }}</p>
<p>{{ $boutique->quantité }}</p>
<div>
<a class="text-danger" href="/delete-article/{{ $boutique->idproduit }}">Supprimer</a>
<a class="text-primary" href="/FormUpdate/{{ $boutique->idproduit }}">Modifier</a>
</div>
</div>
#endforeach
And the DIRECTION :
I think you can see everything is ok, each data is sent correctly on the database but why my image does not appear ??
Thanks a lot !
Try run this command
php artisan storage:link
Ah ok so I forgot to add this on .env file : FILESYSTEM_DISK=public
Now with the command php artisan storage:link it works :D
replace
<img src="{{ asset('storage/app/public/' . $boutique->img) }}"
with
<img src="{{ asset($boutique->img) }}"
My web doesn't seem to be directing to the correct page.
Here is my blade
<div class="container">
<div class="col-lg-12 d-flex justify-content-between align-items-center">
<h2>Informations</h2>
<a href="{{ route('add-new-information') }}" class="btn text-success">
<i class="fas fa-plus-circle fa-2x"></i>
</a>
</div>
<hr>
<div class="row d-flex justify-content-center align-items-center mt-5" style="max-height: 500px !important; overflow-y: scroll">
#foreach ($informations as $info)
<div class="card col-sm-11 p-0 mb-4 clickable-item" onclick='window.location = "{{ route('admin-informations', ['id' => $info->id]) }}"'>
...
</div>
#endforeach
</div>
</div>
Here is my routes/web
Auth::routes();
Route::group(['middleware' => ['auth'], 'prefix' => '/',], function () {
Route::get('/', function () {
return redirect('/home');
});
Route::group(['prefix' => 'admin'], function() {
Route::get('/informations', [App\Http\Controllers\InformationController::class, 'index'])->name('informations');
Route::get('/informations/{id}', [App\Http\Controllers\InformationController::class, 'indexAdminInfo'])->name('admin-informations');
Route::get('/informations/add-new-information', [App\Http\Controllers\InformationController::class, 'add'])->name('add-new-information');
});
});
and here is my controller
public function indexAdminInfo($id){
$information = Information::find($id);
// $comments = Comment::where('information_id', $id)->orderByDesc('created_at')->get();
$ack_count = Acknowledge::where('information_id', $id)->count();
$user_ack = Acknowledge::where([
['information_id', '=', $id],
['user_id', '=', Auth::user()->id],
])->first();
$ack = 'FALSE';
if($user_ack != null){
$ack = 'TRUE';
}
return view('adminviews/infoadmin', compact('information', 'ack_count', 'ack', 'user_ack'));
}
public function add(){
return view('adminviews/addinfo');
}
For some reason, when I click the a tag with the href {{ route('add-new-information') }} to go to the add page 'adminviews/addinfo',
instead the page will go to the 'adminviews/infoadmin' page, which will cause an error, because no parameters are being sent.
I tried checking the code, but it looks correct to me. Can anybody find an error on this?
the problem is with your routes:
these two routes are ambiguous:
Route::get('/informations/{id}');
Route::get('/informations/add-new-information');
just think of below scenario:
router wants to route, this url : /information/add-new-information
router will hit the first defined route, because it is compatible with the definition ('/informations/{id}')
Note :{id} is a variable and can be any string
so it will go with this.
Solution
write the more restricted route first,
and more general route later:
Route::get('/informations/add-new-information');
Route::get('/informations/{id}');
In Controller
public function index()
{
$profile = Profile::where('user_id', auth()->user()->id)->first();
return view('profile', compact('profile'));
}
In Blade View
<div class="card mb-5">
<img class="card-img-top" src="{{ asset('images/'.$profile->image ? $profile->image : "" ) }}"
alt="Profile User">
<div class="card-body">
<h5 class="card-title">{{Auth::user() ? Auth::user()->name :""}}</h5>
<p class="card-text">{{Auth::user() ?Auth::user()->email :""}}.</p>
</div>
<ul class="list-group list-group-flush">
<li class="list-group-item">{{$profile ? $profile->gender :"Belum Di isi"}}</li>
<li class="list-group-item">{{$profile ? $profile->phone :"Belum Di isi"}}</li>
<li class="list-group-item">{{$profile ? $profile->address :"Belum Di isi"}}</li>
</ul>
</div>
But above code throw an error Trying to get property of non-object.
How can you fix these errors, so that the image runs again without an error
In my case! every time this error happens in my code this is because $item->image is not present in my collection instance.
The error is thrown because your $profile variable is not an object, it can be an array or null. Try dd($profile) to make sure that you are passing an object
if you are passing an object then try changing your code like this if you are passing an object
<img class="card-img-top" src="{{ asset('images/' . $profile ? $profile->image : "" )}}"/>
or like below if you are passing an array
<img class="card-img-top" src="{{ asset('images/' . isset($profile['image'] ? $profile['image'] : "" )}}"/>
Do a Auth check before use the auth.
Use firstOrFail instead of first, it will throw a 404 error if get null value :
use Illuminate\Support\Facades\Auth;
public function index()
{
if (Auth::check())
{
$profile = Profile::where('user_id', Auth::id())->firstOrFail();
return view('profile', compact('profile'));
} else {
return "You are not logged in";
}
}
My view is like this :
<li class="{{ Request::is('users*') ? 'active' : '' }}">
<a href="{!! route('users.index', 2016) !!}">
<i class="fa fa-circle-o"></i> YEAR 2016
</a>
</li>
<li>
<a href="{!! route('users.index', 2017) !!}">
<i class="fa fa-circle-o"></i> YEAR 2017
</a>
</li>
When I hover over the link to the year 2016, the url will be like this :
localhost/mysystem/public/users?2016
My routes\web.php is like this :
Route::get('users/index?{year}', 'UserController');
Route::resource('users', 'UserController');
And my controller user is like this :
public function index(Request $request)
{
$year = $request->input('year');
echo $year;die()
}
There is exist error like this :
UnexpectedValueException in Route.php line 646: Invalid route action: [App\Http\Controllers\UserController]
Is there any people who can help me?
Your route should be as:
Route::get('users/index/{year}', 'UserController#index')->name('users.index.year');
Your controller as:
public function index(Request $request, $year)
{
echo $year;die()
}
Then you can use it in your view as:
{{ route('users.index.year', ['year' => 2016]) }}
Pass parameter in route like this:
Route::get('users/index/{year}', 'UserController#index');
View:
<a href="{!! route('users.index', ['year' => 2016]) !!}">
<i class="fa fa-circle-o"></i> YEAR 2016
</a>
Controller (get as argument inside controller method):
public function index($year)
{
echo $year;
die();
}
Or - If you want to pass a parameter as GET params to routes just do this:
Route::get('users/index', 'UserController');
and inside your <a href="">:
<a href="{!! route('users.index', ['year' => 2016]) !!}">
<i class="fa fa-circle-o"></i> YEAR 2016
</a>
The statement: {!! route('users.index', ['year' => 2016]) !!} will create a route like this: http://website.com/users/index?year=2016
and grab it in controller using request() helper like this:
public function index()
{
$year = request()->get('year');
echo $year;
die();
}
Hope this helps!
You will get it from method arguement
public function index(Request $request,$para_year)
{
$year = para_year;
echo $year;die()
}
and you route should be
Route::get('users/index/{year}', 'UserController#index');
I cant't figure out what's going wrong with a link from my "welcome" page to a "profile" page.
It says "Undefined variable: restaurants (View: /Users/beyerdynamic/Documents/Developer/dev1/resources/views/welcome.blade.php)"
The strange thong is that I pass the Variable to my view.
My PagesController is:
public function welcome() {
$restaurants = User::orderByRaw('RAND()')->take(3)->get();
return view('welcome')->withRestaurants($restaurants);
}
My View is:
#foreach($restaurants as $key => $restaurant)
<div class="col-md-4">
<div class="card">
<div class="image">
<img src="{{asset('images/frontend/profile/dummy/profilehero/hero4.png')}}" alt="..." />
<div class="filter filter-white">
<a href="{{ URL::to('profile/'.$restaurant->id)}}" type="button" class="btn btn-info btn-round btn-fill">
<i class="fa fa-heart"></i> View Restaurant
</a>
</div>
</div>
</div>
</div>
#endforeach
The Restaurants Variable is passed properly to my welcome view, but when I click the link the error occurs on /profile/{id} url.
Change return view('welcome')->withRestaurants($restaurants);
to
return view('welcome')->with('restaurants', $restaurants);
OR
return view('welcome', compact('restaurants');
Try with this,
return view('welcome')->with("restaurants",$restaurants);
OR
return view('welcome',["restaurants" => $restaurants]);
Check laravel docs : https://laravel.com/docs/master/views#passing-data-to-views
Change the following line:
return view('welcome')->withRestaurants($restaurants); // there is nothing like withRestaurants in laravel
to
return view('welcome', array('restaurants' => $restaurants));
and try again.