Find max id from collection with Laravel - php

I am trying to find max id from model.
My model is MainsliderStatu.
Here is My Code
$slider = new MainsliderStatu();
$slider = $slider->find($slider->select('MAX(id) AS id'));
$slider->statu = Input::get('statu');
$slider->save();
But I am getting this error:
Controller method not found.
I thing it cant find select method.
How can we do it?

This is solution:
if(empty($slider->count())){
$slider->statu = Input::get('statu');
$slider->save();
flash()->success('Slider Durumu Değiştirildi.');
return Redirect::to('admin/mainslider');
}else{
$maxId = DB::table('mainslider_statu')->max('id');
$slide = MainsliderStatu::find($maxId);
$slide->statu = Input::get('statu');
$slide->save();
flash()->success('Slider Durumu Değiştirildi.');
return Redirect::to('admin/mainslider');
}
DB::table('mainslider_statu')->max('id') solved my problem.

After save, $slider->id should be the last id inserted.
$slider = new MainsliderStatu();
$slider->statu = Input::get('statu');
$slider->save();
$slider->id;
If you would like to get back the latest id without create new model:
DB::table('mainslider_statu')->max('id');
Check if there isn't any row:
if(empty(MainsliderStatu::all()->toArray()))
{
$slider = new MainsliderStatu();
$slider->statu = Input::get('statu');
$slider->save();
$id = $slider->id;
}
else
{
$id = DB::table('mainslider_statu')->max('id');
}

Related

How to get Multiple data in laravel

this is the controller im getting error Property [{$key}] does not exist on this collection instance.
public function showcustom($id)
{
$custompack = CustomPackages::find($id);
$venue = Venue::find($custompack->venue);
$food = Food::all();
$attire = Attire::all();
$hairmakeup = HairMakeup::all();
$invitation = Invitation::all();
$photosvideos = PhotosVideos::all();
$cakescupcakes = CakesCupcakes::all();
$lightsound = LightSound::all();
$programhost = ProgramHost::all();
$eventsingers = EventSingers::all();
//$custompack = CustomPackages::all();
return view('packages.customshow',compact('venue','custompack','packages','food','attire','hairmakeup','photosvideos','cakescupcakes','lightsound','programhost','eventsingers','invitation'));
}
Please check if the venue you are passing in Venue::find(integer_expected) is an integer..

Check value from another table and based on the value save in current

I want to check what value has column in another table and based on that value to save in current table. Here is the controller so you can get what I mean
$preference = Preferences::pluck('preferences_is_active');
$book = new book;
$book->book_description = Input::get('description');
$book->book_title = Input::get('title');
$book->user_id = Auth::user()->id;
if($preference !== 1) {
$book->book_published = 0;
$book->save();
}
return Redirect::to('/users/books');
So I want user to be able to save new books' information in his profile. But also want to check column preferences_is_active in table preferences and do the conditions.
The problem is that no matter what value has preferences_is_active it's always saves 0 in books table. What I miss here?
The problem is pluck will return acollection, so you can do it like this :
$preference = Preferences::first();
$book = new book;
$book->book_description = Input::get('description');
$book->book_title = Input::get('title');
$book->user_id = Auth::user()->id;
$book->book_published = $preference->preferences_is_active;
$book->save();
return Redirect::to('/users/books');
Try this code.
$preference = Preferences::first();
$book = new book;
$book->book_description = Input::get('description');
$book->book_title = Input::get('title');
$book->user_id = Auth::user()->id;
if(!$preference) {
$book->book_published = 0;
$book->save();
}
return Redirect::to('/users/books');

Phalcon validation messages on related (aliased) models

