Is is possible to modify a request before it gets inserted into the database?
public function store(StoreRequest $request)
{
$request->date_posted = strtotime($request->date_posted);
//insert data here.
}
Yes it can be, what you have works perfectly fine
public function store(StoreRequest $request)
{
$request->date_posted = strtotime($request->date_posted);
or
$datePosted = $request->date_posted + 2;
$datePosted = $request->date_posted . 'some other addons';
//insert data here.
}
these are just examples but i hope you get what im saying.
Fyi if you input into the db, the created_at and updated_at will update and so you don't need to insert the time the post was made manually.
Try this solution maybe can resolve your issue . Add setter in model so it will be look like this
Class ModelX {
public function setDateDatePosted Attribute($date_posted)
{
$this->attributes['date_posted'] = strtotime($date_posted);
}
}
hope it useful for you .
Related
If I have a model called Placement which can have many PlacementTqLimit and I want to get the latest one, I'm assuming I can use this.
public function latestLimit()
{
return $this->hasOne(PlacementTqLimit::class)->latestOfMany();
}
And then add a custom attribute to get the value
public function getLatestLimitValueAttribute()
{
if (empty($this->latestLimit())) {
return '100';
}
return $this->latestLimit()->value;
}
However, when I dump the $this->latestLimit() I'm getting a Illuminate\Database\Eloquent\Relations\HasOne... I'm sure I shouldn't have to call return $this->hasOne(PlacementTqLimit::class)->latestOfMany()->latest(). Am I doing something wrong here?
$this->latestLimit() will return hasOne that extend query builder.
If you want to get the latest of PlacementTqLimit model just use $this->latestLimit without () or you can also use $this->latestLimit()->first()
So, to get the latest value limit, example code is:
public function getLatestLimitValueAttribute()
{
if (!$this->latestLimit) {
return '100';
}
return $this->latestLimit->value;
}
I am writing a recursive function to call the child record from the parent record. It seems not to be working. i am getting this error; "Trying to get property 'refid' of non-object". Where am i getting it wrong. Please any idea? below is the code Snippet.
the function controller
public function DisplayDetail($id)
{
$displayDetail = DB::table('tblmembers')
->where('refid',$id)
->get();
return $this->DisplayDetail($displayDetail->refid);
}
main controller where the function is called
public function dashboard()
{
$profile = DB::table('tblmembers')->where('username',$userid)->first();
$data['userdetail'] = $this->DisplayDetail($profile->memid);
return view('main.userArea',$data);
}
the blade where the record fetched is displayed
#foreach($userdetail as $userd)
{{ $userd->memid }}
#endforeach
my sample data
refid | memid
-------------------
12345 | 123456
123456 | 1234567
123456 | 1234568
123456 | 1234569
1234567 | 1234570
from the above table; refid: 123456 brought memid: 1234567,1234568,1234569. then refid: 1234567 brought memid: 12345670
i want to display all the memid after login in as a user with memid 123456
You are doing one thing wrong in your function DisplayDetail. Here the the correction in your function
If you want to get single item then here is the correct code.
public function displayDetail($id)
{
$displayDetail = DB::table('tblmembers')
->where('refid',$id)
->first();
if($displayDetail) {
$displayDetail['userdetail'] = $this->displayDetail($displayDetail->refid);
}
return $displayDetail;
}
And dashboard function will be look like this
public function dashboard()
{
$profile=DB::table('tblmembers')->where('username',$userid)->first();
$userDetail = $this->DisplayDetail($profile->memid);
return view('main.userArea',[
'userdetail' => $userDetail
]);
}
This is the correct code. Try this and let me know if you have another query on this.
The error:
Trying to get property 'refid' of non-object
is occuring because your database query is using ->get(), which returns a Collection, rather than an object. You cannot get the property ->refid on a Collection, you can only get it from an object that resides in the collection.
As Lakhwinder Singh shows in his code, you need to use ->first(), as this will return one object. I would suggest using ->firstOrFail(), that way you will either get back an object which matches your ID, or it will fail if it cannot find it.
If you do:
$displayDetail = DB::table('tblmembers')
->where('refid',$id)
->firstOrFail();
You will now be able to call:
$displayDetail->refid
You can use that in your function call to displayDetail.
try this function
public function DisplayDetail($id,$data=[])
{
$displayDetail = DB::table('tblmembers')
->where('refid',$id)
->get();
if($displayDetail && isset($displayDetail->refid))// your condition for last child
{
$data = $this->DisplayDetail($displayDetail->refid,$data);
}
$data[] = array('refid' =>$displayDetail->id ,
'memid' => $displayDetail->secondid );
return $data;
}
i'll explain you later first modify according to your requirement and run
public function show(Criminal $criminal){
$profile = Criminal::with(['profile','crimes'])->findOrFail($criminal);
dd($profile);
}
I have this method and it should return like this when I type localhost:8000/criminal/1
But when I say like criminal/3
it also returns the criminal/1 json output like this :
the first entry looks like this :
try this
public function show(Criminal $criminal, $id){
$profile = Criminal::with(['profile','crimes'])->findOrFail($id);
dd($profile);
}
There is no need to query the database again, the model passed to your function is already an eloquent model.
public function show(Criminal $criminal) {
dd($criminal);
}
If you really want to lazy load the relations, this can be done as follows:
public function show(Criminal $criminal) {
$criminal->load('profile', 'crimes')
dd($criminal);
}
This should not be necessary however as Laravel loads relations when needed.
The problem looks basic but it is really painful!
I'm using get method and getting value in controller and I want the same value to return in another view.
How can I do that???
Please help!!!
This is my function from controller:
public function guest(){
if (Input::get('Cash On Delivery')){
$get = Input::get('Cash On Delivery');
return Redirect::to('guest/guestview/'.$get);
}
Well, with regards to your answer, using $_REQUEST directly is not Laravel's way of doing things :(
I believe this is better
public function guest(Request $request)
{
if ($request->payment_method == ('Cash On Delivery'))
{
return view('guest/guestview', ['guest'=>$request->payment_method]);
}
}
Ok Guys, I figured it out,
Just do this below.
public function guest(Request $request){
if ($request->payment_method == ('Cash On Delivery')){
$get = $_REQUEST['payment_method'];
return view('guest/guestview', compact('get'));
}
Let me explain about my problem.
I am currently using Laravel 5.0. Here is my structure
Table: bgts, Model: Bgt, Controller: BgtController
Table: bgthistories, Model: BgtHistory
Now I want to do these:
Everytimes creating new item into bgts table, I want to make a copy and insert into bgthistories table. Then, everytimes that record is updated, i'll copy one more version, still insert into bgthistories.
Here is store() method.
public function store(Request $request) {
$bgt = new Bgt();
$history = $this->coppy($bgt);
$uploader = new UploadController('/data/uploads/bgt');
$bgt->name = $request['name'];
$bgt->avatar = $uploader->avatar($request);
$bgt->attachments($uploader->attachments($request));
//dd($bgt);
$bgt->save();
$history->save();
return redirect('bgt');
}
And this is the coping:
public function coppy($bgt) {
$array = $this->$bgt->toArray();
$version = new BgtHistory();
foreach($array as $key => $value) {
$version->$key = $value;
}
return $version;
}
I create migration tables already. Everything is ready. But, when I call
$bgt->save();
$history->save();
It did not work. If I remove $history->save();, it create new record ok. I think the save() method that built-in in Model provided by Laravel is problem. Can anyone tell me how to solve this.
I tried to build the raw query then executed it by DB:statement but it did not work too. Every try to execute anything with DB is failing.
Please research before re-inventing the wheel.
(Same stuff different sites in case one is down)
http://packalyst.com/packages/package/mpociot/versionable
https://packagist.org/packages/mpociot/versionable
https://github.com/mpociot/versionable
Cheers and good luck ;)