I have given routing as mech/class-dept.php in web.php :
Route::get('mech/class-dept.php',function(){
return view('mech/class-dept');})->name('class');
Route::get("mech/class-dept",[App\Http\Controllers\mechplace::class,'rating1']);
Route::get('mech/faculty', 'App\Http\Controllers\mechplace#rating1');
URL will be localhost/mech/class-dept.php
But I am getting error on /mech/class page in my website Undefined variable $users
How to display the MysSQL database content to sub folders in under views folders ?
It's with pages under views folder but not working for sub folders in views in Laravel 9
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\Placements;
class mechplace extends Controller
{
public function rating1()
{
$users = "ram";
return view('mech.class-dept')->with('users', $users);
}
}
views/mech/class-dept.blade.php
{{$users}}
web.php
Related
Im new to laravel, i am trying to query a specific table in my DB. I only have 1 data table and the standard user auth tables. I am getting a error: BadMethodCallException
Call to undefined method App\Figures::table().
Model
namespace App;
use Illuminate\Database\Eloquent\Model;
class Figures extends Model
{
}
controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Figures;
class figuresController extends Controller
public function figurespag2() {
$dummyDetails = Figures::table('figures')->where('name', 'batman');
return view ( 'pagination2.index' )->withUsers($dummyDetails);
}
route
Route::get ( '/pagination2', 'figuresController#figurespag2' );
I know it's going to be something obvious, but I am new to this.
this is wrong
$dummyDetails = Figures::table('figures')->where('name', 'batman');
Method 1---------- laravel eloquent
Model
namespace App;
use Illuminate\Database\Eloquent\Model;
class Figures extends Model
{
protected $table = 'figures';
}
Controller
$dummyDetails = Figures::where('name', 'batman')->get();
and
Method 2 ---------- laravel Query Builder
$dummyDetails = \DB::table('figures')->where('name', 'batman')->get();
Use this you not need to define table name
public function figurespag2() {
$dummyDetails = Figures::where('name', 'batman')->get();
return view ( 'pagination2.index' )->withUsers($dummyDetails);
}
First you may need to know laravel model rules.
If you create a table name like "figures" (plural) you need to create its model by Figure (singular).
if you create a table other then this rule then you have to mentioned table name in model like this.
protected $table = "table_name";
you can access table with where condition in controller like this.
public function figurespag2() {
$dummyDetails = Figure::where('name', 'batman')->get();
return view ( 'pagination2.index' )->withUsers($dummyDetails);
}
Hope this may help you.
I'm building my first laravel app.
I'm trying to display all posts in x category.
My routes:
Route::get('/', 'PostsController#index')->name('home');
Route::get('/{id}/{slug?}', 'PostsController#show')->name('show');
Route::get('/categories/{category}', 'CategoriesController#index')->name('category');
My category model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
public function posts()
{
return $this->hasMany(Post::class);
}
public function getRouteKeyName()
{
return 'slug';
}
}
My category controller:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Category;
class CategoriesController extends Controller
{
public function index(Category $category)
{
$posts = $category->posts;
return view('index', compact('posts'));
}
}
Getting: "Sorry, the page you are looking for could not be found."
Works if I change:
Route::get('/{id}/{slug?}', 'PostsController#show')->name('show');
To:
Route::get('/{post}', 'PostsController#show')->name('show');
Thank you in advance!
Your route /{id}/{slug?} regex is capturing the word categories of your other route and preventing it to work as expected. You have 2 options here:
Move the categories/{category} up to the post route. Laravel routes stops on first match.
Define what {id} can be
Route::get('/{id}/{slug?}', 'PostsController#show')->name('show')->where('id', '[0-9]*');
Anyway, if that post id route is a backend route, I shall recommend to you to prefix it like posts/{id}/{slug?}. If it's a frontend, choose one of the previous solutions (or even both).
This sounds like it's the route that's configured incorrectly, sometimes the order that you define the routes can cause issues.
try to define the routes in theis order instead to see if it makes a difference:
Route::get('/{id}/{slug?}', 'PostsController#show')->name('show');
Route::get('/', 'PostsController#index')->name('home');
Route::get('/categories/{category}', 'CategoriesController#index')->name('category');
İf you have many to many or one to many relationships between categories and posts, you can do that.
Route::get('categories/{category_slug?}',['as'=>'category.show','uses'=>'CategoriesController#Show'})
and in your controller;
public function Show($category_slug){
$category = Category::where('category_slug',$category_slug)->first();
return view('index',compact('category'));
}
and in your blade;
#foreach($category->posts as $post)
$post->title
$post->content
#endforeach
I am new with this laravel , I am using resource.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Cars;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class BookController extends Controller
{
public function show($id)
{
$car = Cars::where('car_id',$id);
return view('book.view',compact('car'));
}
}
Here is my route.php
Route::resource('book','BookController');
Here is my view.blade.php
{{ $car->car_id }}
but it gives me error
Undefined property: Illuminate\Database\Eloquent\Builder::$car_id
How can I display the car_id ?
Thank you in advance.
If you are looking to fetch single record then do
$car = Cars::where('car_id', $id)->first();
return view('view.name', compact('car'))
and then access it in view like this :
{{$car->car_id}}
if you want to fetch many records do like this :
$cars = Cars::where('something', '=','otherthing')->get();
return view('view.name', compact('cars'));
and access it in view like this:
#foreach($cars as $car)
{{$car->car_id}}
#endforeach
You should follow naming conventions Never make your model name plural
try this
$car = Cars::where('car_id','=',$id)->get();
You can also get specific columns of all data in cars table using this:
$car=Cars::where('car_id',$id)->get(['car_id','car_name']);
I have two models (and two tables in the database) in Laravel 5: Artwork and Option (table names are artworks and options). A single artwork can have many options, so my models are as follows:
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Artwork extends Model {
public function options(){
return $this->hasMany('App\Option');
}
}
and
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Option extends Model {
//
}
I'm trying to output an array that will contain ALL the artworks that will have their options nested inside them. Outputting only artworks works fine ($artworks = $artwork->get() as shown below), but I cannot figure out how to merge / nest options within each artwork.
Here is my Route for this
get('api/artworks','ArtworksController#index');
And here is my controller
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Artwork;
class ArtworksController extends Controller {
public function index(Artwork $artwork)
{
$artworks = $artwork->merge($artwork->options())->get();
return $artworks;
}
}
I was playing around with that merge() function and I just couldn't get it right. Is it possible to achieve this with merge() at all?
You just need to eager load the options relationship:
$artworks = $artwork->with('options')->get();
Is this right way to do:
I create a model, controller, view for localhost/users and do the same for localhost/hello-world. Now that I have two views (template designs) for controllers how can I use them in third controller like localhost/home (DashboardController in the code)?
namespace app\controllers;
use app\models\Users;
use app\controllers\HelloWorldController;
class DashboardController extends \lithium\action\Controller {
public function index() {
$users = Users::find('first');
$hello = HelloWorldController::to_string();
return compact('users', 'hello');
}
}
Do I have to style again $users and $hello in DashboardController view and in other new controllers where I want to use multiple models, or I can use their own views which I made at the beginning? This question is really bothering me, becouse I'm new in MVC and frameworks.
If you only require sections you could use Lithium elements.
echo $this->view()->render(array('element' => 'name of element'), array('datavar' => $passingDataIn))
If you need to just render the same view again you could essentially tell the Controller method to use the view:
return $this->render(array('template' => 'dashboard/index.html.php')));