Assuming I try to save the following data and the Songs model's name attribute has a Phalcon\Mvc\Model\Validator\PresenceOf validator set on it
// Get an existing artist
$artist = Artists::findFirst('name = "Shinichi Osawa"');
// Create an album
$album = new Albums();
$album->name = 'The One';
$album->artist = $artist;
$songs = array();
// Create a first song
$songs[0] = new Songs();
$songs[0]->name = 'Star Guitar';
$songs[0]->duration = '5:54';
// Create a second song
$songs[1] = new Songs();
$songs[1]->name = '';
$songs[1]->duration = '4:29';
// Assign the songs array
$album->songs = $songs;
// Save the album + its songs
if (!$album->save()) {
foreach ($album->getMessages() as $message) {
$message->getModel(); // this returns null since model is new
}
}
I will get an error message saying that 'name' is required.
Question: is there a built in way of getting the related model on which the error occurred (in this case $songs[1]), or at least the alias/index of the error model (in this case songs/1)?
Didn't find a built-in solution but came up with this.
Base model
public function beforeValidation()
{
$session = $this->getService('session');
if (!$session->has('model:validation')) {
$validation = $indexes = [];
} else {
$modelName = get_called_class();
$validation = $session->get('model:validation');
if (!isset($validation[$modelName])) {
$validation[$modelName] = 0;
$indexes = $session->get('model:validation:relationIndex');
$indexes[] = $modelName;
} else {
$validation[$modelName]++;
// reset child indexes
$indexes = $session->get('model:validation:relationIndex');
$modelIndex = array_search($modelName, $indexes);
for ($i = $modelIndex + 1; $i < count($indexes); $i++) {
$modelName = $indexes[$i];
unset($validation[$modelName]);
unset($indexes[$i]);
}
$indexes = array_values($indexes);
}
}
$session->set('model:validation:relationIndex', $indexes);
$session->set('model:validation', $validation);
}
Controller
$session = $this->getDI()->get('session');
$session->remove('model:validation');
$session->remove('model:validation:relationIndex');
if (!$this->save()) {
var_dump($session->get('model:validation:relationIndex'));
var_dump($session->get('model:validation'));
}

How can I save a list of ids in user meta?

I am trying to save what will amount to a small list of ids in user meta, but for some reason I am only able to save the most recent visit. Is there something obviously wrong with my approach?
function check_visit() {
$user = get_current_user_id();
$post_visits = get_user_meta($user, 'post_visits', true);
$visited = explode(",",$post_visits);
$id = (string)the_ID();
if($id && !in_array($id, $visited)) {
$visited[] = $id;
update_user_meta($user, 'post_visits', implode(",", $visited));
}
print_r(implode(",",$visited));
}
Actually
$visited[] = $id
is correct!
The issue is that you are using the_ID() function, this function print the ID, do not return any value.
The correct function should be get_the_ID()
Your code should look like this:
function aw_check_visit(){
$user = get_current_user_id();
$post_visits = get_user_meta($user, 'post_visits', true);
$visited = explode(",",$post_visits);
$id = (string)get_the_ID();
if($id && !in_array($id, $visited)) {
$visited[] = $id;
update_user_meta($user, 'post_visits', implode(",", $visited));
}
}
You need to be using array_push instead of adding it to the array like you have.
Change this
$visited[] = $id;
to
array_push($visited, $id);

lithium insert entity array into model object property

The following coding sample doesn't work.
$joiner = new Entity();
$joiner->name = $post['name'];
$joiner->address = $post['address'];
$meeting = Meeting::first($_id);
$meeting->joiner[] = $joiner;
$meeting->save();
I hope each time when the form posted success. a new joiner entity should be pushed into the 'joiner' property of meeting object.
How to correct it ? and thanks for all help.
updated
now I have changed it like this .it works fine but still not very good yet.
$joiner = new Entity();
$joiner->name = $post['name'];
$joiner->address = $post['address'];
$meeting = Meeting::first($_id);
if(empty($meeting->joiner)){
$joiners = array($joiner);
}
else{
$joiners = array();
foreach($meeting->joiner as $one){
$joiners[] = $one;
}
$joiners[] = $joiner;
}
$meeting->joiner = $joiners;
$meeting->save();

Categories