Getting Undefined property: projekti when trying to print field fetched from databse - laravel

i get this error Undefined property: projekti
when trying to print a field fetched from database
this is the code that causes the error
#foreach($dataOfProjects as $dp)
<div class="col-md-4 mt-2">
<h1 class="p-5 border border-3" >{{$dp->projekti}}</h1>
</div>
#endforeach
specifically the `{{$dp->projekti}}
this is the controller that is returning the dataOfProjects variable
public function specificClub(Request $request,$id){
$data = Clubs::find($id);
$dataOfProjects = DB::table('projektet') -> where('klubi', '=',$id);
$teachers = DB::table('mesuesit')->join('klubet','klubet.id','=','mesuesit.klubi_ne_kujdesari')->select('mesuesit.emri')->where('klubet.emri','=','rrobotike')->get();//Per te mare emrin e mesuesve te ketij klubi
return view('specificClub', compact('data','dataOfProjects','teachers'));
}
This is the table klubet
and this is the table projektet

Add ->get() for multiple results or ->first() for one result to this line:
$dataOfProjects = DB::table('projektet') -> where('klubi', '=',$id);
From:
$dataOfProjects = DB::table('projektet') -> where('klubi', '=',$id);
To (for multiple results):
$dataOfProjects = DB::table('projektet')->where('klubi', '=',$id)->get();
To (for one results):
$dataOfProjects = DB::table('projektet')->where('klubi', '=',$id)->first();

Related

How to get Unique value in blade file laravel

I want to fetch unique value for filters of all products.
My db structure as follows
id Product category_id format attribute
1 demo1 5 HD test1
2 demmo2 4 SD test3
3 demmo3 4 HD test2
4 demmo4 3 HD test3
I want add filters of format and attribute in product page. But in that HD comming 3 times. I want to display that one only.
I am not getting how to display only single time.
Below is my controller code:
$item = Item::where('active_status', 1)->where('status', "1");
$data['item_count'] = $item;
$data['item'] = $item->paginate(20);
return view('frontend.pages.explore', compact('data'));
Below is blade file
<div class="filter-btn">
#foreach($data['item'] as $resolution)
<a class="btn btn-white-outline display-4" href="">{{array_unique($resolution->format)}}</a>
#endforeach
</div>
I am not getting how to display unique value only. Anyone have idea then let me know
since you are paginating your data, your "first page" might not have all the formats, so you have to do another query to your database:
$formats = DB::table('items')->select('format')->distinct()->get()
...
view(..., compact('data', 'formats'))
in the blade table:
#foreach($formats as $resolution)
<a class="btn btn-white-outline display-4" href="">{{$resolution->format}}</a>
#endforeach
If I am correct about your query, then you need to use groupby to list the items in your controller.
$items = Item::groupBy('format')->get();
The solution would be to create a sperate model for Formatand relationship between it and Product or whatever model that needs format , then fetch formats from its table and apply eager load .
this may look longer than your solution , but this is standards shoiuld be taken for vrious reasons :
less sql queries
more flexibility and options regarding our new table
better and less code to crud format
single source for change
less database data and faster load
...
Product.php
public function format(){
return $this->belongsTo(Format::class);
}
Format.php
public function products(){
return $this->hasMany(Product::class);
}
Usage
in controller
// get all formts eager loaded with products
$formats = Format::with('products')->get();
//get all products
$product = Format::latest()->paginate(20);
return view('frontend.pages.explore', compact( 'products' ,'formats'));
in your view
// all prodcuts
#foreach($formats as $format)
<a class="btn btn-white-outline display-4" href="">{{$format->name}}</a>
#endforeach
// in filter ( at clicking ) no additional query
#foreach($format->products as $product)
...
#endforeach

Laravel collection querying

I have written a query to grab a collection of results, i've added a check to say if a record contains this field hide the record with the id of 2.
Controller method
$purchasedProducts = $user->products()->where('purchased', 1);
if ($user->products()->where('includes_bonus', 1)->first()) {
$purchasedProducts->where('benefits.id', '!=', 2);
}
$purchasedProducts->get();
Blade
in here i wrote out the foreach loop to be displayed within the blade.
#foreach($purchasedProducts as $product)
<div class="col-xl-6 p-0 p-xl-4 mb-5 mb-xl-0">
<form action="{{route('cancel.product', $product->id)}}" method="POST">
#csrf
error received
Trying to get property 'id' of non-object
<form action="<?php echo e(route('cancel.product', $product->id)); ?>" method="POST">
Can you see where i am going wrong?
You haven't assigned the results of the query to anything, you are just calling get() on the query you are building but are not doing anything with the returned results. Perhaps assign it to a variable:
$purchasedProducts = $purchasedProducts->get();

Undefined variable on Laravel

I´m a beginner starting with Laravel. I´m trying to show a question made on this website.
Here is the Controller page:
public function show($id)
{
//Use the model to get 1 record from the database
$question = $Question::findOrFail($id);
//show the view and pass the record to the view
return view('questions.show')->with('question', $question);
}
I have included at the top of the file:
use App\Question;
Here is my blade page:
#section('content')
<div class="container">
<h1> {{ $question->title }} </h1>
<p class="lead">
{{ $question->description }}
</p>
<hr />
</div>
#endsection
In the model I have not defined anything since I don´t need to specify any special rule. And finally here is the Route:
Route::resource('questions', 'QuestionController');
I got the error "ErrorException Undefined Variable: Question" and supposedly the error is on:
$question = $Question::findOrFail($id);
I´m looking forward to your observations.
Kind Regards.
You just need to change the controller section
public function show($id)
{
//Use the model to get 1 record from the database
$question = Question::findOrFail($id); // here is the error
//show the view and pass the record to the view
return view('questions.show')->with('question', $question);
}
Explanation:- You are going to use a variable $Question that is not defined. This is the basic PHP error, not the laravel issue.
However, You are using the "App\Question" model class not a sperate variable.

displaying an array but it return empty

Good day, i have a laravel 5.8 project and i'm having a little problem
i have this code
public function doctor_details($doctor_id){
$doctor = DB::table('doctors')->select('specification')->where('doctor_id', $doctor_id)->get>first();
$specs_data = explode(',', $doctor_specs);
$specs_array = [];
foreach ($specs_data as $doctor_spec) {
$doctor_spec_result = DB::table('specializations')->where { return explode('speciality_id'',', $doctor_spec)->get();
foreach ($doctor_spec_result as $doctor_spec_res) {
$specs_array[] = $doctor_spec_res->speciality_name;
}
}
return view ('doctor_details', compact('doctor', 'specs_array'));
}
now if i do dd($doctor_spec_result); the result is
as you can see i'm getting an empty array but if i do dd($specs_data); the result is
as you can see there's definitely a data but i can't make it work
this is my blade
<div class="row">
<div class="col-lg-12">
<h3>{{ $doctor->doctor_name }}</h3>
</div>
<div class="col-lg-12">
#foreach( $specs_array as $spec_output )
<p>{!! $spec_output !!}</p>
#endforeach
</div>
</div>
I think you are trying to get an list only containing the values of the specification field for the particular rows queried, so that you can then get the specialty_names. You can use the pluck method on the builder to do this for you. If a search by doctor_id could return more than one result:
$doctor_specs = DB::table('doctors')
->where('doctor_id', $doctor_id)
->pluck('specification')
->transform(function ($item) { return explode(',', $item); })
->flatten();
$specs_array = DB::table('specializations')
// use whereIn to find all the rows by 'specialty_id'
->whereIn('speciality_id', $doctor_specs)
->pluck('specialty_name')
->toArray();
return view ('doctor_details', compact('doctor', 'specs_array'));
Laravel 6.x Docs - Database - Query Builder - Retrieving Results pluck
Laravel 6.x Docs - Collections - Methods - transform
Laravel 6.x Docs - Collections - Methods - flatten
Update:
Though since doctor_id is a key there will only be one, we can remove the collection methods and deal with this simpler:
$doctor_specs = explode(
',',
DB::table('doctors')->where('doctor_id', $doctor_id)
->value('specification')
);
Ideally:
Or if $doctor was retrieved with all columns from the doctors table, including specification:
$doctor_specs = explode(',', $doctor->specification);
You can simplify your code using join
$doctor_spec_result = DB::table('doctors')
->select('specializations.speciality_name')
->join('specializations','specializations.speciality_id','doctors.specification')
->where('doctors.doctor_id', $doctor_id)
->get();
return view ('doctor_details', compact('doctor', 'doctor_spec_result '));
Change in this line.
$doctor_spec_result = DB::table('specializations')->where('speciality_id', $doctor_spec)->get();
you are checking the worng attribute. As you are getting specification in 0 index.
speciality_id to specification

Laravel 5.3 Method currentpage does not exist

My Pagination is not working in latest version of laravel 5.3,
I have used Paginate Method and i just wanted to know why current page method is not exist.
public function getIndex($author =null)
{
if(!is_null($author)){
$quote_author = Author::where('name', $author)->first();
if($quote_author){
$quotes = $quote_author->quotes()->orderBy('created_at','desc')->paginate(6);
}
else{
$quotes = Quote::orderBy('created_at','desc')->paginate(6);
}
return view('index',['quotes' => $quotes]);
}
$quotes = Quote::all();
return view('index',['quotes' => $quotes]);
}
<div class="pagination">
#if($quotes->currentpage() !==1)
<span class="fa fa-caret-left"></span>
#endif
#if($quotes->currentpage() !== $quotes->lastpage() && $quotes->hasPages())
<span class="fa fa-caret-right"></span>
#endif
</div>
The method name is currentPage() with a capital P.
You can learn about the other paginator methods in the documentation.
Also, in the instance the $author is null, you fall back to Quote::all() which is not paginated. Convert that to Quote::paginate(6) so that $quotes will always be an instance of a paginator.
Method name currentPage() P must be in capital letter
currentPage() is a method on the Paginator class. When you use all(), you get an instance of Collection.
What I think you want is $quotes = Quote::paginate($n); where $n is the number of results you want per page.

Resources