How to access this in my view - php

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']);

Related

laravel show database values in my blade front end foreign keys values

So I'm new to laravel and I'm trying to figure out a way to connect my database with my front end!
First this is the villa model for the table villas:
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Villa extends Model
{
use HasFactory;
protected $fillable=[
"id", "title", "description", "price", "state", "status", "city_id", "seller_id", "payment_id", "created_at", "updated_at"
];
public function City()
{
return $this -> hasOne(City::class,'id','city_id');
}
public function Seller()
{
return $this -> hasOne(Seller::class,'id','seller_id');
}
public function Payment()
{
return $this -> hasOne(Payment::class,'id','payment_id');
}
}
this is the website controller : and here I'm trying to call the table
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\Villa;
use App\Models\City;
use App\Models\VillaImg;
class WebsiteController extends Controller
{
public function index(){
$villas = DB::select('select * from villas');
return view('website',['villas'=>$villas]);
}
this is the website blade which I use this way to call the value but I don't know how to call a foreign key value , for example :
I have the foreign key city_id and I want the show the city name on my front end but don't know how
#foreach ($villas as $user)
{{old('name',$user->title )}}
#endforeach
#foreach ($villas as $user)
{{old('city',$user->I don't know what to write here)}}
#endforeach
Note: this is a code in a div that represents a villa block.
Also, I'm not sure if this is really the correct way to do so and I want to know if there's another efficient way to do that!
here's the route:
Route::get('/',[WebsiteController::class,'index'])->name('home.index');
use eloquent orm instead of db facade...
$villas = Villa::with(['City', 'Seller', 'Payment'])->get();
and then if u need any relation value:
#foreach ($villas as $villa)
{{ $villa->City->property }}
#endforeach

Specify DB table in laravel query

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.

Laravel get object attribute if object id is known

How do I get the value from a object if the ID is known?
So, I got the house id, in the house model there is an user_id for every house like:
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;
use stdClass;
use App\Service\ChangesetService;
class HouseProfile extends Model
{
protected $guarded = ['id', 'user_id'];
public function user()
{
return $this->belongsTo(User::class);
}
}
Every user got an address (string)
I need the address and I got the house id and I'm working in a blade file in Laravel 5.5
If I understand correctly:
#if($house->user)
{{ $house->user->address }}
#endif
You should never run queries of put too much logic in you Blade files. Instead you should pass variables to them.
In your controller
$house = HouseProfile::findOrFail($houseId);
return view('your.view.name', [
'house' => $house
]);
In your blade file you can use #IndianCoding response, so
#if($house->user)
{{ $house->user->address }}
#endif

Issue querying laravel eloquent relationship

This is with reference to this question :
Laravel Eloquent One to Many relationship
I tried the suggested way, but couldn't resolve. Please help. Below is the changes i have done :
Earlier :
//Route for Restaurants Page
Route::get('/home/restaurants',function(){
$restaurants = DB::table('restaurants')->simplepaginate(3);
return view('restaurants',['restaurants_data'=>$restaurants]);
});
Changed as per suggestion :
Route::get('/home/restaurants',function(){
// $restaurants = DB::table('restaurants')->simplepaginate(3);
$restaurants = \App\Restaurant::simplePaginate(3);
return view('restaurants',['restaurants_data'=>$restaurants]);
});
In Restaurant model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Restaurant extends Model
{
public function offer(){
return $this->hasMany('Offer');
}
}
In view, now I am trying to access it by dumping the values.
<?php
var_dump($restaurants_data->offer);
?>
Error :
After doing dd()
Firstly, I would suggest changing your Offer Relationship to:
public function offers()
{
return $this->hasMany(Offer::class, 'restaurant_ID', 'id');
}
The above assumes that the Offer class and Restaurant class are in the same namespace. If they're not please add the correct namespace or import the Offer model in to the class.
Secondly, because you're paginating the results you will end up with a collection of Restaurant models (even if there is only one), so you will need to loop through them to get access to the offers for each model. I would also suggest eager loading the results e.g.
Route:
Route::get('/home/restaurants', function () {
$restaurants = \App\Restaurant::with('offers')->simplePaginate(3);
return view('restaurants', compact('restaurants'));
});
in your view:
#foreach($restaurants as $restaurant)
#foreach($restaurant->offers as $offer)
{!! dump($offer) !!}
#endforeach
#endforeach
{{ $restaurants->links() }}
Can you replace
$restaurants = \App\Restaurant::paginate(3); and amend the blade code to say
<?php
foreach($restraunts_data as $resturant) {
if(count($restaurant->offer) {
print_r($restaurant->offer);
}
}
?>
You are using the models incorrectly. You run no queries and you attempt to run a static method on the Restaurant class without selecting any restaurants. As far as I know is this not supported by Eloquent. If you look at the error message it complains that there are no property $offer.
Try to run some query, and the select the related Offer. This should work as expected.
For example:
$offers = \App\Restaurant::find(1)->offer;
This will return the many Offer relations for the Restaurant with ID 1.

Laravel relationship returning null

I'm trying to fetch database data through a model relationship in Laravel.
I've set up one model, like this:
<?php
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class Userskeywords extends Eloquent {
public function relatedKeywords()
{
return $this->hasOne('Keywords', 'id', 'keywordId');
}
}
?>
And the other model is just a normal model. In the database they look like this:
Keywords
UsersKeywords
However, when I run UsersKeywords::with('relatedKeywords')->get() it returns NULLfor related_keywords. This happens when the following code is executed. What am I doing wrong?
$keywords = Userskeywords::where('user', '=', $id)->get();
$keywords->load('relatedKeywords');
return Response::json($keywords);
Your relation is called relatedKeywords so you need to access related object with
$object->relatedKeywords
instead of
$object->related_keywords

Categories