How to call a function save(Request $request) from another function in the same Controller ?
foreach ($_parts as $key => $part_data) {
$request->item = $part;
$request->qty = $part_data->quantity;
$call = $this->save($request);
}
My save function:
public function save(Request $request) {
dd($request->all());
}
The dd($request->all()) returns [].
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
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
i just trying to crud system. for that into controller store function my code is
public function store(Request $request)
{
Article::create([
'user_id' => auth()->id(),
'content' => $request->content,
'live' => (boolean)$request->live,
'post_on' => $request->post_on
]);
return redirect('/articles');
}
it's enough to store data, but when i want to edit article & save again then what will be my edit function code? i have no idea. i trying same code into edit function & it create new article not update. so what will be right code for edit function? thanks
Resource controller method for update is update(). Eloquent method for update() is update() too, so you can do this:
public function update(Request $request, $id)
{
Article::where('id', $id)->update($request->all());
return redirect('/articles');
}
You also can use the same controller and Eloquent method for both crerate and update data updateOrCreate() method.
You can also update it as object format like this.
public function update(Request $request, $id)
{
$article = Article::find($id);
$article->user_id = auth()->id();
$article->content = $request->content;
$article->live = (boolean)$request->live;
$article->post_on = $request->post_on;
$article->save();
}`
//route//
Route::any('/update/{id}', 'ProductController#update');
//controller//
public function update(Request $request, $id) {
$product = $request - > all();
Product::find($id) - > update($product);
return redirect('/product') - > with('message', 'Success', compact('product'));
}
you can use
public function update(Request $request, $id)
{
$article = Article::find($id);
$article->fill($request->all());
}
sure you should add Your column attributes to $fillable array in Your model
protected $fillable = ['user_id', 'content', 'live'];
public function update(Request $request, $id) {
Article::where('id', $id)->update($request->except(['_token']));
return redirect('/articles');
}
If you auto-generate resource controller for a specific Model using php artisan make:model -a Artical then you have the update() function like below:
public function update(Request $request, Article $article)
{
//
}
Here, Lavarel automatically fetch the Article object into $article. So, you can save the $request data like below:
public function update(Request $request, Article $article)
{
$article->update($request->all());
return redirect()->route('article.index'); // or your desired location :)
}
public function update(Request $request, $id)
{
$info = array('user_id' =>$request->user()->id,
'content' => $request->content, 'live'=>$request->live);
DB::table('article')->where('id', $id)->update($info);
session()->flash('success', 'Update Successfully');
return redirect('/article');
}
First, you are having two actions right, the create and update, so for a real crud in laravel you might have these two in a separated logic methods the store() and update():
/**
* This is a resource create method which is a HTTP POST.
*/
public function store(Request $request) {
// create a new item in database
}
/**
* This is a resource update which is a HTTP PUT METHOD.
*/
public function update(Request $request, $id) {
// update the item in database
}
You set up your routes withPOST for creating and PUT to update then your doing a proper crud resource.
I recommend you to separate the create logic off the update logic and if you have sort of unique data then you should validate its value before creating a new resource.
I used the function below to insert my users data to database from a form.
public function store(UsersRequest $request, Users $user)
{
Users::create($request->all());
echo $user->id;
}
I tried to get the last inserted id but nothing is showing. What am I getting wrong?
try
public function store(UsersRequest $request, Users $user)
{
$user = Users::create($request->all());
echo $user->id;
}
Or
public function store(UsersRequest $request)
{
$user = new Users($request->all());
$user->save($user);
echo $user->id;
}