I'm working on a photography website at the moment and am having trouble with the page that should show all the albums and pictures within the album. I always get an error
Class 'Photo' not found (View: /var/www/photography.website/resources/views/album/index.blade.php)
when trying to access the page.
Following are the view, controllers, and models;
Index View:
#extends('layout.main')
#section('title')
<title>Albums</title>
#endsection
#section('content')
<div class="content">
#if(count($albums)==0)
<p>Nothing here yet, come back later!</p>
#else
#foreach($albums as $album)
<div class="album">
<h3>{{$album->name}}</h3>
#foreach($album->photos()->getEager() as $photo)
<img src="/public/thumb-{{$photo->id->toString()}}.jpeg" class="">
#endforeach
</div>
</div>
#endforeach
#endif
#endsection
Album Controller (snippet):
<?php
namespace App\Http\Controllers;
use App\Photo;
use App\Album;
use Illuminate\Http\Request;
class AlbumController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$albums = Album::all();
return view('album.index')->with(compact($albums));
}
}
Album Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Album extends Model
{
protected $fillable = ['name','cover'];
public function photos() {
return $this->hasMany('Photo');
}
}
Photo Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Photo extends Model
{
public $incrementing = false;
protected $fillable = ['id','album_id','title'];
}
You are using the Unqualified Name of the class in your relationship. You would need to use the full name of the class when referring to the class via string like that.
There is no class named Photo in the application but there is a class named App\Photo.
public function photos() {
return $this->hasMany('App\Photo');
// but probably almost always better to use the class constant
return $this->hasMany(Photo::class);
// 'App\Photo'
}
Photo here refers to a class in the current namespace App, as there is no alias for anything named Photo.
Related
Employees.php file
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/**
* #method static find($id)
*/
class Employees extends Model
{
use HasFactory;
protected $table = 'employees';
public $timestamps = false;
}
EmployeesController.php
<?php
namespace App\Http\Controllers;
use App\Models\Employees;
class EmployeesController extends Controller
{
public function employees_salaries()
{
return view('director.employees_salaries');
}
public function employees()
{
$employees = Employees::all();
return view('director.employees', ['employees'=>$employees]);
}
public function destroy($id)
{
$employees = Employees::find($id);
$employees->delete();
return redirect('/director.employees')->with('status', 'Your Data is Deleted');
}
}
employees.blade.php file
<from action="/delete/{{$employee->id}}" method="POST">
#csrf
#method('DELETE')
<button type="submit" class="btn btn-delete btn-form me-3">
Delete
</button>
</from>
route.php file
Route::match(['get', 'post'], '/employees', array(EmployeesController::class, 'employees'))->name('employees');
Route::delete('/delete/{id}', array(EmployeesController::class, 'destroy'))->name('delete');
I cleared the cache but have no idea what the problem is. it looks like I wrote the function correctly
p.s version Laravel 9
mySQL 8
phpMyAdmin
Welcome to SO, i think youre not using the variable you assigned the value into,from controller in your blade view. maybe try to make sure you have the right variable or maybe try to use var dump.
try to put this in your controller b4 parsing value to view, to check whether you get the data you wanted or not
dd('$employees');
make sure you use the variable you assigned in your controller to your view
<?php
namespace App\Http\Controllers;
use App\Models\Employees;
class EmployeesController extends Controller
{
public function employees_salaries()
{
return view('director.employees_salaries');
}
public function employees()
{
$employees = Employees::all();
return view('director.employees', ['employees'=>$employees]); //<< here you name the variable 'employees'
}
public function destroy($id)
{
$employees = Employees::find($id);
$employees->delete();
return redirect('/director.employees')->with('status', 'Your Data is Deleted');
}
}
change this variable in view
<from action="/delete/{{$employee->id}}" method="POST"> //<< this one you use '$employee' instead of '$employees'
#csrf
#method('DELETE')
<button type="submit" class="btn btn-delete btn-form me-3">
Delete
</button>
</from>
there might also be other problem, but for now thats what i can point out.
since im also learning.
im new to laravel and i want to know how to make a dynamic sidebar.
Right now it looks like this
In my native language it means category - berrys, juice, candy - and each of them goes to a view, where there should be only products who have in the database one of these three in the "category" column.
My question are:
1)Can i do it with a foreach loop?
2)Do i need to make a model and controller for each of them and if i do what functions do i need?
3)How do i show only the produkcts that have the right category
Right now i have:
#foreach($products as $item)
<div class="col-sm-6 col-md-4">
<a href="details/{{$item['id']}}">
<div class="thumbnail">
<img src="images/{{$item['galerija']}}" alt="...">
<div class="caption">
<h3>{{$item['nosaukums']}}</h3>
<p>{{$item['cena']}}</p>
</div>
</div>
</a>
</div>
#endforeach
That shows every product. Do i need something similar?
More details.
In my controller:
function shop()
{
$data= Product::all();
return view('pages.shop',['products'=>$data]);
}
My moddel:
class Product extends Model{
protected $primaryKey = 'id';
public $timestamps = false;
protected $fillable = [
'nosaukums', 'cena', 'kategorija', 'galerija', 'apraksts',
];
}
Yes.
Since it's the same model, you only need one controller with one method (if you follow conventions this would be a show method).
Using a relation.
Due to the fact you're coding in your native language (which I cannot decipher), below example is a way of how you could do it.
P.S. In the case that a product can have more than one categories, you should convert the relation into a Many to Many using a pivot table.
Category model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
public function products(): HasMany
{
return $this->hasMany(Product::class);
}
}
Product model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
public function category(): BelongsTo
{
return $this->belongsTo(Category::class);
}
}
Web.php
Route::get('categories/{category}', [CategoryController::class, 'show'])->name('categories.show');
Sidebar view
Note: You can load $categories using a View Composer.
<div>
#foreach($categories as $category)
<a href="{{ route('categories.show', compact('category')) }}">
{{ $category->name }}
</a>
#endforeach
</div>
Category controller
<?php
namespace App\Http\Controllers;
use App\Models\Category;
class CategoryController extends Controller
{
public function show(Category $category)
{
$products = $category->products;
return view('categories.show', compact('products');
}
}
Can Anybody help me with Laravel Relationship ?
i have data base structure
poliklinik Table
===========
id
nama_poliklinik
****************
jadwal_poliklinik Table
=======================
id
poliklinik_id
hari
***********************
the relationship is "One" polikinik "have many" jadwal_poliklinik
my model poliklinik is
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\jadwal_poliklinik;
class poliklinik extends Model
{
protected $table = 'poliklinik';
public function jadwal_poliklinik()
{
return $this->hasMany(jadwal_poliklinik::class);
}
}
my jadwal_poliklinik model
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\poliklinik;
class jadwal_poliklinik extends Model
{
protected $table = 'jadwal_poliklinik';
public function poliklinik()
{
return $this->belongsTo(poliklinik::class);
}
}
my controller is
public function jadwal()
{
$poliklinik = poliklinik::all();
return view('jadwal', compact('poliklinik'));
}
and the view
<div class="row">
#foreach($poliklinik as $p)
<div class="col-md-4 animate-box">
<div class="blog-entry">
<div class="desc">
<h3>{{$p->nama_poliklinik}}</h3>
<hr/>
#foreach($poliklinik->jadwal_poliklinik as $jp)
{{$jp->hari}}<br/>
#endforeach
</div>
</div>
</div>
#endforeach
</div>
but result is
Exception Property [jadwal_poliklinik] does not exist on this
collection instance.
can someone help me with this??
It must be $p->jadwal_poliklinik
#foreach($p->jadwal_poliklinik as $jp)
{{$jp->hari}}<br/>
#endforeach
But also, Its better if you can use CamelCase for class names. That is the standard. You can simply use artisan command to create classes. See the bellow link for more details.
https://laravel.com/docs/7.x/eloquent#defining-models
I have a controller named HomeController and I want to call data from banner table. I have a banner table and a BannerController. I had tried to get data in HomeController like
public function index()
{
$data = banner::all();
echo "hello";
print_r($data);exit;
return view('home');
}
but it's not showing me anything.
My banner model is like
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class banner extends Model{
protected $table='banners';
}
?>
And my route is like
Route::get('\home','HomeController#index')->name('home')
Your model, assuming your table name is 'banners':
App\Banner.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Banner extends Model
{
protected $table = 'banners';
}
From HomeController, sending $data to your view can be done like so:
App\Http\Controllers\HomeController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Banner;
class HomeController extends Controller
{
public function index()
{
$data = Banner::all();
return view('home', compact('data'));
}
}
And you should have the results of Banner:all() in your home blade/view which will be accessible as $data
In your blade file you can access it like so (just an example):
#foreach ($data as $banner)
{{ $banner }}
#endforeach
Try This Method - call multiple table data in a single page
public function index()
{
return View('index')
->with('test1', table1::all())
->with('test2', table2::all())
->with('test3', table3::all());
}
I have been trying to list a dropdown in the index page with data from database. I created a model and made some changes in controller to display it in my view page but making any change in the controller gives a blank page with 500 Internal server error in the console. Please help me out to sort this problem.
Table name: walker_type
Routes:
Route::get('/', 'WebController#index');
Model: ProviderType.php :
<?php
class ProviderType extends Eloquent {
protected $table = 'walker_type';
}
Controller: WebController.php
public function index() {
$walkerTypeList = ProviderType::all();
return view('website.index')->with(['walkerTypeList' => $walkerTypeList]);
}
View:index.php
#foreach ($walkerTypeList as $car)
<option data-icon="glyphicon-road" value="{{ $car->name }}"> {{ $car->name }} </option>
#endforeach
Had u declare your model below your namespace?
eg. use App\WalkerType;
also you forgot to declare a namespace to your Model.
it should have namespace App;
or if you have a folder for your model to make it more conventional.
you should have a namespace on each of your model.
eg. App\Model
and then use that in every controllers by declarin in between your namespace and class
eg.
namespace App\Controllers;
use App\Model\WalkerType;
class SomeController extends Controller{
protected $data; //this is a class variable that can call anywhere to your class by calling it this way $this->data
public function some_method(){
$this->data['variable_a'] = "some_value"; //this can call in you view later by $variable_a
$this->data['sum'] = 1+4; //result can be display in your view by calling the variable $sum
return view('someview',$this->data);
}
}
I hope this can help you for your project efficiently, cause we had experienced that we forgot to include some of the data that has been processed on the controller and needed to display in your view file but forgot to include.