Little question: Currently I've a route
Route::get('update/{id?}', 'SessionController#onClick');
That loads with this function:
public function onClick($id, Request $request)
{
$data = $request->all();
$user = User::where('userRooms', $id)->first();
return view('sessions.cards')->with('user', $user);
}
I want that my blade view displays all users that have access to this room, currently I hard coded everything. My question is now, what do I have to write down in my code so that users have access to multiple rooms and not just one(userRooms row in my db for User::Class) & how I can display all users that have this room?
I hope I understood your question correctly. Try this way:
public function onClick($id, Request $request)
{
$data = $request->all();
$users = User::where('userRooms', $id)->get();
return view('sessions.cards', [
'users' => $users,
]);
}
Related
My question is, if there is data in the cache, I created a condition by saying fetch, and if not, I bring it from the database and refresh the cache for a certain period of time. Normally, if I don't use the cache, I can search the page, but I cannot search by id or nickname with the code below.So when using cache::get can I add a search condition?
public function index(Request $request)
{
$submit = $request->get('submit');
$id = $request->get('id');
$nick_name = $request->get('nick_name');
if (Cache::has('users')) {
$users = Cache::get('users');
return view('admin.users.index', compact('users'));
}
$users = User::when(!empty($id), function ($query) use ($id) {
$query->where('id', $id);
})->when(!empty($nick_name), function ($query) use ($nick_name) {
$query->where('nick_name', $nick_name);
})->orderBy('created_at', 'asc')->get();
Cache::put('users', $users, now()->addMinutes(60));
return view('admin.users.index', compact('users'));
}
Update: Based on the examples on the page below, the problem was solved
https://www.honeybadger.io/blog/caching-in-laravel/
You should add search criteria in your cache key. E.g.
public function index(Request $request) {
$submit = $request->get('submit');
$id = $request->get('id');
$nick_name = $request->get('nick_name');
$cacheKey = User::class . md5(json_encode([$submit, $id, $nick_name]));
if (Cache::has($cacheKey)) {
/* ... */
}
because now your cache writes only first search result and ignores everything else.
Or write to cache all users. Then run that array through array_filter applying filters. But that can be waste of developer time if search is very dynamic.
I have Certificate and Student models, with one to many relation.
When the user is done making the certificate he is redirected to another page with student form, I dont know how to get the certificate_id or how to relate the student to the last certificate that it was created. help please.
Certificate Controller
public function store(StoreCertificateRequest $request)
{
$data = $request->validated();
$data['user_id'] = auth()->id();
Certificate::create($data);
return redirect()->route('students.create')->with(['success' => 'Certificate has been Saved']);
}
Student Controller
public function store(StoreStudentRequest $request)
{
$data = $request->validated();
Student::create($data);
return redirect()->route('users.index')->with('success', 'Students were added');
}
So the certificate is already created, you can get that id by assigning a variable to the create, and then return it to the view as $variable->id
I am trying to update my template details in the controller. But currently, my code is not working. It is not updating the fields. Anyhow if I add $template->save(). It saves the updated record as a new record. How to make my current code work? Why I am facing this situation? Please, someone, explain to me and correct my code as still, I am learning Laravel. Thanks in advance.
update function in TemplateController
public function update(Request $request, $id)
{
if(! lara_club_has_permission('edit-template') ){
return view('403');
}
$this->validate($request, [
'title'=>'required',
'start_date'=> 'required',
'end_date'=>'required',
'template_content'=>'required',
]
);
//check status response
if(isset($request->status)&&$request->status=='on'){
$status='1';
}else{
$status="0";
}
$template=new Template();
$template->title=$request->title;
$template->start_date=convert_to_sql_date_format($request->start_date);
$template->end_date=convert_to_sql_date_format($request->end_date);
$template->is_active=$status;
$template->template_content=$request->template_content;
$input = $request->all();
$template->update($input);
$name = $input['title'];
return redirect()->route('templates.index')->with('success', 'Template <b>'. $name.'</b> Updated!');
}
you creating new object $template=new Template();
but you need to update existing one so try below code
$template=Template::find($id);
//or use $template=Template::where('id',$id)->first();
$template->title=$request->title;
$template->start_date=convert_to_sql_date_format($request->start_date);
$template->end_date=convert_to_sql_date_format($request->end_date);
$template->is_active=$status;
$template->template_content=$request->template_content;
$input = $request->all();
$template->update($input);
You should use Laravel find method, instead of declaring a new instance of it.
It should be
$template=Template::find($id);
Not
$template=new Template();
In my case I specify the table name in the method name, like this.
public function update(Request $request, Template $id)
{
$id->title=$request->title;
$id->start_date=convert_to_sql_date_format($request->start_date);
$id->end_date=convert_to_sql_date_format($request->end_date);
$id->is_active=$status;
$id->template_content=$request->template_content;
$input = $request->all();
$id->update($input);
}
In my case , I am using like this..
public function update(Request $request, $id){
$employee = Employee::find($id);
$employee->name=$request->name;
$employee->email=$request->email;
$employee->phone=$request->phone;
$input = $request->all();
$employee->update($input);
//$validated = $this->validateEmployee($request);
//Employee::update($validated);
return response()->json('Data Updated');
}
I get this error when running this code
public function update(StoreAccountsRequest $request, $id)
{
$user = User::findOrFail($id);
$user->Accounts()->update($request->except('_method', '_token'))->where('accounts.user_id', '$user');
return redirect()->route('accounts.index')->with(['message' => 'Accounts updated successfully']);
}
please I need help understanding how to update a single user account in the accounts Table
Code must e like this
$user->Accounts()->update($request->except('_method', '_token'));
So I got posts page, where I have my post. The post has ID which leads to posts table. For it I have PostsController where everything is accessible thru $post->id, $post->name etc.
But I have another table named orders and on submit button, it needs some of the information from posts table by id.
I have post with url posts/{id} and there's submit button in the view
The button needs to get $post information to OrdersController with submit information.
I've tried this in OrdersController
public function store(Request $request)
{
$this->validate($request, [
]);
$post = Post::all();
$order = new Order;
$order->client = Auth::user()->name;
$order->phone = Auth::user()->phone;
$order->vehicle = $post->id;
$order->date_from = $request->input('date_from');
$order->save();
return redirect('/posts')->with('success', 'Rezervēts');
}
$posts = Post::all(); is getting all array information from table but cannot use specific id.
And I have this in PostsController
public function show($id)
{
$post = Post::find($id);
$images = Image::All();
return view('posts.show')->with(compact('post', 'images'));
}
Route for show function
Error that I'm getting with shown code: Property [id] does not exist on this collection instance.
Expected results:
orders table
Error: Property [id] does not exist on this collection instance.
Problem: Can't get exact id from view to another controller, so I can access posts table records that I need from OrdersController.
Ps. I've been thru most of the pages on this error, but can't seem to find the one that would help.
I would modify the route so that the affected post.
Route:
Route::post('/order/{post}/store', 'OrderController#store')->name('order.store');
Controller (store method):
public function store(Post $post, Request $request){
$this->validate($request, [
]);
$order = new Order;
$order->client = Auth::user()->name;
$order->phone = Auth::user()->phone;
$order->vehicle = $post->id;
$order->date_from = $request->input('date_from');
$order->save();
return redirect('/posts')->with('success', 'Rezervēts');
}
Okay, thanks.
Solved: {{ Form::hidden('id', $post->id) }} in view.
Controller: $post = Post::findorfail($request->id);
Then: $post->(whatever you need from table)