Property [name] does not exist in laravel - php

I want to show my product in a single page but I have "Property [name] does not exist on this collection instance." error
blade:
<div class="title">
<h2>{{ $singleproduct->name }}</h2>
</div>
<div class="single-product-price">
<h3>{{ $singleproduct->price }}</h3>
</div>
<div class="single-product-desc">
<p>{!! $singleproduct->explain !!} </p>
</div>
controller:
public function show()
{
$singleproduct = Singleproduct::get();
return view('UI.store.SingleProduct' , compact('singleproduct' ));
}
route:
Route::get('/singleproduct/{product}' , 'admin\SingleproductController#show');

You have a Collection of potentially many or no Singleproducts. If you only want 1 you would use first.
Most likely because this is a show route you want a specific Singleproduct, which I will guess you are passing an 'id' via the URL.
public function show($product)
{
$singleproduct = Singleproduct::findOrFail($product);
return view('UI.store.SingleProduct', compact('singleproduct'));
}
Now in the view you know that singleproduct is definitely an instance of Singleproduct and is accessible.

You need to select only one product not all of them,
so you can update your show method like this
using the route model binding you can read more about this awesome feature in laravel docs
public function show(Singleproduct $product)
{
return view('UI.store.SingleProduct' , compact('product' ));
}

Related

Beginner in Laravel - Eager loading seems to not work?

I'm new to Laravel and I'm trying to make a post where the user's post can receive likes from other users. But I'm trouble at when you click the user, it was suppose to direct to the users page where you can see all its post and the likes the it received. This is the code that suppose to do that work:
This is the controller:
public function index(User $user)
{
$posts = $user->posts()->with('user', 'likes')->get();
return view('users.posts.index', [
'user' => $user,
'posts' => $posts,
]);
}
}
and this is the view/template:
<div class="flex justify-center">
<div class="w-8/12 bg-white p-6 rounded-lg">
{{$user->name}}
</div>
</div>
I don't know what's the problem though, or is it a bug with Laravel eager loading?
But unfortunately it only returns a blank page, there's no error though.
The thing is after putting this code inside dump and die, there's data showing from the database. I don't why it doesn't show. Can someone help me with this problem? Much appreciated
Following this tutorial: https://www.youtube.com/watch?v=MFh0Fd7BsjE
Tutorial Github: https://github.com/codecourse/posty-traversy-media
If you want to access the posts you can iterate through them:
#foreach($posts as $post)
{{ $post->title }}
{{ $post->likes->count() }}
#endforeach
Not sure what data or relationships you want to access.
Check your route whether it has a user id for model injection
eg:/index/{user}
also in your blade example, there is no posts are used.you are using the user object
public function index(User $user)
{
$posts = $user->posts()->with('user', 'likes')->get();
return view('users.posts.index')->with([
'user' => $user,
'posts' => $posts,
]);
}

Array to string conversion error, Laravel

I'm trying to save to database my data, but I get error "Array to string conversion". It's my controller
public function create()
{
$listapacjentow = Patient::pluck('nazwisko','id');
return view('leczenie.create')->with('listapacjentow', $listapacjentow);
}
public function store(CreateOperacjaRequest $request)
{
$operacja = new Operacja($request->all());
$PacjenciIds = $request->input('PacjentList');
$operacja->user()->associate($PacjenciIds);
$operacja->save();
return redirect('leczenie');
}
It's my form
<div class="form-group">
<div class="col-md-4 control-label">
{!! Form::label('PacjentList','Wybierz pacjenta:') !!}
</div>
<div class="col-md-6">
{!! Form::select('PacjentList[]', $listapacjentow); !!}
</div>
When I return $operacja is everything ok, but I can't save it to database
Try with this approch in your store method.
$operacja = new Operacja($request->all());
$operacja->user_id = $request->input('PacjentList.0');
$operacja->save();
$operacja->user()->saveMany($patients);
If you want to associate only one user with Operacja, do this:
{!! Form::select('user_id', $listapacjentow); !!}
$operacja = Operacja::create($request->all());
If you want to add many users to the Operacja, you'll need to revert one to many relationship, so User will have operacja_id and Operacja will not have user_id.
If you want to make Operacja have many users and a user could have many Operacja, use many to many relationship.

I am learning laravel and i want to try show a single book through a wildcard but it shows an error

here is my route
Route::get('showbooks', 'AdminController#show');
Route::get('showbooks/{book_id}', 'AdminController#singleBook');
here is methods for all and single book
public function show()
{
$allBooks = Book::all();
return view ('admin.showbooks', compact('allBooks'));
}
public function singleBook($book_id)
{
$book = Book::find($book_id);
return view ('admin.singlebook', compact('book'));
}
here is link for single book
#foreach($allBooks as $book)
<h2> Book Id is: {{ $book->book_id }}</h2>
<h4>Book Title is: {{ $book->title }}</h4>
#endforeach
this is my single book view
<div class="col-lg-9">
<h1>{{ $book->book_id }}</h1>
<h3>{{ $book->tile }}</h3>
</div>
You should set primary key in your model. Because by default primary key is taken as "id".
Thats why when you write
$book = Book::find($book_id);
laravel makes sql as
where books.id=blabla
Change your model as below:
class Book extends Eloquent {
protected $primaryKey = 'book_id';
}

Laravel displaying data from 2 tables

