In CodeIgniter 3.x, we can pass data with array by using this code:
$data=array(
'apartment' => $this->apartmentmodel->get_all(),
'title' => "List Apartement"
);
$this->load->view('apartment/index',$data);
But, this code is not working when implement on my Laravel 6 project
$data=array(
'apartment' => DB::table('apartment')->get();,
'title' => "List Apartement"
);
return view(apartment.index,$data);
What is the problem and how to fix it? Anyone can help me?
Just remove ; from line number 2 before , your it will be work
$data=array(
'apartment' => DB::table('apartment')->get(),
'title' => "List Apartement"
);
return view('apartment.index', $data);
or you can use compact too, see below docs
https://laravel.com/docs/6.x/views
In Laravel we can use eloquent for fetch data from database.
$data=[
'apartment' => Apartment::get();,
'title' => "List Apartment"
];
return view('apartment.index', ['data' => $data]);
It will help full for you.
Thanks
PHpanchal
return view('apartment.index',compact('data'));
You can use compact() method, and you have an error (if this is project code).
Try:
$apartment = DB::table('apartment')->get();
$title = 'List Apartement';
return view('apartment.index', compact('apartment', 'title'));
Or
$data=[
'apartment' => DB::table('apartment')->get();,
'title' => "List Apartement"
];
return view('apartment.index', compact('data'));
Or:
$data=[
'apartment' => DB::table('apartment')->get();,
'title' => "List Apartement"
];
return view('apartment.index', ['data' => $data]);
Update:
In template file you can use it by calling $data, because in return statement you pass the variable name that will be shared with current template. So, to get some data from your array $data you just need to do:
#foreach($data['apartment'] as $apartment)
{{ $apartment }}
#endforeach
And to get your title use $data['title'] as well
Related
I have following test function to check the update data is correct or not.
It has no problem in updating.
My question is how to check the given parameters are correct after updated.
for example
if response.id == 1 and response.name = 'Mr.Smith'
assertcode = OK
else
assertcode = NG
public function user_update_info(){
$this->post('login',['email' => config('my-app.test_user'),
'password' => config('my-app.test_pass')]);
$response = $this->post('/update_info',[
'id' => 1,
'name' => 'Mr.Smith',
'post_code' => '142-4756',
'prefectural_code' => '15',
'address' => 'Merchat St.',]);
$response->assertStatus(200);
}
Assume your update_info route update User model.
Try below after your code,
$user = User::find(1);
static::assertTrue($user->id == 1 && $user->name = 'Mr.Smith');
To check if the response returns a json data you expect, you can use assertJson() method of the response object like so:
$response->assertJson([
'id' => 1,
'name' => 'Mr.Smith'
]);
DB::table('job_post')->insert(['user_id'=>$user->id,'job_title'=>$data['job_title'],'job_skill'=>$data['skill'],'max_sal'=>$data['max_sal'],'min_sal'=>$data['min_sal'],'job_type'=>$data['job_type'],'shift'=>$data['shift'],'last_date'=>$data['last_date'],'job_image'=>$imagename,'job_description'=>$data['job_description'],'latitude'=>$data['latitude'],'longitude'=>$data['longitude'],'location'=>$data['location']]);
//return redirect()->to('employer/congrats/......here i want id of job_post which i create');
return view('employer/congrats',compact('id','navbar_data'));
Yes, You Can Get last inserted id in Laravel by insertGetId:
$id = DB::table('users')->insertGetId(
array('email' => 'john#example.com', 'votes' => 0)
);
return redirect('employer/congrats',compact('id','navbar_data'));
You could try something like this
$id = DB::table('job_post')->insertGetId([
// ... data to insert e.g. 'user_id' => $user->id,
]);
return view('employer.congrats', compact('id','navbar_data'));
or
DB::table('job_post')->insert([
// ... data to insert e.g. 'user_id' => $user->id,
]);
$id = DB::getPdo()->lastInsertId();
return view('employer.congrats', compact('id','navbar_data'));
For more, check out this blog post.
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpExceptionNomessage
I'm getting this error while trying to update a record in the database. Don't know what's the problem. This question might be a duplicate but I've checked all and couldn't find the answer. Please Help me with this.
Controller Update Method:
public function updateEvent(Request $request, $id=''){
$name = $request->name;
$startdate = date_create($request->start_date);
$start_date = $startdate;
$time = $request->start_time;
$start_time = $time;//date("G:i", strtotime($time));
$endDate = date_create($request->end_date);
$end_date =$endDate;
$time_e = $request->end_time;
$end_time = $time_e;//date("G:i", strtotime($time_e));
$location = $request->location;
$description = $request->description;
$calendar_type = $request->calendar_type;
$timezone = $request->timezone;
if ($request->has('isFullDay')) {
$isFullDay = 1;
}else{
$isFullDay = 0;
}
DB::table('events')->where('id', $id)->update(
array(
'name' => $name,
'start_date' => $start_date,
'end_date' => $end_date,
'start_time' => $start_time,
'end_time' => $end_time,
'isFullDay' =>$isFullDay,
'description' => $description,
'calendar_type' => $calendar_type,
'timezone' => $timezone,
'location' => $location,
));
// Event Created and saved to the database
//now we will fetch this events id and store its reminder(if set) to the event_reminder table.
if(!empty($id))
{
if (!empty($request->reminder_type && $request->reminder_number && $request->reminder_duration)) {
DB::table('event_reminders')->where('id', $id)->update([
'event_id' => $id,
'type' => $request->reminder_type,
'number'=> $request->reminder_number,
'duration' => $request->reminder_duration
]);
}
}
else{
DB::table('event_reminders')->insert([
'type' => $request->reminder_type,
'number'=> $request->reminder_number,
'duration' => $request->reminder_duration
]);
}
return redirect()->back();
}
Route:
Route::post('/event/update/{id}', 'EventTasksController#updateEvent');
Form attributes :
<form action="/event/update/{{$event->id}}" method="POST">
{{ method_field('PATCH')}}
i'm calling the same update function inside my calendar page and it working fine there. Don't know why it doesn't work here.
Check the routeing method.
Route::patch('/event/update/{id}', 'EventTasksController#updateEvent');
patch should be the method called on route facade.
Change your route to patch:
Route::patch('/event/update/{id}', 'EventTasksController#updateEvent');
For your comment:
You can send the method to the ajax call by something like data-method attribute on the element you click on,take the method and use it in your ajax call. see this question/answer for help. How to get the data-id attribute?
I need to return a json with all products and stores.
I tried this
$products = $products->paginate(20);
$stores=$this->getStores();
return ['product'=> $products, 'stores' =>$stores];
but $products returns empty. (If i run "return $products" it works fine)
Is there something i can do? Why laravel doesn't let me have the pagination in an array?
You could do something like this :
return Response::json(
array(
'products' => $products,
'stores' => $stores,
), 200
);
You also need to 'use' the Response facade:
use Illuminate\Support\Facades\Response;
Solution:
return ['product'=> $products->toArray(), 'stores' =>$stores];
or
return Response::json(
array(
'products' => $products->toArray(),
'stores' => $stores,
), 200
);
I have the following query
public static function createConversation( $toUserId )
{
$now = date('Y-m-d H:i:s');
$currentId = Auth::user()->id;
$results = DB::table('pm_conversations')->insert(
array( 'user_one' => $currentId, 'user_two' => $toUserId, 'ip' => Request::getClientIp(), 'time' => $now )
);
return $results;
}
How would i return the id of the row just inserted?
Cheers,
Instead of doing a raw query, why not create a model...
Call it Conversation, or whatever...
And then you can just do....
$result = Conversation::create(array( 'user_one' => $currentId, 'user_two' => $toUserId, 'ip' => Request::getClientIp(), 'time' => $now ))->id;
Which will return an id...
Or if you're using Laravel 4, you can use the insertGetId method...In Laravel 3 its insert_get_id() I believe
$results = DB::table('pm_conversations')->insertGetId(
array( 'user_one' => $currentId, 'user_two' => $toUserId, 'ip' => Request::getClientIp(), 'time' => $now )
);
This method requires that the id of the table be auto-incrementing, so watch out for that...
The last method, is that you can just return the last inserted mysql object....
Like so...
$result = DB::connection('mysql')->pdo->lastInsertId();
So if you choose that last road...
It'll go...
public static function createConversation( $toUserId )
{
$now = date('Y-m-d H:i:s');
$currentId = Auth::user()->id;
$results = DB::table('pm_conversations')->insert(
array( 'user_one' => $currentId, 'user_two' => $toUserId, 'ip' => Request::getClientIp(), 'time' => $now )
);
$theid= DB::connection('mysql')->pdo->lastInsertId();
return $theid;
}
I would personally choose the first method of creating an actual model. That way you can actually have objects of the item in question.
Then instead of creating a model and just save()....you calll YourModel::create() and that will return the id of the latest model creation
You can use DB::getPdo()->lastInsertId().
Using Eloquent you can do:
$new = Conversation();
$new->currentId = $currentId;
$new->toUserId = $toUserId;
$new->ip = Request::getClientIp();
$new->time = $now;
$new->save();
$the_id = $new->id; //the id of created row
The way I made it work was I ran an insert statement, then I returned the inserted row ID (This is from a self-learning project to for invoicing):
WorkOrder::create(array(
'cust_id' => $c_id,
'date' => Input::get('date'),
'invoice' => Input::get('invoice'),
'qty' => Input::get('qty'),
'description' => Input::get('description'),
'unit_price' => Input::get('unit_price'),
'line_total' => Input::get('line_total'),
'notes' => Input::get('notes'),
'total' => Input::get('total')
));
$w_id = WorkOrder::where('cust_id', '=', $c_id)->pluck('w_order_id');
return $w_id;