I have form with one question and four choices, I am sending data into question table with
$this->db->insert('questions',$quest);
and choices in choices table with $this->db->insert_batch('choices',$choices); I am getting last insert id of question as $last_id = $this->db->insert_id(); I want to send this id with each choice in question_no column in the choices table, how do I do that?
If I am getting it right and $choices is an array.
Send both the arrays together from your controller.
$this->model_name->whatever($choices, $quest);
then whatever function you have in model
function whatever($choices, $quest){
$this->db->insert('questions',$quest);
$last_id = $this->db->insert_id();
$choices['quest_id'] = $last_id;
$this->db->insert_batch('choices',$choices);
}
Related
I have a eloquent model say Math which has a column in db say number, which is of type array
in db suppose,
for id =1 number = [1,2]
When I try to edit this entry from form, I have to select values 1,2 again. As in the data is not persisting
if($form->isEditing()){
$number = Math::where('id', 1)->pluck('number', 'number');
$form->multipleSelect('number')->options(NumberList::all()->pluck('id', 'id'))->default($number);
}
I have tried doing the above, but the data is not persisting in edit mode. How to solve this?
I want match data from "jobTable" based on job id then i got 5 data within this 5 data i have userId i want to show data from "userTable" based on userID. Then i will catch it on variable and i want to show it on my view then i will show it using loop.
If i do this below code it shows all data from my userTable not show based on matched JobID base UserId Details.
public function applyUser($jobId){
$applyUsers=ApplyInfo::where('job_id', $jobId)->get();
//5 rows found, with in this 5 rows everybody has userId I want to show data from user table based on this found userId and pass value a variable then i will loop it on view page.
foreach ($applyUsers as $applyUser){
$applyUsersDtials=Employee::find($applyUser->user_id)->get();
}
return view('front.user.apply-user-details', ['applyUsersDtials'=>$applyUsersDtials]);
}
//when i do that that time show all data based on my user table not show based on my JobId
First of all in your code $applyUsersDtials is a variable it should be an array. Next when you need to find a single row you just need to use find(). Try this-
public function applyUser($jobId){
$applyUsers = ApplyInfo::where('job_id', $jobId)->get();
$applyUsersDtials = [];
foreach ($applyUsers as $applyUser){
$applyUsersDtials[] = Employee::find($applyUser->user_id);
}
return view('front.user.apply-user-details',
['applyUsersDtials' => $applyUsersDtials]);
}
in my Laravel 5 application i have many to many relationship between two models.I'm using a pivot table to keep track of them. In my both models i have defined belongsToMany method with relevant pivot table name as parameter.Then i'm going to add values to the pivot table in controller. It works fine for only one column. For the other one it's not inserting any values. In the Controller i'm calling like this,
$this->mymodel->addToPivotTable($values);
Should i pass two parameters there?
I could able to solve this. I needed to call the method after saving my data set to the table.It's like this,
public function add(Request $request){
$post = $request->all();
$arr = array(1,4,5);
$result = $this->mymodel->create($post);
$result->classifications()->attach($arr);
}
I have relationship between two tables: Blog, Images.
Blog Model
public function images(){
return $this->hasMany('App\ImageBlog', 'id_blog', 'id');
}
Controller
$lastPosts = Blog::orderBy('id', 'desc')->with('images')->take(3)->get();
So, how can I get last three rows where images is not null?
If I understood you correctly, you want to use has() method:
$lastPosts = Blog::orderBy('id', 'desc')->has('images')->take(3)->get();
Another optimize way of getting post with images is as follows
$lastPosts=Blog::orderBy('id','desc')
->with(array('images'=>function($query){
$query->select('id','image'....);
}))->take(3)->get();
Here in $query->select() you can pass only those column name which are required from image table instead of getting whole data so it will reduce your response time.
I am having 2 tables, users and profiledetails
I am able to run Join query and access the data and send to view.
But when I am manipulation the field 'dob' (date format) in profiledetails table. No Success, Please check the code below ,
webpagesConroller:
$users = DB::table('users')
->join('profiledetails', 'users.id', '=', 'profiledetails.user_id')
->select('users.*', 'profiledetails.dob')
->get();
$age = $users->dob->diffInYears(Carbon::now());
return view('webpages.index',compact('users'))->with($age);
View:
<li class="cate_head">Age : {{ $age}}</li>
Error:
Trying to get property of non-object
I have model Profiledetails added the mutators as below,
public function getAge(){
return $this->dob->diffInYears(Carbon::now());
}
public function getDOB(){
return $this->dob->format('d-m-Y');
}
Can I not use this method on another controller for Ex- webpagesController, If yes How.
Since you are using a raw query, the data returned is not an object but an array with the data.
Also you did not limit the query, meaning it could return multiple rows.
You'll probably need to get the data from the $users as:
//with the possibily of multiple rows. Index is the row which has to be used
$users[index]['dob']
//when you are sure it only returns one row
$users[0]['dob'] // or $users['dob'] not sure as of the moment
You want to check the differences with Carbon.
So you will probably need to make a Carbon object of the dob result and check it against the Carbon::now.
$age = Carbon::createFromFormat('Y-m-d', $users[0]['dob'])->diffForHumans();
// diffForHumans() uses the local time
Note that this only gets the age for one row (the index or the first because of [0]).
If you want to do it for every row use a for or foreach where you set the $users->age for every $user. But as these are not object/models but raw data, this will not work.
Hope this helps your question a bit