Let me explain situation first.
I am on the page with list of skills available such as "Plumber","Carpenter" and "Painter", when I click on one of those skills I want to get a list of handymans that have that skill and once clicked on a handyman I get full details about him.
Skills are displayed however when I click on one of the skills it doesn't want to retrieve any data. Both tables "handymen" and "skills" have many to many relationship and also there is a junction table. What am I doing wrong here?
Route::group(['middleware' => ['web']], function () {
Route::get('home', 'HandymanController#home');
Route::get('search', 'HandymanController#search');
Route::get('details/{handyman}', 'HandymanController#details');
Route::post('assignjob', 'HandymanController#assignJob');
Route::get('addjob', 'HandymanController#addJob');
Route::post('addjform', 'HandymanController#addjForm');
Route::get('jobs', 'HandymanController#jobs');
Route::get('jobsdetails/{jobId}', 'HandymanController#jobsdetails');
Route::get('deletejob', 'HandymanController#deleteJob');
Route::post('deletejform', 'HandymanController#deletejForm');
Add Job View:
#extends('layouts.master')
#section('title', 'Add Job')
#section('header2')
<ul>
<li>Assign job</li>
</ul>
#show
#section('content')
<h1>Handyman details</h1>
<ul>
#foreach ($handymen as $handyman)
<a href= "{{ url("HandymanController#details", $handyman->id) }}">
{{$handyman->name}}
</a>
#endforeach
</ul>
Controller:
function search()
{
$skills = Skill::all();
return view('layouts/search',['skills' => $skills]);
}
function details($skillId)
{
$skill = Skill::find($skillId);
$handymen = $skill->handymen;
return view('layouts/details', ['handymen' => $handymen]);
}
According to your edited question, first you list all the skills in your search view. So, in your search view, you would have something like this:
#foreach ($skills as $skill)
<a href= "{{ action("HandymanController#addjForm", $skill->id) }}">
{{ $skill->name }}
</a>
#endforeach
So you have to define in your routes file this route:
Route::post('addjform/{skill}', 'HandymanController#addjForm');
This will point to Handyman Controller, where you will list all handymen that have that skill:
public function addjForm(Request $request, Skill $skill)
{
$handymen = $skill->handymen;
return view('layouts/skilledHandymen', ['skill' => $skill,'handymen' => $handymen]);
}
For this to work, you have to define in Skill Model, the association:
public function handymen()
{
return $this->belongsToMany(Handyman::class,
'handyman_skill',
'skill_id',
'handyman_id');
}
The controller will point to a view where you will list all handymen that have such skill:
In your case, it would be easier if you define an association in Skill model that links to Handyman:
#foreach ($handymen as $handyman)
<a href= "{{ action("HandymanController#details", $handyman->id) }}">
{{ $handyman->name }}
</a>
#endforeach
When you choose a handyman, it will take you to Handyman controller details:
function details(Request $request, Handyman $handyman)
{
return view('layouts/details', ['handymen' => $handymen]);
}
For this you will define this route:
Route::get('/handyman/{handyman}', 'Handyman#details');
And this will point you finally to the details of chosen handyman, and you can show his details:
<p>{{ $handyman->id }}<p>
<p>{{ $handyman->name }}</p>
The thing that is important to understand here is that you will first have a Collection of skills that will lead you to a Collection of Handymen, and not just a single one. After you choose a Handyman from the second list you will be able to show his details. If you try to jump over this step you will be trying to show details of a list.
Hope this helps...

How to show related items with pagination in Eloquent and Blade?

AdminsController
public function index(){
$someMessages=$this->blog->paginate(5);
$users=User::all();
// return 'Welcome Admin';
return View::make('admin.admin',['someMessages'=>$someMessages,'users'=>$users]);
}
admin.blade.php
Here need to display username from 'users' variable based on uid stored in 'someMessages' object.
#extends('layout.default')
#section('title')
<title>Welcome Admin</title>
#stop
#section('content')
#foreach($someMessages as $message)
<blockquote>{{$message['blog']}}
<small>
<cite>
</cite>
</small>
</blockquote>
#endforeach
{{ $someMessages->links()}}
#stop
If you have a Eloquent relation between blog messages and users defined correctly you don't need to query the users separately. What you want to do is just go with the eager loading:
//in case you call it author in your "relation definition"
$someMessages = $this->blog->with('author')->paginate(5);
And then in your Blade template:
#foreach($someMessages as $message)
<div>
<div>{{ $message->blog }}</div>
<div>{{ $message->author->name }}</div>
</div>
#endforeach
If you haven't declared the relationship yet - it's easy. Open up your user eloquent model class and add a method:
public function blogs()
{
return $this->hasMany('Blog', 'uid');
}
In the blog eloquent model class you'd add:
public function author()
{
return $this->belongsTo('User', 'uid');
}
More about eloquent and relationships can be found here.
If that's not the case you can still filter your users collection to get the one you want:
#foreach($someMessages as $message)
<?php
$user = $users->filter(function($user) use ($message)
{
return $user->id == $message->uid;
})->first();
?>
<div>
<div>{{ $message->blog }}</div>
<div>{{ $user->name }}</div>
</div>
#foreach
What happens here is you filter your users collection based on $message->uid value and take the first one from it (it should always be just one or none since user IDs are unique).
Although you need to understand that in this case you will get all the users from database and filter through them for every blog message you're outputting. Eager loading is a much better idea here and I'd stick to it if possible.

Categories