How to get one record by decrypting - php

I'm using an encrypt script. I can store data one by one as encrypt. And I can get all the table data by decrypting using below code.
public function alldata(Request $request)
{
$data = Contact::all();
return view('mail.list', ['data' => $data]);
}
Now, I have problem. I'm trying to get on record but it didn't decrypt.
Could someone please tell me what is wrong with my code below?
public function onerecord(Request $request)
{
$param = ['id' => $request->id];
$data = DB::select('select * from contacts where id = :id', $param);
return view('mail.one', ['data' => $data]);
}
UPDATES
This is my current code
public function one(Request $request)
{
$data = Contact::find($request->id);
return view('mail.one', ['data' => $data]);
}
my blade files
#foreach ($data as $val)
<tr>
<td>{{ $val->id }}</td>
</tr>
#endforeach
Result
using below code
public function one(Request $request)
{
$data = Contact::where('id',$request->id)->first();
return $request->all();
}
{
"id": "1"
}

One easy way is use laravel eloquent
public function onerecord(Request $request)
{
$data = Contanct::where('id',$request->id)->first();
return view('mail.one', ['data' => $data]);
}
or
public function onerecord(Request $request)
{
$data = Contanct::whereId($request->id)->first();
return view('mail.one', compact('data'));
}
both of them are same
Update:
public function onerecord(Request $request)
{
$data = Contanct::where('id',$request['id'])->first();
return view('mail.one', ['data' => $data]);
}
or
public function onerecord(Request $request)
{
$data = Contanct::whereId($request['id'])->first();
return view('mail.one', compact('data'));
}
Hope this is helpful

Related

Passing a variable in the laravel controller

How to pass a variable($test) from store to index? because I would like to display a variable in my index.blade
public function index()
{
return view('users.index', [
'users' => User::all()
]);
}
public function store(Request $request)
{
$user = new User($request->all());
$user->save();
$test = "test";
return redirect('users');
}
To resolve your problem you may edit your code like below:
index function:
public function index($test=null)
{
return view('users.index', [
'users' => User::all(),
'test' => $test
]);
}
store function:
public function store(Request $request)
{
$user = new User($request->all());
$user->save();
$test = "test";
return redirect(route('users.index', compact('test')));
}
N.B: for storing your user I don't recommend to you mass assignment (new User($request->all())) when you create a new user especially if you have a password or token to store there.

Laravel trying to store multi-data

I'm trying to store multi-data to my pivot table. I have CategoryUser table with category_id and user_id.
Store function
public function store(Request $request)
{
foreach ($request->userId as $key => $userId) {
$data = new CategoryUser();
$data->user_id = $userId;
$data->category_id = $request->categoryId[$key];
$data->save();
}
return redirect()->back();
}
In my Blade file i have name="categoryId[]" and name="userId[]".
However, it stores only one category_id. What am I doing wrong?
For 1 user ID case, loop through category ID array first.
public function store(Request $request)
{
foreach ($request->categoryId as $key => $categoryID) {
$data = new CategoryUser();
$data->user_id = $request->userId[0];
$data->category_id = $categoryID[$key];
$data->save();
}
return redirect()->back();
}

Laravel get only on user

Hey guys so I made a route:
Route::get('/dashboard/{user}', [DashboardController::class, 'show'])->name('dashboard.show');
My controller is
public function show($id)
{
return view('dashboard.profile')->with('name',User::where($id));
}
How do pass it into the view? so I get only data from the current user / userid
You can simplify it to this by using route model binding:
public function show(User $user)
{
return view('dashboard.profile', [ 'user' => $user ]);
}
You Can Do This:
public function show(User $user)
{
return view('dashboard.profile', [ 'user' => $user ]);
}
Or :
public function show($id)
{
$user = User::findOrFail($id);
return view('dashboard.profile', [ 'user' => $user ]);
}
Or :
public function show($id)
{
$user = User::where("id",$id)->first();
return view('dashboard.profile', [ 'user' => $user ]);
}
If You Need Authenticated user use :
auth()->user

Why laravel create method return Data missing from carbon, but using new model() works

in IdentityController.php
public function store(Request $request)
{
$req = $request->all();
$req['user_id'] = Auth::user()->id;
$input = PersonalData::updateOrCreate($req);
}
this return
"Data missing"
C:\Project\vendor\nesbot\carbon\src\Carbon\Traits\Creator.php
but if i use this, it works
public function store(Request $request)
{
$input= new PersonalData;
$input->user_id = Auth::user()->id;
$input->save();
}
can someone tell me why this happened? thanks before

Laravel 5 modal form data not updating

My route:
Route::post('/edit/{id}',['as'=>'edit','uses'=>'datacontroller#update']);
My controller:
public function update(Request $request, $id)
{
$task = veriler::findOrFail($id);
$input = $request->all();
$task->fill($input)->save();
return redirect()->back();
}
My modal form page:
Only the last data updating. I could not solve the problem.
public function update(Request $request, $id)
{
$task = veriler::findOrFail($id);
$input = $request->all();
$task->fill($input);
$task->save();
return redirect()->back();
}
Fill function takes the input, and save function saves to the databaes.

Categories