Reputation: 154
(Im rather new to laravel but ill try to explain my problem as best as I can) I'm trying to create a remark application that should be simple enough, where the admin can pass some data to a specific user and the user can see the data on a table from their own dashboard.
So I thought I should show all the users in one table then from a button or text i can click to create a "remark notice" and it would choose that user based on their id and then store it on a table I have already made a foreign key with the two tables Notice at the recieverID with the ID of the Users table This is how the table with all the users look this is how all users look and the button to access the create function
Then it shows the create view of the remark here
Here I made it so it gets the id on the link of the specific user url with specific id
This is the code that I am using for this(RemarkController)
public function index()
{
$users = User::all();
return view('admin.remark.index', compact('users'));
}
public function create()
{
// $user = User::find($id);
return view('admin.remark.create', compact('user'));
}
public function store(Request $request)
{
$this->validate($request,[
'title'=>'required',
'subject'=>'required',
'message'=>'required',
'sender'=>'required'
]);
$data=$request->all();
$data['recieverID']=auth()->user()->id;
Remark::create($data);
return redirect()->route('remarks')->with('message','Remark Created Successfully'); }
And this is how I use the code to show the id of the selected user on the link and i was thinking because of this it would store on the table
<td><a href="{{route('remarks.create', $user->id)}}"><i class="fa fa-edit" style="font-size:20px; color: #e03e27;"></i></a></td>
But instead, I get an error like this
ErrorException Trying to get property 'id' of non-object
I'm not sure what to do since I'm a beginner and I would really appreciate your help thank you EDIT: Error picture error picture
Upvotes: 1
Views: 171
Reputation: 2271
Based on your comments, my suggestion is to exclude create method when you create the resource route.
Route::resource('remarks','RemarkController', [
'except' => ['create']
]);
Make a new route for create like so:
Route::get('remarks/create/{user}', 'RemarkController@create')->name('remarks.create');
And then change your create method to implicitly bind user:
public function create(User $user)
{
return view('admin.remark.create', compact('user'));
}
EDIT:
As for store method, I understand from your comments that the data['receiverId']
should be the id
of the user
sent to create method / user id in the create link. I suggest you post this id in a hidden field in your form. So, include a new hidden field in the create form:
<input type="hidden" value="{{$user->id}}" name="receiverId" />
This way you can get rid of the $data['receiverId']
line, because you are sending that along with other form data.
Edit 3: removed post/redirect bit since it looks like it was a misreading on my part.
Upvotes: 1
Reputation: 31
I think your problem is that you try to find the user by the id, but the create() function doesn't have it as a parameter. So the correct way should be:
public function create($id)
{
$user = User::find($id);
return view('admin.remark.create', compact('user'));
}
Upvotes: 1
Reputation: 138
As the error says, you're trying to get the property id
with an object accessor ->
, while $user isn't an object. Try to dump the user with dd($user)
and check, what you get. If you get an array, you'd need to use []
to access it.
Upvotes: -1